我正在尝试在 WordPress 页面上创建一个表单,该表单允许我将数据输入到 mysql 表中。当我输入代码时,主键会自动增加,但是数据本身不会进入数据库。这是mysql中的表信息:
CREATE TABLE `macscabs`.`customers` (
`custno` INT NOT NULL AUTO_INCREMENT ,
`sname` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`fname` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`homeaddr` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`commondest` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`mobileno` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`homeno` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`email` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
PRIMARY KEY ( `custno` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
以下是来自 WordPress 页面的代码与 php 在同一页面上:
<table>
<form name="form1" method="post" action="<?php echo $_SERVER[PHP_SELF] ?>">
<strong>Please enter your information in order to download the Macs Cabs App</strong>
<tr><td>
Surmane Name:</td><td><input name="sname" type="text" id="sname"></td></tr>
<tr><td>
First Name:</td><td><input name="fname" type="text" id="fname"></td></tr>
<tr><td>
Home Address:</td><td><input name="homeaddr" type="text" id="homeaddr"></td></tr>
<tr><td>
Most common Destination:</td><td><input name="commondest" type="text" id="commondest"></td></tr>
<tr><td>
Mobile Number:</td><td><input name="mobileno" type="text" id="mobileno"></td></tr>
<tr><td>
Home Number:</td><td><input name="homeno" type="text" id="homeno"></td></tr>
<td>
Email Address:</td><td><input name="email" type="text" id="email"></td></tr>
<tr><td>
<input type="submit" name="Submit" value="Submit"></td></tr>
</form></table>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("macscabs", $con);
$sql="INSERT INTO `customers` (`sname`, `fname`, `homeaddr`, `commondest`, `mobileno`, `homeno`, `email`)
VALUES ('{$_POST['sname']}','{$_POST['fname']}','{$_POST['homeaddr']}','{$_POST['commondest']}','{$_POST['m obileno']}',
'{$_POST['homeno']}','{$_POST['email']}')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
$info = mysql_info(); echo $info;
mysql_close($con);
?>
我还通过输入下面的代码手动插入数据而不使用表单来测试数据库和网页之间的连接,它可以工作。但是,当我尝试使用表单插入它时,我无法让它工作。
mysql_query("INSERT INTO customers (sname, fname, homeaddr, commondest, mobileno, homeno, email)
VALUES ('mcguinness','dave','moate','athlone','0254','56587',5)");