表单连接到数据库

时间:2016-04-26 23:51:34

标签: php database forms

寻找一些帮助,我有一个连接到数据库的表单,当用户输入他们的信息时,它存储在数据库中

 <?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "usbw", "demo");

// 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, $_POST['firstname']);
$last_name = mysqli_real_escape_string($link, $_POST['lastname']);
$email_address = mysqli_real_escape_string($link, $_POST['email']);
$comments = mysqli_real_escape_string($link, $_POST['comments']);

// attempt insert query execution
$sql = "INSERT INTO persons (first_name, last_name, email_address, comments) 
    VALUES ('$first_name', '$last_name',    '$email_address', '$comments')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link); 
?>

以上文件是我的插入文件,当我提交表单时出现此错误:

注意:未定义的索引:第15行的C:\ Users \ Erica \ Documents \ College \ usb webserver \ root \ WeatherStationWebsite \ insert.php中的注释

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

从您的错误中,似乎以下行是问题所在:

$comments = mysqli_real_escape_string($link, $_POST['comments']);

似乎$ _POST [&#39;评论&#39;]似乎不存在。转到您的输入页面,看看您是否在表单中正确写了name="comments"

如果能解决这个问题,请告诉我!