MySQL 更新表中所有行中的一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16066202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:22:43  来源:igfitidea点击:

Updating one column in all rows in a table

mysqlrowsupdating

提问by Mohammad_Hosseini

I want to update one same column in all rows in one table can some one give me a hand how to update the table ? there is just one input field that should update all rows value . this code is not working and I know there is something wrong in index and array .

我想更新一个表中所有行中的同一列,有人可以帮我更新表吗?只有一个输入字段应该更新所有行值。这段代码不起作用,我知道 index 和 array 有问题。

<?php
    <form method="post" dir="rtl" name="maxcharfield" >                     
        <textarea onKeyDown="textCounter(this,'progressbar1',300)" 
          onKeyUp="textCounter(this,'progressbar1',300)" 
          onFocus="textCounter(this,'progressbar1',300)"  
            style="font-family:'B_yekan';" id="text" name="text" rows="0" cols="0" class="required"></textarea>
        <div class="cleaner h10"></div> 

        <div style="font-family:'B_yekan';" dir="rtl" id="progressbar1" class="progress" ></div>
        <script>textCounter(document.getElementById("maxcharfield"),"progressbar1",100)</script>    

        <input class="styled-button-8" style="margin-top:10px; float:right; margin-right:50px; font-size: 14px;  padding: 5px 14px;" type="submit" value="save" name="Submit"   />
        <input style="font-family:'B_yekan';" type="reset" value="reset" id="reset" name="reset" class="submit_btn float_l" />

    </form>

<?php
// for updating Check if button name "Submit" is active, do this 
if(isset($_POST['Submit']) && $_POST['Submit'] == 'save')
    {
        $sql1="UPDATE `".$tbl_name."` SET `board`='".$_REQUEST['text']."'  ";
                            $result1=mysql_query($sql1);
     }

    }   
?>

回答by David

You're over-complicating the solution. In order to update every record, the approach you're trying to take is:

您使解决方案过于复杂。为了更新每条记录,您尝试采用的方法是:

  1. Select all records.
  2. Get the ID for each record.
  3. Loop through the IDs.
  4. Update each record by that ID.
  1. 选择所有记录。
  2. 获取每条记录的 ID。
  3. 循环遍历 ID。
  4. 通过该 ID 更新每条记录。

The UPDATEsyntax has a much easier way to do this. You don't need to supply a WHEREclause to an UPDATEstatement. Without that clause, it will by default update every record in the table:

UPDATE语法有一个更简单的方法来做到这一点。您不需要为语句提供WHERE子句UPDATE。如果没有该子句,它将默认更新表中的每条记录:

UPDATE TableName SET `board`='value'

Also, please be aware that you have a SQL injectionvulnerability in your code. By using $_REQUEST['text']directly in your SQL query, you allow any user to send SQL code to your query. Your code then executes whatever they send you. This could allow them to corrupt or delete your data, even gain administrative access to your server.

另外,请注意您的代码中存在SQL 注入漏洞。通过 $_REQUEST['text']直接在您的 SQL 查询中使用,您允许任何用户向您的查询发送 SQL 代码。然后您的代码执行他们发送给您的任何内容。这可能允许他们破坏或删除您的数据,甚至获得对您服务器的管理访问权限。

For starters, please stop using mysql_*functions. PHP has deprecated those and they should no longer be used. There is a mysqli_replacementfor them. In addition to that, use the mysqli_real_escape_string()functionto sanitize your inputs before using them in a SQL query. And finally, use prepared statementsinstead of directly concatenating values into your SQL string.

对于初学者,请停止使用mysql_*功能。PHP 已弃用它们,不应再使用它们。他们有一个mysqli_替代品。除此之外,在 SQL 查询中使用输入之前,请使用mysqli_real_escape_string()函数对其进行清理。最后,使用准备好的语句而不是直接将值连接到 SQL 字符串中。