我在Xamp 5.538上运行了这个。该文件的名称是application.php。表单正常显示,我没有收到任何错误消息。但是,它不会将数据插入MYSQL数据库。到目前为止,我只是试图让表单的顶部部分工作;一旦它,我填写代码中的其余部分。我的数据库名为employees;表的名称是table1。到目前为止,它包含以下字段: 首先,最后,mi,地址,城市,州,邮政编码,电话,电子邮件,ss,dob,安全 我试图发布整个表格,但不够聪明,弄清楚如何做到这一点,或者只是我讨厌谜题。请为我解决这个问题。我已经走了,但还有更进一步。 谢谢; 埃里克 这是代码:
<?php
// Your database info
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '""';
$db_name = 'employees';
if (!empty($_POST))
{
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if ($con‐>connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '.
mysqli_connect_error());
$sql = "INSERT INTO table1 (first, last, mi, address, city, state, zip, phone, email, ss, dob, security) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
if (!$stmt = $con‐>prepare($sql))
die('Query failed: (' . $con‐errno . ') ' . $con‐error);
if (!$stmt‐ >bind_param('ssi',$_POST['fist'],$_POST['last'],$_POST['mi'],$_POST['address'],$ _POST['city'],$_POST['state'],$_POST['zip'],$_POST['phone'],$_POST['email'],$_PO ST['ss'],$_POST['dob'],$_POST['security']))
die('Bind Param failed: (' . $con‐errno . ') ' . $con‐error);
if (!$stmt‐>execute())
die('Insert Error ' . $con‐>error);
echo "Record added";
$stmt‐>close();
$con‐>close();
}
?>
答案 0 :(得分:0)
if (!empty($_POST['sub'])) {
$father = $_POST['father'];
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$con) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
echo $sql = "INSERT INTO `father`( `FirstName`) VALUES ('" . $father . "')";
if (!$stmt = mysqli_prepare($con, $sql)) {
echo "done";
}
if ($stmt->execute()) {
// it worked
} else {
// it didn't
}}
使用此代码正常工作