** update in an array brick wall **

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

** update in an array brick wall **

Post by glennn3 »

i'm full done with this site except for this one brick wall i've hit - i need to be able to update the one field "status" for each of the rows with the same "recID" field *separately*:

Code: Select all

<?
$grab_usertitles = "SELECT * FROM titles WHERE recID = '$id'";
$result = @mysql_query($grab_usertitles);

sql_query("$result", "1", "$errors&#1111;03]");

while ($row = mysql_fetch_array($result))
&#123;
$tid = $row&#1111;"id"];
$title = $row&#1111;"title"];
$status = $row&#1111;"status"];

echo "<tr><td width="150">$tid - $title  </td><td width="400">
  <select name=status&#1111;". $i ."]"><option value="$status">$status</option>
<option value="Received">Received</option>
<option value="Under Review">Under Review</option>
<option value="Approved">Approved</option>
<option value="Declined">Declined</option>
</select></td></tr>";
&#125;
?>
i was given this:

Code: Select all

$status = $_POST&#1111;'status']; // if your form's method is POST
$updates = array();
foreach($status as $key=>$value)
&#123;
    $updates&#1111;] = "UPDATE titles SET status = '". $value ."' WHERE id = $key";
&#125;

// Then finally loop through the $updates array to execute the updates:

for($i=0;$i<count($updates);++$i)
&#123;
   $success = mysql_query($updates&#1111;$i]);
&#125;
but it is only updating status field to random chioces of the four options, not the option selected...

someone please help? this has brought the finishing of the site to a standstill...

thanks very much,
glennn
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

With similiar code, I did something like this.

Code: Select all

foreach ($_POST['status'] as $k => $v) {
        echo "update titles set status = '$v' where id = '$k'";
    }
Result:

Code: Select all

update titles set status = 'Received' where id = '1'
update titles set status = 'Declined' where id = '2'
update titles set status = 'Under Review' where id = '3'
update titles set status = 'Approved' where id = '4'
You are not writing your code entirely correct. You are missing the \" in the following statement (might be a forum bug...):

Code: Select all

&lt;select name=status&#1111;". $i ."]"&gt;
You could also do something similiar to the following to ease up on reading the code:

Code: Select all

echo '
        <select name="status['.$i.']">
            <option value="Received">Received</option>
            <option value="Under Review">Under Review</option>
            <option value="Approved">Approved</option>
            <option value="Declined">Declined</option>
        </select>';
...but mileage may vary. (Nay might want to mention heredoc also? ;))

Another question is, what is $i? Shouldn't you use $tid ther instead to bind an title-id to it?

Edited:
the forum wont show my correct code for some reason... Will try to edit it in an readable way...
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

this worked:

Post by glennn3 »

aside from this site's inability to reproduce our syntax,

this worked:

Code: Select all

<select name="status&#1111;'.$tid.']"> 
            <option value="Received">Received</option> 
            <option value="Under Review">Under Review</option> 
            <option value="Approved">Approved</option> 
            <option value="Declined">Declined</option> 
        </select>';
with this:

Code: Select all

$status = $_POST&#1111;'status']; // if your form's method is POST 
$updates = array(); 
foreach($status as $key=>$value) 
&#123; 
    $updates&#1111;] = "UPDATE titles SET status = '". $value ."' WHERE id = $key"; 
&#125; 

// Then finally loop through the $updates array to execute the updates: 

for($i=0;$i<count($updates);++$i) 
&#123; 
   $success = mysql_query($updates&#1111;$i]); 
&#125;
thanks very much...

glenn
Post Reply