我有以下PHP v.5.6.29
<?php
// Connect to the database
include_once("php_includes/db_connect.php");
error_reporting(E_ALL);
ini_set("display_errors", 1);
// Gather the posted data into local variables
$m = $_POST['MRN'];
$l = $_POST['LastName'];
$f = $_POST['FirstName'];
$s = $_POST['SSN'];
// Get user IP Address
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// Form data error handling
if($m == "" || $l == "" || $f == "" || $s == ""){
echo "The form submission is missing data.";
} else {
// End form data error handling
$sql = "INSERT INTO nys_demographics (mrn, pt_last_name, pt_first_name, pt_ssn, ip_address, record_insert_dtime)
VALUES('$m', '$l','$f','$s','$ip',now()";
mysqli_query($db_connect, $sql);
echo "insert success";
}
header("refresh:3; url=demographics.php");
?>
我收到了成功消息但是db表中没有任何内容。不知道该怎么做。
答案 0 :(得分:1)
首先打印您的成功消息,只有当您从Boolean
返回时实际插入成功
if (mysqli_query($db_connect, $sql)) {
echo "insert success";
} else {
printf("Error: %s\n", mysqli_error($db_connect));
}
答案 1 :(得分:0)
您可以根据位于here的PHP文档将查询放入if
,以查看查询是否成功。
答案 2 :(得分:0)
我修改了代码以包含布尔条件检查。假设您还在db_connect.php文件中使用mysqli_connect_errno,这应该可以正常工作。
<?php
// Connect to the database
include_once("php_includes/db_connect.php");
error_reporting(E_ALL);
ini_set("display_errors", 1);
// Gather the posted data into local variables
$m = $_POST['MRN'];
$l = $_POST['LastName'];
$f = $_POST['FirstName'];
$s = $_POST['SSN'];
// Get user IP Address
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// Form data error handling
if($m == "" || $l == "" || $f == "" || $s == ""){
echo "The form submission is missing data.";
} else {
// End form data error handling
$sql = "INSERT INTO nys_demographics (mrn, pt_last_name, pt_first_name, pt_ssn, ip_address, record_insert_dtime)
VALUES('$m', '$l','$f','$s','$ip',now()";
if( mysqli_query($db_connect, $sql) ) { echo "insert success"; }
else { echo "error with insert"; }
}
header("refresh:3; url=demographics.php");
?>