简体   繁体   English

PHP变量值不更新MySQL表字段?

[英]PHP variable value to NOT update MySQL table field?

I'm updating a MySQL table with posted PHP data. 我正在用发布的PHP数据更新MySQL表。 I first gather the posted data, and put them in appropriate variables. 我首先收集发布的数据,并将其放入适当的变量中。 Together with the necessary if/else checks. 连同必要的if / else检查。

Then, I only have to write my query once. 然后,我只需要编写一次查询。

But now I have an if/else to check wether to update a specific field or not. 但是现在我有一个if / else来检查是否更新特定字段。 How can I store a "do-not-update" value inside the corresponding variable? 如何在相应变量中存储“请勿更新”值

Because otherwise I have to put an if/else check around the whole query, just for one field. 因为否则,我必须对整个查询进行if / else检查,仅针对一个字段。

I just want to be as efficient as possible. 我只是想提高效率。 :) :)

My query is as follows: 我的查询如下:

$updateTable = mysql_query("UPDATE myTable SET field1 = '$field1', field2 = '$field2'");

wherever you are get $_POST into variables, do this, 无论您将$ _POST放入变量中,都可以执行此操作,

 if( $field2 === 'xyz' ) { //if value is 'xyz', do not update
     $sql = ''; 
  } else 
      $sql = ", field2 = '$field2'";

Then in the query, 然后在查询中

   $updateTable = mysql_query("UPDATE myTable SET field1 = '$field1' $sql");

Edit: if using 1/0 (true or false), 编辑:如果使用1/0(对或错),

   if( $field2 == true ) { //if value is true, do not update
     $sql = ''; 
  } else 
      $sql = ", field2 = '$field2'";

You will need to build up your query, storing it in a PHP string, for example: 您将需要构建查询,并将其存储在PHP字符串中,例如:

$sql = "UPDATE `table` SET ";
if ($_POST['foo']!=='') {
  $sql .= " `foo`='".mysql_real_escape_string($_POST['foo'])."',";
}
if ($_POST['bar']!=='') {
  $sql .= " `bar`='".mysql_real_escape_string($_POST['bar'])."',";
}
$sql = rtrim($sql,',');
$sql .= " WHERE `id`='".mysql_real_escape_string($_POST['id'])."'"

Then execute your string as the query. 然后执行您的字符串作为查询。

If you are asking whether the field should be updated, you can do one of two things: 如果您询问是否应更新该字段,则可以执行以下两项操作之一:

1) Specify a criteria that ensures field1 and field2 are only updated if the rows match the criteria. 1)指定一个条件,以确保仅在行与条件匹配时才更新field1field2 If the criteria does not match, the record will not be updated. 如果条件不匹配,则记录将不会更新。 This is the most common way. 这是最常见的方式。

UPDATE myTable ...
WHERE criteria1 = 1 AND criteria2 = 'Red'

2) Run a query before the UPDATE to see whether to perform an update. 2)在UPDATE之前运行查询以查看是否执行更新。

I'm not exactly sure what you are asking for, but perhaps this answers your question: 我不确定您要的是什么,但这也许可以回答您的问题:

$updateTable = mysql_query("
UPDATE myTable SET
    field1 = IF('$field1'>'','$field1', field1),
    field2 = IF('$field2'>'','$field2', field2)
");

Of course, you are opening yourself up to SQL injection with the code, as written. 当然,您可以使用编写的代码进行SQL注入。

Lets assume you have gathered the fields to update in an array $fields like this : 假设您已经收集了要在数组$fields更新的$fields如下所示:

array (
'filed1' => 'value' ,
'field2' => ''value
)

Now you need to generate the query, you can do this by looping in the array: 现在您需要生成查询,可以通过在数组中循环来执行此操作:

    $sql = "UPDATE mytable ";
$sql .= $fields ? "SET " : "" ;
    foreach ($fields as $key=>$value) {
    $sql.= $value ? "$key = '$value' , " : '' ;
    }
    //you need to omit the trailing ','
$sql[strlen($sql) -1 ] = "";

Tips : Do sanitize all user input using mysqli_real_escape_string or something better than that. 提示:请使用mysqli_real_escape_string或其他更好的方法清除所有用户输入。

Happy coding :) 快乐的编码:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM