944,183 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 4646
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 31st, 2007
0

MySQL update not working

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  1. <h1>Update Items</h1>
  2. <p>Update items below.</p>
  3.  
  4.  
  5. <?php
  6. $db_host = 'X;
  7. $db_user = 'X';
  8. $db_pwd = 'X';
  9. $database = 'X';
  10. $table = 'pricelist';
  11. if (!mysql_connect($db_host, $db_user, $db_pwd))
  12. die("Can't connect to database");
  13. if (!mysql_select_db(X))
  14. die("Can't select database");
  15.  
  16. // sending query
  17. $sql="SELECT * FROM $table";
  18. $result=mysql_query($sql);
  19.  
  20. // Count table rows
  21. $count=mysql_num_rows($result);
  22. ?>
  23. <table width="500" border="0" cellspacing="1" cellpadding="0">
  24. <form name="form1" method="post" action="">
  25. <tr>
  26. <td>
  27. <table width="500" border="0" cellspacing="1" cellpadding="0">
  28.  
  29.  
  30. <tr>
  31. <td align="center"><strong>Id</strong></td>
  32. <td align="center"><strong>Item</strong></td>
  33. <td align="center"><strong>Price</strong></td>
  34. </tr>
  35. <?php
  36. while($rows=mysql_fetch_array($result)){
  37. ?>
  38. <tr>
  39. <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
  40. <td align="center"><input name="item[]" type="text" id="item" value="<? echo $rows['item']; ?>"></td>
  41. <td align="center"><input name="price[]" type="text" id="price" value="<? echo $rows['price']; ?>"></td>
  42. </tr>
  43. <?php
  44. }
  45. ?>
  46. <tr>
  47. <td colspan="4" align="center"><input type="submit" name="Update" value="Update"></td>
  48. </tr>
  49. </table>
  50. </td>
  51. </tr>
  52. </form>
  53. </table>
  54. <?php
  55.  
  56. //define each variable
  57. $item= $_POST['item'][$i];
  58. $price = $_POST['price'][$i];
  59.  
  60.  
  61. // Check if button name "Submit" is active, do this
  62. if($Update){
  63. for($i=0;$i<$count;$i++){
  64. $sql1="UPDATE $table SET item='$item', price='$price' WHERE id='$id[$i]'";
  65. $result1=mysql_query($sql1);
  66. mysql_error();
  67. }
  68. }
  69.  
  70. if($result1){
  71. header("location:pricelistdata.php");
  72. }
  73. mysql_close();
  74. ?>
  75.  

Cheers.
Last edited by mortalex; Oct 31st, 2007 at 8:43 am. Reason: Editing out Passwords and stuff
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
mortalex is offline Offline
30 posts
since Oct 2007
Oct 31st, 2007
0

Re: MySQL update not working

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>
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Nov 1st, 2007
0

Re: MySQL update not working

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.
Last edited by mortalex; Nov 1st, 2007 at 8:43 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
mortalex is offline Offline
30 posts
since Oct 2007
Nov 1st, 2007
0

Re: MySQL update not working

You've misplaced $item and $price definitions. $i that is used is not defined yet. Try rearranging things:

PHP Syntax (Toggle Plain Text)
  1. // Check if button name "Submit" is active, do this
  2. if($Update){
  3. for($i=0;$i<$count;$i++){
  4. //define each variable
  5. $item= mysql_real_escape_string($_POST['item'][$i]);
  6. $price = mysql_real_escape_string($_POST['price'][$i]);
  7. $sql1="UPDATE $table SET item='$item', price='$price' WHERE id='$id[$i]'";
  8. $result1=mysql_query($sql1);
  9. mysql_error();
  10. }
  11. }

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 7:35 pm.
Reputation Points: 13
Solved Threads: 2
Junior Poster
php_daemon is offline Offline
138 posts
since Aug 2006
Nov 2nd, 2007
0

Re: MySQL update not working

Guys, I have this problem witha form- suggestions.

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/genericads.com.au/httpdocs/photoads/contact.php on line 15
a major decryption error occurred. [logged]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
genericgene is offline Offline
1 posts
since Nov 2007
Nov 2nd, 2007
0

Re: MySQL update not working

wheres your code?? contact.php_?
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Nov 2nd, 2007
0

Re: MySQL update not working

Click to Expand / Collapse  Quote originally posted by php_daemon ...
Oh I see where does $id come from. Well, I'd rather go the hidden field way -- it's a lot more efficient.
What do you mean the hidden field way? Sorry i'm new to this.
Reputation Points: 10
Solved Threads: 0
Light Poster
mortalex is offline Offline
30 posts
since Oct 2007
Nov 2nd, 2007
0

Re: MySQL update not working

Click to Expand / Collapse  Quote originally posted by mortalex ...
What do you mean the hidden field way? Sorry i'm new to this.
you can use hidden fields to keep datas which need not to be seen by browsers...
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Nov 3rd, 2007
0

Re: MySQL update not working

Ok i've looked into the hidden field suggestion, but really i still don't have a clue as to what to do. Doesn't this only stop it from being shown in the web browser, does it do anything else?

Cheers
Reputation Points: 10
Solved Threads: 0
Light Poster
mortalex is offline Offline
30 posts
since Oct 2007
Nov 3rd, 2007
0

Re: MySQL update not working

Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Barcode
Next Thread in PHP Forum Timeline: dropdown box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC