正在使用XAMPP和PHP版本5.4.27,并编写一个简单表单的代码,将字段中的数据插入到我的localhost数据库中
我使用XAMPP Control Panel
创建了数据库,数据库名称为sitedb
并创建了一个表:customers
列是:
这是我的PHP代码:
<html>
<body>
<?php
$con = mysql_connect("localhost","root","");
$conSet = mysql_select_db("sitedb",$con);
$users_name = $_GET['name'];
$users_email = $_GET['email'];
$users_tele = $_GET['tele'];
$users_age = $_GET['age'];
$users_name = mysql_real_escape_string($users_name);
$users_email = mysql_real_escape_string($users_email);
$users_tele = mysql_real_escape_string($users_tele);
$users_age = mysql_real_escape_string($users_age);
$res = mysql_query("INSERT INTO `sitedb`.`customers`(`CustomerID`,`Full Name`,`Age`,`E-mail`,`Tele Phone`) VALUES (NULL,'$users_name','$users_age','$users_email','$users_tele')",$con);
mysql_close($con);
?>
</body>
</html>
表单的HTML代码:
<html>
<body>
<form action="addCustomer.php" method="get">
Full Name: <input type="text" name="name"><br>
Age: <input type="text" name="age"><br>
Email: <input type="text" name="email"><br>
TelePhone: <input type="text" name="tele"><br>
<input type="submit">
</form>
</body>
</html>
当我通过localhost
输入html文件并填写表单的fiels并单击“提交”按钮时,不会显示任何错误,但数据未插入
为什么?
答案 0 :(得分:1)
customerID不能为null。所以查看这个查询:
$res = mysql_query("INSERT INTO `sitedb`.`customers`(`Full Name`,`Age`,`E-mail`,`Tele Phone`) VALUES ('$users_name','$users_age','$users_email','$users_tele')",$con);
此外,您应该检查错误。
答案 1 :(得分:0)
是客户ID具有主键,因此客户ID自动获取值并自动递增。因此无需定义客户ID ...导致错误的是SQL查询。
答案 2 :(得分:0)
<html>
<body>
<?php
$con = mysql_connect("localhost","root","");
$conSet = mysql_select_db("sitedb",$con);
$users_name = $_GET['name'];
$users_email = $_GET['email'];
$users_tele = $_GET['tele'];
$users_age = $_GET['age'];
$users_name = mysql_real_escape_string($users_name);
$users_email = mysql_real_escape_string($users_email);
$users_tele = mysql_real_escape_string($users_tele);
$users_age = mysql_real_escape_string($users_age);
$res = mysql_query("INSERT INTO `sitedb`.`customers`(`CustomerID`,`Full Name`,`Age`,`E- mail`,`Tele Phone`) VALUES ('".$users_name."','".$users_age."','".$users_email."','$".users_tele."')",$con);
mysql_close($con);
?>
</body>
</html>
当你从db调用值时尝试这样做: -
VALUES('".$users_name."','".$users_age."','".$users_email."','$".users_tele."')",$con);
答案 3 :(得分:0)
执行查询后回显mysql错误(如果有)。尝试使用以下代码来获取正确的mysql错误。
$res = mysql_query("INSERT INTO `sitedb`.`customers`(`Full Name`,`Age`,`E-mail`,`Tele Phone`) VALUES ('$users_name','$users_age','$users_email','$users_tele')",$con);
if (mysql_errno()) {
echo "Mysql error : "mysql_errno($con) . "- " . mysql_error($con) . "\n";
exit;
}
mysql_close($con);
答案 4 :(得分:0)
首先将这些行放在mysql connect - &gt;
之前ini_set('display_errors','On');
error_reporting(E_ALL);
然后提交表单,很可能问题将在您的查询中。并记下此查询 - &gt;
$insert_query = "INSERT INTO `sitedb`.`customers`(`Full Name`,`Age`,`E-mail`,`Tele Phone`) VALUES ('".$users_name. "', '" .$users_age. "', '" .$users_email. "', '" .$users_tele."')";
$res = mysql_query($insert_query);
一旦您在查询中遇到任何问题,请回显您的$insert_query
变量,然后复制该查询并将其直接运行到您的xampp phpmyadmin面板中。