开发者

How to update sql like this?

开发者 https://www.devze.com 2022-12-16 13:22 出处:网络
Hey,i\'ve got a problem, i want to execute this sql update, but it doesn\'t work. Could anyone help me? Thanks!

Hey,i've got a problem, i want to execute this sql update, but it doesn't work. Could anyone help me? Thanks!

The code:

$temp = $_GET['ul'];

foreach ($temp as $key=> $value)
{
    $str=array();
    $arr=explode(',',$value); 
    $str =array($key=>$arr);

开发者_StackOverflow中文版    for($i=0;$i<count($str[$key]);$i++)
    {
        $tripID='2';                    
        $sql = "UPDATE places_trips 
                SET orderNo='$i', ColumnNo='$key' 
                WHERE place_id=" . $str[$key][$i] . "
                AND trip_id=" . $tripID;
        mysql_query($sql);
        }
    } 
}

I want to set the $tripID = 2, but actually, $tripID=2222. So how to make the $tripID=2 all the time?


Your query doesn't change trip_id. It tries to change orderNo and ColumnNo for rows where trip_id is 2. If I understand you correctly, you should put it in the first part of your query:

"UPDATE places_trips SET orderNo = '$i', ColumnNo = '$key', trip_id = $tripID WHERE place_id = ".$str[$key][$i];

That being said, read about SQL injections. You need it because your current code is terribly dangerous.


I hope this is what you're after:

UPDATE places_trips SET
    orderNo = $i,
    ColumnNo = $key
WHERE place_id = $str[$key][$i]
AND trip_id RLIKE '^2+$';

Should update all rows where trip_id contains only 2s.

Also search on StackOverflow for SQL Injections, your code is vulnerable to them.

0

精彩评论

暂无评论...
验证码 换一张
取 消