我正在尝试从我创建的数据库的简单联系人表格中插入数据。我正在使用phpMyAdmin
这是PHP(我出于安全原因删除了define语句,但我能够毫无问题地建立数据库链接。)
<?php
if (isset($_POST['submitted'])){
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PW', '');
define('DB_HOST', '');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PW, DB_NAME);
if(!$link){
die('Test Connection Failed 1: ' . mysql_error());
}
//Name
$FName = $_POST['FName'];
$FName = mysqli_real_escape_string($link, $FName);
$LName = $_POST['LName'];
$LName = mysqli_real_escape_string($link, $LName);
//Phone
$Phone1 = $_POST['Phone1'];
$Phone2 = $_Post['Phone2'];
$Phone3 = $_Post['Phone3'];
$Phone = $Phone1.$Phone2.$Phone3;
$Phone = mysqli_real_escape_string($link, $Phone);
//Address
$Street = $_POST['Street'];
$Street = mysqli_real_escape_string($link, $Street);
$City = $_POST['City'];
$City = mysqli_real_escape_string($link, $City);
$Zip = $_POST['Zip'];
$Zip = mysqli_real_escape_string($link, $Zip);
$Country = $_POST['Country'];
$Country = mysqli_real_escape_string($link, $Country);
//Message
$Message = $_POST['Message'];
$Message = mysqli_real_escape_string($link, $Message);
//Email
$Email = $_POST['Email'];
$Email = mysqli_real_escape_string($link, $Email);
$sqlInsert = "INSERT INTO phpmy1_belairfinishing_com.Contact (ID, FName, LName, Phone, Street, City, State, Country, Zipcode, Email, Message) VALUES (NULL, '".$FName."', '".$LName."', '".$Phone."'', '".$Street."'', '".$City."'', '".$State."'', '".$Country."'', '".$Zipcode."'', '".$Email."'', '".$Message."')";
if(!mysqli_real_query($link, $sqlInsert)){
die('SLQ Insert Statement Failed: ' . mysql_error());
} else {
echo "Posted To Database";
mysqli_close($link);
}
} //End of ISSET if statement
?>
在有人点击论坛上的提交按钮后,代码会运行,据我所知,工作正常。
错误发生在php代码的末尾。正在输出“SLQ Insert Statement Failed”但未发布mysql错误。
if(!mysqli_real_query($link, $sqlInsert)){
die('SLQ Insert Statement Failed: ' . mysql_error());
}
我的数据库/表格设置如下 数据库名称= phpmy1_belairfinishing_com 表名=联系人 列名与insert语句中的完全相同。
任何帮助将不胜感激!
编辑:如果有帮助,这是表格结构
列类型
ID tinyint(4)
FName varchar(75)
LName varchar(75)
电话int(11)
街道文字
城市文字
国家文本
国家文本
Zipcode int(10)
电子邮件varchar(75)
消息文本
我想输入的值是 空值 蒂姆 重击 111-111-1111 1随机街道 随机城市 随机状态 美国 randomemail@gmail.com randomMessage
答案 0 :(得分:0)
你不使用mysqli_stmt_bind_param
http://php.net/manual/en/mysqli-stmt.bind-param.php
但使用mysqli_real_escape_string
http://php.net/manual/en/function.mysql-real-escape-string.php
清理你的线索(但mysqli_stmt_bind_param确实是更好的做法)。
请在doc:
中查看此通知**Caution**
Security: the default character set
The character set must be set either at the server level, or with the API function mysql_set_charset() for it to affect mysql_real_escape_string(). See the concepts section on character sets for more information.