MySQL update not working

Please support our PHP advertiser: 50% off 6 Months Dedicated Server Hosting from 1&1!
Reply

Join Date: Oct 2007
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

MySQL update not working

 
0
  #1
Oct 31st, 2007
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:

  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 7:43 am. Reason: Editing out Passwords and stuff
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 271
Reputation: fatihpiristine has a little shameless behaviour in the past 
Solved Threads: 18
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: MySQL update not working

 
0
  #2
Oct 31st, 2007
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>
Do a favour, leave me alone
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: MySQL update not working

 
0
  #3
Nov 1st, 2007
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 7:43 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 138
Reputation: php_daemon is an unknown quantity at this point 
Solved Threads: 2
php_daemon php_daemon is offline Offline
Junior Poster

Re: MySQL update not working

 
0
  #4
Nov 1st, 2007
You've misplaced $item and $price definitions. $i that is used is not defined yet. Try rearranging things:

  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 6:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 1
Reputation: genericgene is an unknown quantity at this point 
Solved Threads: 0
genericgene genericgene is offline Offline
Newbie Poster

Re: MySQL update not working

 
0
  #5
Nov 2nd, 2007
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]
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 271
Reputation: fatihpiristine has a little shameless behaviour in the past 
Solved Threads: 18
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: MySQL update not working

 
0
  #6
Nov 2nd, 2007
wheres your code?? contact.php_?
Do a favour, leave me alone
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: MySQL update not working

 
0
  #7
Nov 2nd, 2007
Originally Posted by php_daemon View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 271
Reputation: fatihpiristine has a little shameless behaviour in the past 
Solved Threads: 18
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: MySQL update not working

 
0
  #8
Nov 2nd, 2007
Originally Posted by mortalex View Post
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...
Do a favour, leave me alone
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 30
Reputation: mortalex is an unknown quantity at this point 
Solved Threads: 0
mortalex mortalex is offline Offline
Light Poster

Re: MySQL update not working

 
0
  #9
Nov 3rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 271
Reputation: fatihpiristine has a little shameless behaviour in the past 
Solved Threads: 18
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: MySQL update not working

 
0
  #10
Nov 3rd, 2007
Do a favour, leave me alone
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the PHP Forum


Views: 3276 | Replies: 12
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC