How to update data to MySQL

I get blur to update data with multiple option_id@ option_nameat the 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 DR anwser

$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);

$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);

Is there a way to make these update requests simpler?

+3
source share
5 answers

You need to use a few 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);
}
+3
source

UPDATEqueries are executed under the same conditions WHEREas regular queries.

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

updates all lines where the field option_contentis "My site".

+3
source

web_name, web_url, web_desc web_keyword. SQL. .

... , @deceze .

0
source

in order to use the update, you must know exactly what option_id you want to configure for

option_id must be the 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);
0
source

does blur and give error for bad SQL statements?

I like to use

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

This usually helps to identify the problem.

0
source

Source: https://habr.com/ru/post/1777415/


All Articles