0

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

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 命令已经过时但我仍然想学习。谢谢!

4

4 回答 4

3

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.";
}
于 2013-02-19T19:42:32.813 回答
2

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 概述以获得更多帮助。

相反,您应该使用mysqliorPDO与 mysql 驱动程序。

于 2013-02-19T19:42:06.920 回答
1

几件事,

  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_* 扩展。如果可能,使用Mysqli或。PDO

于 2013-02-19T19:43:57.430 回答
1

如果您想使用 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";
        }
    }
}
?>
于 2013-02-19T19:53:12.493 回答