mysql_query更新不起作用,尽管我没有收到任何mysql错误


mysql_query update is not working though i am not getting any mysql errors

我刚刚为mysql数据库编写了一个更新查询。问题不在于它既没有显示任何mysql错误,也没有更新行。my_sql_rows总是输出false,这意味着我的表中没有更新任何内容代码如下

if(isset($_POST['update_subject']))
{  
    $id  = $_POST['id'];
    $menu_name  = $_POST['menu_name'];
    $visible  = $_POST['visible'];
    $position  = $_POST['position'];
    $content  = $_POST['content'];
    mysql_query("UPDATE subjects SET menu_name ='$menu_name', visible = $visible, position = $position,
                    content =    '$content'  WHERE id =  $id ")
                     or die("updation of '$menu_name' in table subjects failed due to : " . mysql_error ());
    if(mysql_affected_rows == 1 ) {
        echo "Updation of '$menu_name' in table subjects is done successfully !!! ";
    } else {
        echo "something went wrong. updation process is halted.";
    }   
}

它总是告诉我"出了问题。更新过程已停止"我知道mysql命令已经过时了,但我仍然想学习。thanx!

Ita,因为您使用了不将括号与if语句合并的禁忌做法,此外还需要在函数后面加括号

if(mysql_affected_rows() == 1 ){
    echo "Updation of '$menu_name' in table subjects is done successfully !!! ";
}
else{
    echo "something went wrong. updation process is halted.";
}

mysql_affected_rows是函数,因此需要在它后面加括号,如:

$result = mysql_query($yourquery);
if(mysql_affected_rows($result) === 1) {
  // logic
}

现在开始抨击垃圾邮件。。。

ext/mysql不应用于新项目:

从PHP 5.5.0开始,该扩展已被弃用,不建议用于编写新代码,因为它将来会被删除。相反,应该使用mysqli或PDO_MySQL扩展。请参阅MySQL API概述,以获取选择MySQL API时的进一步帮助。

相反,您应该将mysqliPDO与mysql驱动程序一起使用。

  1. 如果列值是字符串,则UPDATE查询需要在列值周围加上'引号。因此,请检查查询。

    mysql_query("UPDATE subjects SET menu_name ='$menu_name', visible = '$visible', position = '$position', content = '$content'  WHERE id =  '$id' ")
    
  2. mysql_affected_rows是一个函数,所以它必须是

    if(mysql_affected_rows() == 1 )
    

注意:不赞成使用Mysql_*扩展。如果可能,请使用MysqliPDO

如果你想用PDO+更干净的代码来实现这一点,请尝试:

<?php
try {
    $db = new PDO("mysql:host=127.0.0.1;dbname=databasename", 'dbuser', 'dbpassword');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch (Exception $e){
    die('Cannot connect to mySQL server.');
}
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['update_subject'])){
    if(!empty($_POST['id'])){
        $sql = "UPDATE subjects SET menu_name = :menu_mame,
                                    visible   = :visible,
                                    position  = :position,
                                    content   = :content
                                WHERE id = :id";
        $update = array(':menu_name'=>$_POST['menu_name'],
                        ':visible'=>$_POST['visible'],
                        ':position'=>$_POST['position'],
                        ':content'=>$_POST['content'],
                        ':id'=>$_POST['id']);
        $query = $db->prepare($sql);
        $query->execute($update);
        $affected = $query->rowCount();
        if($affected > 0 ){
            echo htmlspecialchars($_POST['menu_name'])." updated successfully !!!";
        }else{
            echo "Something went wrong or no rows were found tobe updated";
        }
    }
}
?>