mysql表插入错误

时间:2014-05-26 05:08:43

标签: php

嗨,大家好,感谢时间。 我在这里得到一个mysql错误是我使用的PHP代码

if(isset($_POST["content_txt"]) && strlen($_POST["content_txt"])>0) 
{   //check $_POST["content_txt"] is not empty

    //sanitize post value, PHP filter FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH Strip tags, encode special characters.
    $contentToSave = filter_var($_POST["content_txt"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 

    // Insert sanitize string in record
    $insert_row = $mysqli->query("INSERT INTO f_add_course(content) VALUES('".$contentToSave."')");

    if($insert_row)
    {
         //Record was successfully inserted, respond result back to index page
          $my_id = $mysqli->insert_id; //Get ID of last inserted row from MySQL
          echo '<li id="item_'.$my_id.'">';
          echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$my_id.'">';
          echo '<img src="images/icon_del.gif" border="0" />';
          echo '</a></div>';
          echo $contentToSave.'</li>';
          $mysqli->close(); //close db connection

    }else{

        //header('HTTP/1.1 500 '.mysql_error()); //display sql errors.. must not output sql errors in live mode.
        header('HTTP/1.1 500 Looks like mysql error, could not insert record!');
        exit();
    }

并且没有插入数据

$insert_row = $mysqli->query("INSERT INTO f_add_course(content) VALUES('".$contentToSave."')");

if($insert_row)

它跳过if条件并且没有数据提前插入表hank

3 个答案:

答案 0 :(得分:0)

我认为您的查询中需要f_add_course(content)之间的空格。

答案 1 :(得分:0)

试试此代码

<?php
if(isset($_POST["content_txt"]) && strlen($_POST["content_txt"])>0){   
    //check $_POST["content_txt"] is not empty
    //sanitize post value, PHP filter FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH Strip tags, encode special characters.
    $contentToSave = filter_var($_POST["content_txt"],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 
    // Insert sanitize string in record
    $insert_row = $mysqli->query("INSERT INTO f_add_course(content) VALUES('$contentToSave')");
    if($insert_row){
        //Record was successfully inserted, respond result back to index page
        $my_id = $mysqli->insert_id; //Get ID of last inserted row from MySQL
        echo '<li id="item_'.$my_id.'">';
        echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$my_id.'">';
        echo '<img src="images/icon_del.gif" border="0" />';
        echo '</a></div>';
        echo $contentToSave.'</li>';
        $mysqli->close(); //close db connection
    } else {
        //header('HTTP/1.1 500 '.mysql_error()); //display sql errors.. must not output sql errors in live mode.
        header('HTTP/1.1 500 Looks like mysql error, could not insert record!');
        exit();
    }
}
?>

答案 2 :(得分:0)

请在此处查看示例查询:

http://www.w3schools.com/php/func_mysqli_query.asp

似乎您需要将查询执行更改为:

$insert_row = $mysqli->query("INSERT INTO f_add_course (content) 
VALUES("'.$contentToSave.'")");