Updating one column in all rows in a table
Asked Answered
M

2

34

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 .

<?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);
     }

    }   
?>
Mielke answered 17/4, 2013 at 17:18 Comment(0)
E
78

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.

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

UPDATE TableName SET `board`='value'

Also, please be aware that you have a SQL injection vulnerability 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.

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

Epicrisis answered 17/4, 2013 at 17:21 Comment(6)
@mohamad: Can you update the original question with the new code? Any number of things could potentially be wrong.Epicrisis
accually some of those part for injection that you've mentioned in this code are not important this is a test code . for the record I'm not pro in php so I need some professionals helpMielke
@mohamad: Does the execution reach the SQL call? That is, does mysql_query($sql1) actually get executed? If so, what is the runtime value of $sql1 when that happens? If you take that runtime value and execute it manually against your database, what happens?Epicrisis
I have fixed the problem it was the submit problem . for and unknown reason it wasn't working I changed the submit update code to the above and it's working .Mielke
@Epicrisis hey David, is there a way, for each row within a column send it as parameter of python function and with the return value, update different column?Intellectualism
UPDATE Table_Name SET "column_name" ='value';Dodger
Q
3

Above gave Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. error.

The one query to update all values in row, under single column.

UPDATE TableName SET `board`='value' WHERE `id` > 1;

Just append a WHERE clause, now if this is in any CMS, you will get the variant of this id, it could be entity_id or, post_id.

Quack answered 14/7, 2021 at 9:51 Comment(1)
Nice. Good call.Szymanowski

© 2022 - 2024 — McMap. All rights reserved.