mysql和php的整数值不正确
我收到以下错误消息: 插入数据时出错。请稍后重试。(第98行)第1行“主题”列的整数值“”不正确 这是我的代码:mysql和php的整数值不正确,php,mysql,Php,Mysql,我收到以下错误消息: 插入数据时出错。请稍后重试。(第98行)第1行“主题”列的整数值“”不正确 这是我的代码: <?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
<?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';
?>
问题不在于数据周围的引号,而在于$\u SESSION['user\u id']
可能返回为空白或空。MySQL会将“”解释为空字符串而不是int。因此请删除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
最新答复:
看起来您在运行MySQL时启用了SQL严格模式
要禁用严格模式,请在编辑器中打开my.cnf并删除/注释掉以下行:
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
然后重新启动数据库服务器。同时检查您的状态!结果
if(!result)
{
}
else
{
}
else
{
}
2.否则的话就错了
像这样做if(){}else if(){}else{}
尝试回显$\u SESSION['user\u id']的val,并检查它是否为int。我已经尝试过了,我收到了以下错误消息:插入数据时发生错误。请稍后再试。(第98行)您的SQL语法有错误;查看与MySQL服务器版本相对应的手册,以了解第9行中使用的正确语法(接近“)”。看起来最接近my.cnf的页面是“my default.ini”。我注释掉了文件结尾的一行“#sql mode=NO_ENGINE_SUBSTITUTION_TRAN_TABLES。我仍然在第9行收到错误消息“检查MySql服务器版本以获得正确的语法以使用near”)。请尝试签入/etc/my.cnf