我使用PHP将新记录插入到MySQL数据库/表中,一切都很好。 DB和表的排序是utf8_unicode_ci。这是插入代码:
if($_SERVER['REQUEST_METHOD'] = "POST")
{
$category = mysql_real_escape_string($_POST['category']);
mysql_connect('localhost', 'xxx','xxxx') or die(mysql_error()); //Connect to server
mysql_select_db('xxxx') or die("Cannot connect to database"); //Connect to database
mysql_query("INSERT INTO categories (category) VALUES ('$category')"); //SQL query
header("location: home.php");
}
else
{
header("location:home.php"); //redirects back to home
}
我在托管服务中创建相同的数据库,我使用WAMP / phpMyAdmin / Export +数据。我的托管/ phpMyAdmin中的每个东西看起来都很好,并且表中有记录。整理是一样的。 但是当我尝试插入记录时,新文本字段为空白,我有一个新记录,但新文本字段内容为空。没有错误消息。有一个警告:
警告:mysql_real_escape_string():在第12行的/home/yyyyyy/public_html/xxxxx/add.php中拒绝用户'yyyy'@'localhost'(使用密码:NO)
'yyyy'与mysql_select_db('xxxx')中xxxx的名称不同,我不知道它来自哪里?
答案 0 :(得分:1)
检查$ category是否为null
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['category']))
{
$category = mysql_real_escape_string($_POST['category']);
mysql_connect('localhost', 'xxx','xxxx') or die(mysql_error()); //Connect to server
mysql_select_db('xxxx') or die("Cannot connect to database"); //Connect to database
mysql_query("INSERT INTO categories (category) VALUES ('$category')"); //SQL query
//header("location: home.php");
echo 'successful'
}
else
{
//header("location:home.php"); //redirects back to home
echo 'problem'
}
还要修复:
if($_SERVER['REQUEST_METHOD'] = "POST")
要
if($_SERVER['REQUEST_METHOD'] == "POST")
答案 1 :(得分:0)
试试这个mysql_query("INSERT INTO categories (category) VALUES ('" . $category . "')");
答案 2 :(得分:0)
我只是简单地将这两行带到了mysql_real_escape_string
mysql_connect('localhost', 'xxx','xxxx') or die(mysql_error()); //Connect to server
mysql_select_db('xxxx') or die("Cannot connect to database"); //Connect to database
$category = mysql_real_escape_string($_POST['category']);
它工作正常!