【问题标题】:PHP cannot insert dataPHP无法插入数据
【发布时间】:2015-06-30 15:56:49
【问题描述】:

当我使用以下代码时,我无法插入数据。它显示以下错误消息:

[插入数据时出错。请稍后再试。您的 SQL 语法有错误:请查看与您的 MySQL 服务器版本相对应的手册,以在第 10 行的 ')' 附近使用正确的语法]

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
    {

        //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']) . ",
                           " . $_SESSION['userid'] . "
                           )";

        $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 . ",
                              " . $_SESSION['userid'] . "
                        )";
            $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>.';
            }
        }
    }
}

`

【问题讨论】:

  • 您能否将您的代码示例简化为一个示例?您在这里给我们的 PHP 对 mysql_query() 有 7 次不同的调用 - 失败了?
  • 除了使用 PDO 或 mysqli 之外,我在这里要做的就是在执行查询之前将查询打印出来, echo $sql; $result = mysql_query($sql);那么问题出在哪里就很清楚了。
  • 你检查 $_SESSION[userid] 在你的第二个 sql 函数中定义了吗?

标签: php mysql


【解决方案1】:

您错过了在每个字符串周围添加引号:

$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_real_escape_string 周围添加单引号。 (如果它包含一个字符串,也可以在你的 $_SESSION['userid'] 周围。)

【讨论】:

    【解决方案2】:
    <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>
    

    我正在使用相同的脚本并且它正在工作。请检查您的会话是否创建

    【讨论】:

      【解决方案3】:

      您的 sql 查询在这里中断enclose 您的字符串和日期值与"'"

       VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',
                             NOW(), <--- enclose with ."'"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-30
        • 2016-09-24
        • 1970-01-01
        相关资源
        最近更新 更多