gpt4 book ai didi

php - $_POST ['var' ] 进入 MySQL 数据库?

转载 作者:行者123 更新时间:2023-11-29 11:22:27 25 4
gpt4 key购买 nike

我正在使用 WAMP 来尝试学习一些 PHP 和 SQL。我试图从这里的一个非常基本的表格中获取用户输入:

<form action="input.php" method="post" class="registration_form"/>
<fieldset>
<div class="elements">
<label for="name">Username :</label>
<input type="text" id="name" name="name" size="25" />
</div>
<div class="elements">
<label for="e-mail">E-mail :</label>
<input type="text" id="e-mail" name="e-mail" size="25" />
</div>
<div class="elements">
<label for="Password">Password:</label>
<input type="password" id="Password" name="Password" size="25" />
</div>
<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Register" />
</div>
</fieldset>
</form>

我希望能够获取输入并将其发布到数据库。我一直在尝试使用以下代码来实现这一点:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


$sql = "INSERT INTO MyGuests (name, email, password)
VALUES ($_POST[name], $_POST[e-mail], $_POST[password])";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}

$conn->close();
var_dump('name', 'e-mail', 'password');

?>

当我尝试插入 $_POST["name"] 中的“”时,出现错误:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\input.php on line 16

当我尝试删除“”时,出现此错误:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\input.php on line 16

我还尝试在代码顶部设置变量:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$name=$_POST['name']

//Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


$sql = "INSERT INTO MyGuests (name, email, password)
VALUES ('name','email', 'password');

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;//
}

$conn->close();
var_dump('name', 'e-mail', 'password');

?>

这样我就得到了一条错误消息:

( ! ) Parse error: syntax error, unexpected '$conn' (T_VARIABLE) in C:\wamp\www\input.php on line 9

我能够使用 $_POST 在另一个脚本中回显该名称,我不确定为什么它不能与 SQL 命令一起使用。如果有人愿意提供帮助,和/或给我一些学习/学习的资源,我将不胜感激!

最佳答案

你缺少一个“;”后:$name=$_POST['name']

关于php - $_POST ['var' ] 进入 MySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38695560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com