Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I have this basic html form. My goal is to pass the values submitted to this form into a mysql table.

 <form action="?action=settings" method="post">
      <ul>
        <li>
           <label for="settingValue[1]">Setting 1:</label>
           <input type="text" name="settingValue[1]" id="setting1" required />
        </li>
        <li>
            <label for="settingValue[2]">Setting 2:</label><br>
            <input checked type="radio" name="settingValue[2]" value="1" />On<br>
            <input type="radio" name="settingValue[2]" value="0" />Off
         </li>
         <li>
             <input type="submit" name="saveChanges" value="Save Changes" />
         </li>
       </ul>
   </form>

My mysql table has only 3 columns (id, settingName, settingValue) So I want to update multiple records in my table using this form (In this case rows 1 and 2) by storing the value into the "settingValue" column.

After some research I found that what seems to be the best way to do this would be by posting an array (Which is what I attempted to do above), then use a foreach loop to to run through the array and update the table.

I guess my main problem is figuring out how to use the array. Because I need to UPDATE a record NOT create a new one. I also need to retain the ID so I can update the corresponding record.

After hours of googling my best guess was to do this:

if ( isset( $_POST['saveChanges'])) {
    foreach($_POST['settingValue'] as $key => $settingValue )  {
        $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
        $conn->exec("UPDATE settings SET settingValue=$settingValue WHERE id=$key");
        $conn = null;
    }

But it is yielding no results.

share|improve this question
1  
Try to echo your SQL query and copy-paste it to mysql console. You'll see errors if any –  Alexander Larikov Jul 30 '12 at 7:32
 
+ try to enclose value SET value in quotes (') or (") UPDATE settings SET settingValue='$settingValue' WHERE id='$key' –  Paul T. Rawkeen Jul 30 '12 at 7:43
1  
also try print_r($_POST['settingValue']) or print_r($_POST) and inspect the array. –  Wouter Huysentruit Jul 30 '12 at 7:43
 
Tip: label for= must point to the id of an element, not the name. –  Wouter Huysentruit Jul 30 '12 at 7:44
 
Thanks WouterH for the tip never knew that! I always had the name and the id the same so I never knew the difference... :/ but now i do :) –  aonline1 Jul 30 '12 at 7:48

2 Answers

up vote 0 down vote accepted

Keep $settingValue and $key in quotes:

$conn->exec("UPDATE settings SET settingValue='$settingValue' WHERE id='$key'");

Tip: Keep $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); outside the foreach loop. No need to repeat the connection on each loop

share|improve this answer
 
Perfect! I can't believe it was something so simple! Thanks asprin :) –  aonline1 Jul 30 '12 at 7:46
1  
Note that this is prone to sql injection. You might want to use bind parameters. –  Wouter Huysentruit Jul 30 '12 at 7:47
 
@aonline1 You're welcome –  asprin Jul 30 '12 at 7:48
 
i had it bound in the original but took it out to post here. thanks! –  aonline1 Jul 30 '12 at 7:49

Summing up all proposed solutions I would add.

Array keys as recordID is Ok to keep info about parameters for multiple records.

Looping is better to organize next way (to reuse existing connection, but not to reopen on each iteration):

$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); // opened connection
if ( isset( $_POST['saveChanges'])) // if smth. POSTed
    foreach($_POST['settingValue'] as $key => $settingValue ) // LOOPing thorugh each item
        $conn->exec("UPDATE settings SET settingValue='".$settingValue."' WHERE id='".$key."'"); // executing query using the previously opened connection

And try to enclose value SET value in quotes (') or (") UPDATE settings SET settingValue='$settingValue' WHERE id='$key'.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.