Forum Moderators: coopster

Message Too Old, No Replies

PHP MySQL Update Query

         

LinusIT

3:27 pm on Feb 16, 2011 (gmt 0)

10+ Year Member



Hi

I'm having great difficultly getting my head around this and I'm sure it's obvious where I'm going wrong.

I have a table that populates it's data from a database, this works fine. The problem I'm having is updating the records.

Code to populate table:

<? while($row=mysql_fetch_array($result)) { ?>
<tr>
<td><? echo $row['model_name'] ?></td>
<td><input type="hidden" name="model_id" value="<?=$row['model_id']?>" /><input type="text" name="model_weight" value="<?=$row['model_weight']?>" /></td>
</tr>
<? } ?>


This produces:

td>1 Series</td>
<td><input type="hidden" name="model_id" value="39" /><input type="text" name="model_weight" value="0" /></td>
</tr>
<tr>
<td>3 Series</td>
<td><input type="hidden" name="model_id" value="40" /><input type="text" name="model_weight" value="1435" /></td>
</tr>
<tr>
<td>5 Series</td>
<td><input type="hidden" name="model_id" value="41" /><input type="text" name="model_weight" value="1652" /></td>
</tr>


Code to update database:

$query="UPDATE model SET model_weight='" . $_REQUEST['model_weight'] . "' WHERE model_id='" . $_REQUEST['model_id'] . "'";


I'm not getting any errors when updating, it just isn't updating the records. The model_id is unique for each record.

Hope someone can help :)

Demaestro

3:34 pm on Feb 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My first guess would be that the where clause isn't getting met.

Add a line that will echo $query to the screen. Then check that your variables come through properly.

Then copy the WHERE part of the sql and put it at the end of

select * from model YOUR_WHERE_HERE

and see if that returns a row. If it does then there is something else at play here.

LinusIT

4:04 pm on Feb 16, 2011 (gmt 0)

10+ Year Member



I've added a line to echo the $query and this is what it produces:

UPDATE model SET model_weight='' WHERE model_id=''


This looks fine to me, prehaps I'm not setting the variables properly.

I need it to update model_weight based on the model_id. I'm going to kick myself when it's resolved, I just can't see where I'm going wrong.

coopster

4:23 pm on Feb 16, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



First, try using the actual superglobal specific to the form action. So, rather than using $_REQUEST, try $_POST, which is more often than not the method type used in form processing.

Second, NEVER use raw user-supplied input without edit checking. And certainly not without safely escaping [php.net] the values before using them in your query statement.

LinusIT

4:33 pm on Feb 16, 2011 (gmt 0)

10+ Year Member



I think the problem lies with the html part of it.

<td><input type="hidden" name="model_id" value="39" /><input type="text" name="model_weight" value="0" /></td>


That's two seperate inputs, there's nothing linking them together other than their within the same td.

So within that one td it's saying the model_id is x and the model_weight is y. The query then updates model_weight using y based on the model_id of x.

I can see the solution but in practice it doesn't work so maybe it's not the solution.

If I could do the following then maybe it could work.

<input type="text" id="<?=$row['model_id']?>" name="model_weight" value="<?=$row['model_weight']?>" />