开发者

How do I update data to MySQL

开发者 https://www.devze.com 2023-01-28 11:40 出处:网络
I\'m getting blur to update data with multiple option_id @ option_namein same time. Current db option_idoption_nameoption_contentoption_status

I'm getting blur to update data with multiple option_id @ option_name in same time.

Current db

option_id   option_name        option_content           option_status
1           web_url            http://localhost.com     1
2           web_name           My Website               1
3           web_description    Welcome to my website    1
4           web_keywords       movies, power, ranger    1

php update data

$web_name    = $_POST['web_name'];
$web_url     = $_POST['web_url'];
$web_desc    = $_POST['web_desc'];
$web_keyword = $_POST['web_keyword'];

Update From DR anwser

$query = "UPDATE web_options SET option_content=
      '{$db->string_escape($web_name, true)}'
      WHERE option_name = 'web_name'";
$d开发者_Python百科b->rq($query);

$query = "UPDATE web_options SET option_content=
     '{$db->string_escape($web_url, true)}'
      WHERE option_name = 'web_url'";
$db->rq($query);

$query = "UPDATE web_options SET option_content=
     '{$db->string_escape($web_desc, true)}'
      WHERE option_name = 'web_desc'";
$db->rq($query);

$query = "UPDATE web_options SET option_content=
     '{$db->string_escape($web_keyword, true)}'
      WHERE option_name = 'web_keyword'";
$db->rq($query);

There have a way to make this update queries more simple?


You have to use multiple queries:

$query = "UPDATE web_options SET option_content=
          '{$db->string_escape($web_name, true)}'
          WHERE option_name = 'web_name'";
$db->rq($query);

$query = "UPDATE web_options SET option_content=
         '{$db->string_escape($web_url, true)}'
          WHERE option_name = 'web_url'";
$db->rq($query);

// And so on...

A slightly better approach is to use an associative array:

$data['web_name'] = $_POST['web_name'];
$data['web_url'] = $_POST['web_url'];
//and so on...
//Resist the temptation to use $_POST directly!

foreach ($data as $name => $value) {
    $query = "UPDATE web_options SET option_content=
             '{$db->string_escape($value, true)}'
              WHERE option_name = '$name'";
    $db->rq($query);
}


UPDATE queries take exactly the same WHERE conditions as normal queries.

UPDATE `web_options` SET ... WHERE `option_content` = 'My Website';

updates all rows where the option_content field equals "My Website".


Your table doesn't seem to have columns named web_name, web_url, web_desc or web_keyword. I suggest you review your basic SQL tutorial. A lot more carefully this time.

As for the solution... Ah, @deceze just posted it.


in order to use update , you should know what is option_id you want to target

the option_id must be primary key

and your code will be like this

$web_name    = $_POST['shop_name'];
$web_url     = $_POST['shop_url'];
$web_desc    = $_POST['shop_desc'];
$web_keyword = $_POST['shop_keyword'];
$option_id = $_POST['option_id'];

$query = 'UPDATE web_options SET 
        web_name="' . $db->string_escape($web_name, true) . '",
        web_url="' . $db->string_escape($web_url, true) . '",
        web_desc="' . $db->string_escape($web_desc, true) . '",
        web_keyword="' . $db->string_escape($web_keyword, true) . '"
        WHERE option_content ="' $bd->string_escape($option_id , true) . '" ';
$db->rq($query);


does blur kill and give error for bad SQL statements?

I like to use

or die("Cannot Update: ".mysql_error());

It normally helps pinpoint the issue.

0

精彩评论

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