我已经尝试了所有我能想到的但是我的表单中的数据没有插入到MySQL表中。 这是我表格的相关部分:
<form id="masteringhtml5_form" action="send_formdata.php" method="POST">
<label for="heading" class="heading">Health Survey Form</label>
<fieldset class="fieldset_border">
<legend class="legend">Personal Information</legend>
<div>
<label for="name">Name</label><br>
<input type="text" name="Firstname" class="name txtinput" placeholder="First" autofocus>
<input type="text" name="Lastname" class="name txtinput" placeholder="Last">
</div>
<!-- other input fields here -->
<div class="submit">
<input type="submit" name="submit" class="submit_btn" value="Submit">
</div>
</form>
这是我的php文件“send_formdata.php”的相关部分:
<?php
mysqli_connect("localhost:3308","root","9gUCFpWmzh");
mysqli_select_db("personal_info");
if(isset($_REQUEST['submit'])) {
$errorMessage = "";
$Firstname=$_POST['Firstname'];
$Lastname=$_POST['Lastname'];
// other variables and validation here
if ($errorMessage != "" ) {
echo "<p class='message'>" .$errorMessage. "</p>" ;
} else {
//Inserting record in table using INSERT query
$insertTB="INSERT INTO `personal_info`.`personal`
(`Firstname`, `Lastname`, `Dob`, `Gender`, `Saddress`,
`Aaddress`, `City`, `State`, `Pincode`, `Country`, `Home`,
`Work`, `Email`) VALUES ('$Firstname', '$Lastname', '$Dob',
'$Gender', '$Saddress', '$Aaddress', '$City', '$State',
'$Pincode', '$Country', '$Home', '$Work', '$Email')";
mysqli_query($insertTB);
}
}
?>
数据库和表格是正确的。我用“echo $ insertTB”运行脚本,并在浏览器中生成一个完整的查询语句。
答案 0 :(得分:3)
将您的PHP代码更改为
$link = mysqli_connect("localhost:3308","root","9gUCFpWmzh") or die("cannot connect to server !!");
//mysqli_select_db("personal_info");
mysqli_select_db($link,"personal_info");
if(isset($_REQUEST['submit']))
{
$errorMessage = "";
$Firstname=$_POST['Firstname'];
$Lastname=$_POST['Lastname'];
// other variables and validation here
if ($errorMessage != "" ) {
echo "<p class='message'>" .$errorMessage. "</p>" ;
}
else{
//Inserting record in table using INSERT query
$insertTB="INSERT INTO `personal_info`.`personal`
(`Firstname`, `Lastname`, `Dob`, `Gender`, `Saddress`,
`Aaddress`, `City`, `State`, `Pincode`, `Country`, `Home`,
`Work`, `Email`) VALUES ('$Firstname', '$Lastname', '$Dob',
'$Gender', '$Saddress', '$Aaddress', '$City', '$State',
'$Pincode', '$Country', '$Home', '$Work', '$Email')";
//mysqli_query($insertTB);
mysqli_query($link,$insertTB) or die(mysqli_error($link));
}
}
答案 1 :(得分:0)
您应该更改插入查询,如下所示
替换
$insertTB="INSERT INTO `personal_info`.`personal`
(`Firstname`, `Lastname`, `Dob`, `Gender`, `Saddress`,
`Aaddress`, `City`, `State`, `Pincode`, `Country`, `Home`,
`Work`, `Email`) VALUES ('$Firstname', '$Lastname', '$Dob',
'$Gender', '$Saddress', '$Aaddress', '$City', '$State',
'$Pincode', '$Country', '$Home', '$Work', '$Email')";
mysqli_query($insertTB);
用
$insertTB="INSERT INTO `personal_info`.`personal`
(`Firstname`, `Lastname`, `Dob`, `Gender`, `Saddress`,
`Aaddress`, `City`, `State`, `Pincode`, `Country`, `Home`,
`Work`, `Email`) VALUES ('".$Firstname."', '".$Lastname."', '".$Dob."',
'".$Gender."', '".$Saddress."', '".$Aaddress."', '".$City."', '".$State.",
'".$Pincode."', '".$Country."', '".$Home."', '".$Work."', '".$Email."')";
mysqli_query($insertTB);
我们必须始终在插入查询中使用'".$address."'
编写变量作为标准实践,因为如果我们不能这样写,有时它会在“john's street”等任何字段中传递值时中断查询。所以单引号(')打破查询,值不插入表,
答案 2 :(得分:0)
要调试常规php错误,请在脚本上添加:
error_reporting(E_ALL);
ini_set('display_errors', '1');
调试数据库连接:
$dbCon = mysqli_connect('localhost', 'root', '9gUCFpWmzh', 'personal_info');
if (!$dbCon) { echo 'Connect Error ('.mysqli_connect_errno().')'. mysqli_connect_error(); exit(); }
要调试查询错误,请将mysqli_query()更改为:
mysqli_query($dbCon, $insertTB) or die(mysqli_error($dbCon));
对于您插入数据库的每个字段,请修改您的查询
而不是'$Firstname'
更改为'".mysqli_real_escape_string($dbCon, $Firstname)."'
为你所有的领域做这件事。