我有一个下面的表单,它已经工作了一段时间,但由于某种原因,代码现在已经停止插入用户通过表单输入的内容。一旦表单工作,我改变的唯一事情是样式,数据库站点上没有任何内容,或插入数据。
当我点击“提交”按钮时,我收到零错误,它将我带到我希望该表单也能吸引用户的页面。
<form action="insert.php" method="post">
<p>
<label for="firstName">First Name:</label>
<input type="text" name="name" id="Name">
</p>
<p>
<label for="emailAddress">Email Address:</label>
<input type="text" name="email" id="email">
</p>
<input type="submit" value="Submit">
我的PHP代码是:
<?php
$link = mysqli_connect("localhost", "root", "root", "travelsite");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_REQUEST['name']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
// attempt insert query execution
$sql = "INSERT INTO subscribe (name, email) VALUES ('$name', '$email')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
//关闭连接 mysqli_close($链接); ?&GT;
答案 0 :(得分:0)
尝试使用$_POST['name']
和$_POST['email']
。
同样如评论中所述:你的变量混乱了。 (thx @NitinP)
还尝试使用如下的准备语句:
/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "INSERT INTO subscribe (name, email) VALUES (?, ?)")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "ss", $name, $email); /*or $first_name*/
/* execute query */
mysqli_stmt_execute($stmt);
printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
/* close statement */
mysqli_stmt_close($stmt);
}
有关详细信息,请参阅the link under Procedural style