PHP无法发布到MySQL数据库
我有一个formText.php文件,其中包含一个表单,表单代码如下:PHP无法发布到MySQL数据库,php,mysql,database,Php,Mysql,Database,我有一个formText.php文件,其中包含一个表单,表单代码如下: <form action="insert.php" method="post"> <p> <label for="theNames">Name:</label> <input type="text" name="theName" id="theName"> </p> <p>
<form action="insert.php" method="post">
<p>
<label for="theNames">Name:</label>
<input type="text" name="theName" id="theName">
</p>
<p>
<label for="theCitys">City:</label>
<input type="text" name="theCity" id="theCity">
</p>
<p>
<label for="theAges">Are you over eighteen?(Y/N)</label>
<input type="text" name="theAge" id="theAge">
</p>
<p>
<label for="theDates">Date:</label>
<input type="text" name="theDate" id="theDate">
</p>
<input type="submit" value="Submit">
</form>
姓名:
城市:
你超过十八岁了吗?(是/否)
日期:
然后我有一个带有以下脚本的insert.php文件:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "root","phpteste");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security (EDITED)
$theName = mysqli_real_escape_string($link, $_POST['theName']);
$theCity = mysqli_real_escape_string($link, $_POST['theCity']);
$theAge = mysqli_real_escape_string($link, $_POST['theAge']);
$theDate = mysqli_real_escape_string($link, date("Y-m-d h:i:s",$_POST['theDate']));
// attempt insert query execution
$sql = "INSERT INTO tabelateste (id, name, city, overeighteen, date) VALUES (NULL, '$theName', '$theCity', '$theAge', '$theDate')";
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);
?>
您的post数据名称字段错误。因此,您需要更改以下行:
// Escape user inputs for security
$theName = mysqli_real_escape_string($link, $_POST['theName']);
$theCity = mysqli_real_escape_string($link, $_POST['theCity']);
$theAge = mysqli_real_escape_string($link, $_POST['theAge']);
$theDate = mysqli_real_escape_string($link, date("Y-m-d h:i:s",$_POST['theDate']));
您需要根据数据库表结构将日期更改为注册日期
$sql = "INSERT INTO tabelateste (name, city, overeighteen, signup_date) VALUES ('$theName', '$theCity', '$theAge', '$theDate')";
也许像这样试试吧
if(isset($_POST['submit']) && !empty($_POST) ){
$theName = $_POST['theName'];
$theCity = $_POST['theCity'];
$theAge = $_POST['theAge'];
$theDate = $_POST['theDate'];
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "phpteste";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO tabelateste (name, city, overeighteen, date)
VALUES ('$theName ', '$theCity ', '$theAge ', '$theDate ')";
if ($conn->query($sql) === TRUE) {
$last_id = $conn->insert_id;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
if(设置($\u POST['submit'])&&!空($\u POST)){
$theName=$_POST['theName'];
$theCity=$_POST['theCity'];
$theAge=$_POST['theAge'];
$theDate=$_POST['theDate'];
$servername=“localhost”;
$username=“root”;
$password=“root”;
$dbname=“phpteste”;
//创建连接
$conn=newmysqli($servername、$username、$password、$dbname);
//检查连接
如果($conn->connect\u错误){
die(“连接失败:”.$conn->connect\U错误);
}
$sql=“插入到选项卡中(名称、城市、日期)
值(“$theName”、“$theCity”、“$theAge”、“$theDate”);
if($conn->query($sql)==TRUE){
$last\u id=$conn->insert\u id;
}否则{
echo“Error:”.$sql.“
”$conn->Error;
}
}
使用此代码我刚刚测试了您的代码(复制和粘贴),它在我的服务器配置(Windows10-PHP5.6)下运行良好。我最好的猜测是您在表名或MySQL配置中有一个输入错误
如果您从其他站点复制了此代码。请检查您是否创建了数据库和表,以及MySQL配置是否正确
检查此类错误的一个好方法是阅读PHP错误日志是否有任何错误?没有,屏幕上不会简单显示任何内容。如果$\u POSTFirst中的变量名不正确,请使用简单转储/呈现发布数据的测试脚本替换insert.php,以验证formText.php生成的表单是否正在向insert.php发送正确的数据。然后验证insert.php是否可以通过硬编码在数据库中插入虚拟记录来修改数据库。然后让他们一起工作。您的列名是signup_date,但在insert中是dateChanage sql to:$sql=“insert INTO tabelateste(name,city,overeighten,date)值(“$theName”,“$theCity”,“$theAge”,“$theDate”);将数据插入数据库时,您是否遇到任何错误?或者echo
您的查询并尝试将其直接运行到数据库中。仍然不起作用。我在那里有空值,因为id是空值。但是,没有运气。不知道我可能做错了什么。你的数据库结构是什么?id
是否自动递增和主键?是否。tabelateste(id INT NOT NULL主键自动递增,名称VARCHAR(20),城市VARCHAR(20),overeighteen CHAR(1),注册日期);仍然返回空页。数据库上没有错误,但没有输出或条目。您的数据库上有自动递增的ID吗?是的,我有,第一列。我刚从sql行中取出ID。试试看,试过了,没修好。该死的!打印(邮政美元);useto debug.values是否正确post我正在用PHP5.5.9运行Xubuntu 14.04,但我无法让它正常工作。有什么问题吗?你能检查一下PHP错误日志吗?它们往往是有用的
$sql = "INSERT INTO tabelateste (`name`, `city`, `overeighteen`, `date`) VALUES ('$theName', '$theCity', '$theAge', '$theDate')";