插入语句在PHP中失败

时间:2012-01-27 15:22:52

标签: php mysql

if(isset($_POST['title']) && isset($_POST['tag_h1']) && isset($_POST['tag_h2']) && isset($_POST['tag_metadata']) && isset($_POST['title']) && isset($_POST['postContent']))
{    
    $tag_h1=$_POST['tag_h1'];
    $tag_h2=$_POST['tag_h2'];
    $tag_metadata=$_POST['tag_metadata'];       
    $content=$_POST['postContent'];
    $title=$_POST['title'];
    $isTop=isset($_POST['isTop'])?1:0;
    $query = "INSERT INTO topic (tag_h1,tag_h2,tag_metadata,content,title,is_top) VALUES ('".$tag_h1."', '".$tag_h2."', '".$tag_metadata."', '".$content."', '".$title."'".$isTop."')";
    mysql_query($query) or die(mysql_error());
}

以上陈述失败。抛出异常。我看不到任何因素,导致Chrome冻结..其他浏览器变空了......我看不出任何错误!

有谁知道为什么我的插入语句错了?

6 个答案:

答案 0 :(得分:2)

您指定了6个要插入的列,只有5个值:

$query = "INSERT INTO topic (tag_h1,tag_h2,tag_metadata,content,title,is_top) 
VALUES (
     '".$tag_h1."', '"
     .$tag_h2."', '"
     .$tag_metadata."', '"
     .$content."', '"
     .$title."'".$isTop."')";

也许你错过了一个逗号?

please make sure you have a read about SQL injection

答案 1 :(得分:2)

更改此行:

$query = "INSERT INTO topic (tag_h1,tag_h2,tag_metadata,content,title,is_top) VALUES ('".$tag_h1."', '".$tag_h2."', '".$tag_metadata."', '".$content."', '".$title."'".$isTop."')";

对此:

$query = "INSERT INTO topic
            (tag_h1,tag_h2,tag_metadata,content,title,is_top)
          VALUES
            ('".mysql_real_escape_string($tag_h1)."', '".mysql_real_escape_string($tag_h2)."', '".mysql_real_escape_string($tag_metadata)."', '".mysql_real_escape_string($content)."', '".mysql_real_escape_string($title)."', '".mysql_real_escape_string($isTop)."')";

您忘记了, ',但您没有忘记输入。

答案 2 :(得分:0)

缺少:“,”和缺少“'”

$query = "INSERT INTO topic (tag_h1,tag_h2,tag_metadata,content,title,is_top) VALUES ('".$tag_h1."', '".$tag_h2."', '".$tag_metadata."', '".$content."', '".$title."'".$isTop."')";

'".$title."'".$isTop."'之间

应该是:

$query = "INSERT INTO topic (tag_h1,tag_h2,tag_metadata,content,title,is_top) VALUES ('".$tag_h1."', '".$tag_h2."', '".$tag_metadata."', '".$content."', '".$title."', '".$isTop."')";

答案 3 :(得分:0)

我不能说冻结,但是:

'".$title."'".$isTop."' 

应该是

'".$title."', '".$isTop."'

答案 4 :(得分:0)

//checking that they're all set won't do anything, because, unless it's a checkbox,
//it's always set if it's in the form
//So you should check if the submit button has been set (form submitted)
//And that the inputs are !empty()
//Also, for future reference, isset() can take multiple arguments and will return true if all arguments are set or false if one isn't.  empty() only takes one argument though.
if( (isset($_POST['submitbutton']) || isset($_POST['submitbutton_x'])) //checking for the submitname_x is a fix for image submit buttons on IE
    && !empty($_POST['title']) 
    && !empty($_POST['tag_h1']) 
    && !empty($_POST['tag_h2']) 
    && !empty($_POST['tag_metadata']) 
    && !empty($_POST['title']) 
    && !empty($_POST['postContent']) )
{ 

    $tag_h1 = mysql_real_escape_string($_POST['tag_h1']);  //Always escape directly used input!
    $tag_h2 = mysql_real_escape_string($_POST['tag_h2']);
    $tag_metadata = mysql_real_escape_string($_POST['tag_metadata']);       
    $content = mysql_real_escape_string($_POST['postContent']);
    $title = mysql_real_escape_string($_POST['title']);

    $isTop = isset($_POST['isTop'])?1:0;


    $sql = 'INSERT INTO topic (tag_h1,tag_h2,tag_metadata,content,title,is_top)';
    $query = sprintf("%s VALUES ('%s', '%s', '%s', '%s', '%s', %d)",$sql,$tag_h1,$tag_h2,$tag_metadata,$content,$title,$isTop);
    //I assume is_top is an int or tinyint or whatever field 
    //and as such should not have single quotes around it.

    mysql_query($query) or die(mysql_error());

}

答案 5 :(得分:0)

最后两个值之间缺少:逗号和单引号。:

您的查询应更改为:

$query = "INSERT INTO topic (tag_h1,tag_h2,tag_metadata,content,title,is_top) VALUES ('".$tag_h1."', '".$tag_h2."', '".$tag_metadata."', '".$content."', '".$title."', '".$isTop."')";