我收到以下错误消息:
插入数据时出错。请稍后再试。(第98行)不正确的整数值:''第1行第'topic_by'栏
这是我的代码:
<?php
//create_cat.php
include 'connect.php';
include 'header.php';
echo '<h2>Create a topic<h2>';
/*
if(isset($_SESSION['signed_in']) == false)
{
//the user is not signed in
echo 'Sorry, you have to be <a href="signin.php">signed in</a> to create a topic.';
}
else
{
*/
//the user is signed in
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
//the from hasn't been posted yet, display it
//retrieve the categories from the database for use in the dropdown
$sql = "SELECT
cat_id,
cat_name,
cat_description
FROM
categories";
$result = mysql_query($sql);
if(!$result)
{
//the query failed, uh-oh :-(
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['user_level']==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="">
Topic By: <input type="text" name="topic_by" />
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
{
//the form has been posted, so save it
//insert 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']) . "',
'" . $_SESSION['user_id'] . "'
)";
$result = mysql_query($sql);
if(!$result)
{
//something went wrong, display the error
echo 'An error occured while inserting your data. Please try again later.(line 98)'.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.",
".$_SESSION['user_id']."
)";
$result = mysql_query($sql);
if(!$result)
{
//something went wrong, display the error
echo 'An error occured while inserting your post. Please try again later.(line 124)'.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>.';
}
}
}
}
//}
include 'footer.php';
?>
答案 0 :(得分:0)
问题不是数据的引用,而是$_SESSION['user_id']
可能会空白或空白的事实。 MySQL将''解释为空的STRING而不是int.So删除qoutes。
$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['user_id'] . " )"; //Remove the qoutes
更新回答:
看起来你在启用SQL严格模式的情况下运行MySQL。
要禁用严格模式,请在编辑器中打开my.cnf并删除/注释掉以下行:
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
然后重新启动数据库服务器。
答案 1 :(得分:0)
检查你的病情!结果
if(!result)
{
}
else
{
}
else
{
}
2其他陈述是错误的
这样做if () { } else if () { } else { }