我正在尝试建立一个非常简单的表单以将数据提交到MySQL数据库(我以前从未真正设置过MySQL数据库或自己通过Web表单与之交互所需的连接)。
我的网页上有一个简单的HTML表单,允许用户输入姓名,电子邮件地址和他们想问的问题:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Page title
</head>
<body>
<h1>Customer Information</h1>
<form id="customerInfoForm" accept-charset = "utf-8" action = "insert.php" method = "post" >
<table id = "customerInfoTable">
<tr><td>Name: </td><td><input type = "text" name = "customerName" ></input></td></tr>
<tr><td>Email: </td><td><input type = "text" name = "customerEmail"></input></td></tr>
<tr><td>Enquiry: </td><td><input type = "text" name = "enquiryText"></input></td></tr>
</table>
<br>
<input type = "submit" value = "Sumit"></input>
</form>
</body>
</html>
<?php
$con = mysql_connect('localhost','[retracted]','[retracted]');
if(!$con) {
echo 'Not connected to server!';
}
$Name = $_POST['customerName'];
$Email = $_POST['customerEmail'];
$Query = $_POST['enquiryText'];
$sql = "INSERT INTO Enquiries(Name, Email, Query) VALUES ('$Name', '$Email', '$Query')";
if(!mysql_query($con, $sql)) {
echo 'Data not inserted';
} else {
echo 'Data inserted';
}
?>
在单击表单上的“提交”按钮时调用的insert.php
文件中,我具有以下内容:
<?php
//Check if data has been entered
if( isset( $_POST['data'] ) && !empty( $_POST['data'] ) )
{
$data = $_POST['data'];
} else {
header( 'location: form.html' );
exit();
}
//set up mysql
$sql_server = 'localhost';
$sql_user = 'user';
$sql_pwd = 'password';
$sql_db = 'database';
//Connect to sql database
$mysqli = new mysqli( $sql_server, $sql_user, $sql_pwd, $sql_db) or die( $mysqli->error );
//Insert details into table
$insert = $mysqli->query( "INSERT INTO Enquiries ('data') VALUE ( '$data' )" );
//Close mysqli connection
$mysqli->close;
?>
但是,当前,当我单击表单上的“提交”按钮时,浏览器中显示错误:
error); //将详细信息插入表$ insert = $ mysqli-> query(“ INSERT INTO查询('data')VALUE('$ data')”); //关闭mysqli连接$ mysqli-> close; ?>
即上面的错误显示为网页的内容,因此看来我在数据库连接方面做错了,但我不确定是什么...有人可以向我指出正确的方向吗?