我有这段代码,我想在特定的数据库中插入一些值。由于某种原因,记录不会存储到数据库中。我知道我的mysql_query出了问题。
您认为可能导致此问题的问题(我已经检查过,它们不会影响我的问题): 未建立与数据库的连接。 变量不包含值。
以下是代码:
<?php
include('includes/connect-db.php');
$firstname = mysql_real_escape_string(stripslashes(trim($_POST['firstname'])));
$surname = mysql_real_escape_string(stripslashes(trim($_POST['surname'])));
$username = mysql_real_escape_string(stripslashes(trim($_POST['username'])));
$password = mysql_real_escape_string(stripslashes(trim($_POST['password'])));
$email = mysql_real_escape_string(stripslashes(trim($_POST['email'])));
$tel = mysql_real_escape_string(stripslashes(trim($_POST['tel'])));
$month = mysql_real_escape_string(stripslashes(trim($_POST['month'])));
$day = mysql_real_escape_string(stripslashes(trim($_POST['day'])));
$year = mysql_real_escape_string(stripslashes(trim($_POST['year'])));
$address = mysql_real_escape_string(stripslashes(trim($_POST['address'])));
$postcode = mysql_real_escape_string(stripslashes(trim($_POST['postcode'])));
$city = mysql_real_escape_string(stripslashes(trim($_POST['city'])));
$country = mysql_real_escape_string(stripslashes(trim($_POST['country'])));
if(isset($_POST['submit_register'])) {
if(!empty($firstname) && !empty($surname) && !empty($username) && !empty($password) && !empty($email) && !empty($tel) && !empty($month) &&
!empty($day) && !empty($year) && !empty($address) && !empty($city) && !empty($country)) {
mysql_query("INSERT INTO customers (firstname, surname, username, password, email, tel, month, day, year, address, city, country)
VALUES ('$firstname','$surname','$username','$password', '$email', '$tel', '$month', '$day','$year','$address','$postcode','$city','$country')");
echo'success';
}
else{
echo'failure';
}
}
?>
答案 0 :(得分:1)
首先,您不应该使用mysql_query ,因为从PHP 5.5.0开始不推荐使用此扩展,并且将来会被删除。相反,请使用MySQLi或PDO_MySQL扩展名。
关于您的代码,您忘记在SQL查询中添加邮政编码列类型。
mysql_query("INSERT INTO customers (firstname, surname, username, password, email, tel, month, day, year, address, postcode, city, country)
VALUES ('$firstname','$surname','$username','$password', '$email', '$tel', '$month', '$day','$year','$address','$postcode','$city','$country')");
如果这不起作用,那么检查您尝试存储到数据库的任何变量是否缺失,如果它们是否为它们提供空值。
例如:
if ( isset($_POST['firstname']) ) {
$firstname = mysql_real_escape_string(stripslashes(trim($_POST['firstname'])));
} else {
$firstname = '';
}
或者,您可以使用&#34; ternary operator&#34;:
$firstname = isset($_POST['firstname']) ? mysql_real_escape_string(stripslashes(trim($_POST['firstname']))) : '';
对所有变量执行此操作,然后尝试运行查询。
答案 1 :(得分:0)
在这种情况下我通常会做什么,我将我的sql查询语句复制到mysql终端。 我在那里尝试代码,以确定它是否运行没有任何问题。 如果没有,则mysql将引发错误。 如果它,那么我通常使用sprintf函数来格式化查询。 像这样:
if(!empty($firstname) && !empty($surname) && !empty($username) && !empty($password) && !empty($email) && !empty($tel) && !empty($month) &&
!empty($day) && !empty($year) && !empty($address) && !empty($city) && !empty($country)) {
$query = sprintf('INSERT INTO customers (firstname, surname, username, password, email, tel, month, day, year, address, city, country) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',$firstname,$surname,$username,$password,$email,$tel,$month,$day,$year,$address,$postcode,$city,$country);
$result = mysql_query($query);
if($result){
echo 'Success';
}else{
echo 'Failure';
}
}
使用sprintf函数时,%s是字符串占位符。有几个。检查php文档。