dtrn74832 2014-09-30 08:11
浏览 60

如果Null不起作用,则为SQL Update文本值

I am trying to make it so that a specific value can only be set once, by making it so that i can only be updated or set if it is equal to null. For some reason, the PHP still echoes success even when the value is equal to something like: "Steve". What am I doing wrong?

PHP:

<?php
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("???????",$dbhandle) 
  or die("Could not select examples");

if(empty($_POST['OrderID']) || empty($_POST['UserName'])){
            echo 'Failed. Fill out all fields.';
        }
else{
        $sql = mysql_query("SELECT * FROM orders WHERE order_id = '".$_POST['OrderID']."' AND LENGTH(accepter_name)= 0");
        $row = mysql_fetch_row($sql); // get the single row.
        echo $row['accepter_name']; // display the value.    

        if($row['accepter_name']==''){ 
            $sql = mysql_query("UPDATE orders SET accepter_name= '".$_POST['UserName']."' WHERE order_id='".$_POST['OrderID']."'");
                    echo "Success";   
        }
        else{
        echo "Failed";
         }
}
mysql_close($dbhandle);
?>

When i check on the database, the accepter name value is changed.

  • 写回答

2条回答 默认 最新

  • dony113407 2014-09-30 08:20
    关注

    You can use if( mysql_affected_rows($sql) == 1 ) instead of if($sql) because the sql is executed and it's returns no rows affected. So, if you check for affected rows it should return 1 when your sql updated at least one row in your table

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器