PHP无法插入数据
当我使用以下代码时,我无法插入数据。它显示以下错误消息: [插入数据时出错。请稍后重试。您的SQL语法有错误:请查看与您的MySQL服务器版本对应的手册,以获取第10行的“')”附近使用的正确语法]PHP无法插入数据,php,mysql,Php,Mysql,当我使用以下代码时,我无法插入数据。它显示以下错误消息: [插入数据时出错。请稍后重试。您的SQL语法有错误:请查看与您的MySQL服务器版本对应的手册,以获取第10行的“')”附近使用的正确语法] if($\u服务器['REQUEST\u方法]!='POST') { $sql=“选择 猫咪, 猫的名字, 类别描述 从…起 类别”; $result=mysql\u查询($sql); 如果(!$result) { echo“从数据库中选择时出错。请稍后再试。”; } 其他的 { if(mys
if($\u服务器['REQUEST\u方法]!='POST')
{
$sql=“选择
猫咪,
猫的名字,
类别描述
从…起
类别”;
$result=mysql\u查询($sql);
如果(!$result)
{
echo“从数据库中选择时出错。请稍后再试。”;
}
其他的
{
if(mysql_num_rows($result)==0)
{
//没有类别,因此无法发布主题
if($\u会话['userlevel']==1)
{
echo“您尚未创建类别”;
}
其他的
{
echo“在发布主题之前,必须等待管理员创建一些类别。”;
}
}
其他的
{
回声'
主题:
类别:';
回声';
while($row=mysql\u fetch\u assoc($result))
{
回显'.$row['cat_name'].';
}
回声';
回音信息:
';
}
}
}
其他的
{
//启动事务
$query=“开始工作;”;
$result=mysql\u query($query);
如果(!$result)
{
//该死!查询失败,退出
echo“创建主题时出错。请稍后再试。”;
}
其他的
{
//表单已发布,请保存它
//首先将主题插入topics表,然后将文章保存到posts表中
$sql=“插入到
主题(主题),
主题(日期),
主题(猫),
主题(作者)
值(“.mysql\u real\u escape\u string($\u POST['topic\u subject'])。”,
现在(),
“.mysql\u real\u escape\u字符串($\u POST['topic\u cat'])。”,
“$\u会话['userid']”
)";
$result=mysql\u查询($sql);
如果(!$result)
{
//出现问题,请显示错误
echo“插入数据时出错。请稍后重试。”.mysql_error();
$sql=“回滚;”;
$result=mysql\u查询($sql);
}
其他的
{
//第一个查询成功,现在开始第二个查询,发布查询
//检索新创建的主题的id,以便在posts查询中使用
$topicid=mysql\u insert\u id();
$sql=“插入到
帖子(post_内容,
发布日期,
后主题,
邮递员(邮递员)
价值观
(“.mysql\u real\u escape\u字符串($\u POST['POST\u content'])。”,
现在(),
“$topicid。”,
“$\u会话['userid']”
)";
$result=mysql\u查询($sql);
如果(!$result)
{
//出现问题,请显示错误
echo“插入帖子时出错。请稍后重试。”.mysql_error();
$sql=“回滚;”;
$result=mysql\u查询($sql);
}
其他的
{
$sql=“COMMIT;”;
$result=mysql\u查询($sql);
//经过大量工作,查询成功!
echo“您已成功创建”;
}
}
}
}
`您的sql查询在此中断
用“'”
value(“.mysql\u real\u escape\u string($\u POST['topic\u subject'])。”,
现在(),您没有在每个字符串周围添加引号:
$sql = "INSERT INTO
topics(topic_subject,
topic_date,
topic_cat,
topic_by)
VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
NOW(),
'" . mysql_real_escape_string($_POST['topic_cat']) . "',
'" . $_SESSION['userid'] . "'
)";
您必须在第二个mysql\u real\u escape\u字符串
周围添加单引号(如果会话['userid']
包含字符串,也必须在其周围添加单引号)。
你能把你的代码示例归结为一个例子吗?你在这里给我们的PHP有7个不同的对mysql_query()的调用-这是失败的?除了使用PDO或mysqli,我在这里要做的是在执行查询之前打印出来,echo$sql;$result=mysql_query($sql);然后问题就清楚了。您是否检查了第二个sql函数中是否定义了$\u会话[userid]?
VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
NOW(), <--- enclose with ."'"
$sql = "INSERT INTO
topics(topic_subject,
topic_date,
topic_cat,
topic_by)
VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
NOW(),
'" . mysql_real_escape_string($_POST['topic_cat']) . "',
'" . $_SESSION['userid'] . "'
)";
<pre>
<?php
$con = mysql_connect( 'localhost', 'root','' );
if (!$con)
{
die( 'Could not connect: ' . mysql_error() );
}
mysql_select_db( "stack",$con );
$_SESSION['userlevel']= 1;
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
$sql = "SELECT
cat_id,
cat_name,
cat_description
FROM
categories";
$result = mysql_query($sql);
if(!$result)
{
echo 'Error while selecting from database. Please try again later.';
}
else
{
if(mysql_num_rows($result) == 0)
{
//there are no categories, so a topic can't be posted
if($_SESSION['userlevel'] == 1)
{
echo 'You have not created categories yet.';
}
else
{
echo 'Before you can post a topic, you must wait for an admin to create some categories.';
}
}
else
{
echo '<form method="post" action="">
Subject: <input type="text" name="topic_subject" />
Category:';
echo '<select name="topic_cat">';
while($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
}
echo '</select>';
echo 'Message: <textarea name="post_content" /></textarea>
<input type="submit" value="Create topic" />
</form>';
}
}
}
else
{
//start the transaction
$query = "BEGIN WORK;";
$result = mysql_query($query);
if(!$result)
{
//Damn! the query failed, quit
echo 'An error occured while creating your topic. Please try again later.';
}
else
{
$user =1;
//the form has been posted, so save it
//insert the topic into the topics table first, then we'll save the post into the posts table
$sql = "INSERT INTO
topics(topic_subject,
topic_date,
topic_cat,
topic_by)
VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
NOW(),
" . mysql_real_escape_string($_POST['topic_cat']) . ", ". $user. "
)";
$result = mysql_query($sql);
if(!$result)
{
//something went wrong, display the error
echo 'An error occured while inserting your data. Please try again later.' . mysql_error();
$sql = "ROLLBACK;";
$result = mysql_query($sql);
}
else
{
//the first query worked, now start the second, posts query
//retrieve the id of the freshly created topic for usage in the posts query
$topicid = mysql_insert_id();
$sql = "INSERT INTO
posts(post_content,
post_date,
post_topic,
post_by)
VALUES
('" . mysql_real_escape_string($_POST['post_content']) . "',
NOW(),
" . $topicid . ",1
)";
$result = mysql_query($sql);
if(!$result)
{
//something went wrong, display the error
echo 'An error occured while inserting your post. Please try again later.' . mysql_error();
$sql = "ROLLBACK;";
$result = mysql_query($sql);
}
else
{
$sql = "COMMIT;";
$result = mysql_query($sql);
//after a lot of work, the query succeeded!
echo 'You have successfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';
}
}
}
}
?>
</pre>