| | |
MySQL update not working
![]() |
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Solved Threads: 0
Hey, i'm new here and i need help.
I'm trying to write an update query so i can update mulitple items in a database, but it aint working..
Ive used the mysql_error() thingy, but it still does not display any errors. All it does is nothing, the web page doesnt update, and neither does the database in MySQL.
Is the syntax wrong or what?
My code:
Cheers.
I'm trying to write an update query so i can update mulitple items in a database, but it aint working..
Ive used the mysql_error() thingy, but it still does not display any errors. All it does is nothing, the web page doesnt update, and neither does the database in MySQL.
Is the syntax wrong or what?
My code:
PHP Syntax (Toggle Plain Text)
<h1>Update Items</h1> <p>Update items below.</p> <?php $db_host = 'X; $db_user = 'X'; $db_pwd = 'X'; $database = 'X'; $table = 'pricelist'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db(X)) die("Can't select database"); // sending query $sql="SELECT * FROM $table"; $result=mysql_query($sql); // Count table rows $count=mysql_num_rows($result); ?> <table width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="post" action=""> <tr> <td> <table width="500" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>Id</strong></td> <td align="center"><strong>Item</strong></td> <td align="center"><strong>Price</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td> <td align="center"><input name="item[]" type="text" id="item" value="<? echo $rows['item']; ?>"></td> <td align="center"><input name="price[]" type="text" id="price" value="<? echo $rows['price']; ?>"></td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><input type="submit" name="Update" value="Update"></td> </tr> </table> </td> </tr> </form> </table> <?php //define each variable $item= $_POST['item'][$i]; $price = $_POST['price'][$i]; // Check if button name "Submit" is active, do this if($Update){ for($i=0;$i<$count;$i++){ $sql1="UPDATE $table SET item='$item', price='$price' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); mysql_error(); } } if($result1){ header("location:pricelistdata.php"); } mysql_close(); ?>
Cheers.
Last edited by mortalex; Oct 31st, 2007 at 7:43 am. Reason: Editing out Passwords and stuff
change your tags.
<?php //code here ?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="item[]" type="text" id="item" value="<? echo $rows['item']; ?>"></td>
<td align="center"><input name="price[]" type="text" id="price" value="<? echo $rows['price']; ?>"></td>
</tr>
<?php //code here ?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="item[]" type="text" id="item" value="<? echo $rows['item']; ?>"></td>
<td align="center"><input name="price[]" type="text" id="price" value="<? echo $rows['price']; ?>"></td>
</tr>
Do a favour, leave me alone
•
•
Join Date: Oct 2007
Posts: 30
Reputation:
Solved Threads: 0
Why would that work, i've enabled short tags in the php.cfg file, so why would changing them let it work.
I'll give it a go, but all the stuff is at school, so i'll let you know how it went
Cheers
Oh do you mean to make them all the same tags, not one pair simple version and the other the elongated version?
Could be the case, it's just that i left this pieco of work and came back to it a couple of weeks later... my php style had changed.
I'll give it a go, but all the stuff is at school, so i'll let you know how it went
Cheers
Oh do you mean to make them all the same tags, not one pair simple version and the other the elongated version?
Could be the case, it's just that i left this pieco of work and came back to it a couple of weeks later... my php style had changed.
Last edited by mortalex; Nov 1st, 2007 at 7:43 am.
•
•
Join Date: Aug 2006
Posts: 138
Reputation:
Solved Threads: 2
You've misplaced $item and $price definitions. $i that is used is not defined yet. Try rearranging things:
That's not all though. Where does the $id come from? I can't see one in your form, let alone the script. Add a hidden field for each row in the form and pick it up as you do the $item and $price.
Also, note how I used mysql_real_escape_string() to validate the user input.
[edit]
Oh I see where does $id come from. Well, I'd rather go the hidden field way -- it's a lot more efficient.
[/edit]
PHP Syntax (Toggle Plain Text)
// Check if button name "Submit" is active, do this if($Update){ for($i=0;$i<$count;$i++){ //define each variable $item= mysql_real_escape_string($_POST['item'][$i]); $price = mysql_real_escape_string($_POST['price'][$i]); $sql1="UPDATE $table SET item='$item', price='$price' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); mysql_error(); } }
That's not all though. Where does the $id come from? I can't see one in your form, let alone the script. Add a hidden field for each row in the form and pick it up as you do the $item and $price.
Also, note how I used mysql_real_escape_string() to validate the user input.
[edit]
Oh I see where does $id come from. Well, I'd rather go the hidden field way -- it's a lot more efficient.
[/edit]
Last edited by php_daemon; Nov 1st, 2007 at 6:35 pm.
![]() |
Similar Threads
- mysql DELETE not working (PHP)
- mysql UPDATE not working! Why? (PHP)
- mysql update help??? (PHP)
Other Threads in the PHP Forum
- Previous Thread: Barcode
- Next Thread: dropdown box
Views: 3276 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache array box buttons cakephp check checkbox class cms code cookies database date directory display download dropdown drupal dynamic echo email error file files folder form forms function functions header hosting href htaccess html image include insert ip java javascript joomla jquery limit link list login loop mail menu methods mlm mod_rewrite multiple mysql order parse password paypal pdf php post problem query radio random redirect regex remote rewrite script search select server session sessions shopping soap sort sorting source sql storage string table unicode update upload url user validation variable video web website wordpress xml zend





