2013-04-08 50 views
0

我在创建一个CMS,其中当您添加新页面时,display_order将根据已存在的行数自动获取下一个最高数字。这是我目前有:INSERT mysql_num_rows作为变量

<?php 

if(isset($_POST['updateContent'])){ 

    require ("connection.php"); 
    $sql = "SELECT * FROM pages"; 
    $result = $conn->query($sql) or die(mysqli_error()); 

    $content = $_POST['content']; 
    $title = $_POST['title']; 
    $id = $_POST['id']; 

    $order = mysqli_num_rows($result); 

    if (empty($id)){ 
     /** ADD NEW SLIDE*/ 
     $sql = "INSERT INTO pages (title, content, display_order, visible) VALUES ('".$title."', '".$content.", '".$order.", 0)"; 
    }else{ 
     /** UPDATE SLIDE*/ 
     $sql = "UPDATE pages SET content = '".$content."', title = '".$title."' WHERE id = '".$id."'"; 
    } 

    if ($result){ 
     header("Location: admin.php"); 
    } 
} 

?> 

这段代码是做的是,我使用在一个名为页edit.php和确定HTML表单,如果它是新的页面或者干脆说正在更新页面。我得到的错误是没有任何东西发布到数据库中。如果我删除$sql,$result$order行..脚本可以正常工作,但display_order变量不会设置为下一个最高的数字。

+0

//评论标题行。然后张贴你看到的 – 2013-04-08 16:13:51

回答

1

。在你的查询时出现错误:

INSERT INTO pages (title, content, display_order, visible) 
VALUES ('".$title."', '".$content.", '".$order.", 0)"; 
            ^-- here 

应该是:

INSERT INTO pages (title, content, display_order, visible) 
VALUES ('".$title."', '".$content."', ".$order.", 0)"; 
            ^-- quote goes here 

此外,使用mysqli不会奇迹般地保护你的SQL插入。转义数据输入!