Update database issue

Reply

Join Date: Dec 2008
Posts: 2
Reputation: pupsaa is an unknown quantity at this point 
Solved Threads: 0
pupsaa pupsaa is offline Offline
Newbie Poster

Update database issue

 
0
  #1
Dec 27th, 2008
Hi! First of all to say that I am a newbie in PHP world. I have a table in a database and I am trying to update a row's content based on its id that I get inside the page's link.
But the update never occurs. Please help. I can't see what's wrong.

Thanks.

  1. <?php
  2. // Connects to your Database
  3. mysql_connect("localhost", "user", "pass") or die(mysql_error());
  4. mysql_select_db("bestcv") or die(mysql_error());
  5. $pno=$_GET['pno'];
  6.  
  7. if(isset($_COOKIE['bestupt']))
  8.  
  9. //if there is, it logs you in and directes you to the members page
  10. {
  11. $username = $_COOKIE['bestupt'];
  12. $pass = $_COOKIE['bestupt_pass'];
  13. $check = mysql_query("SELECT * FROM bestupt_login WHERE username = '$username'")or die(mysql_error());
  14. while($info = mysql_fetch_array( $check ))
  15. {
  16.  
  17. //if the cookie has the wrong password, they are taken to the login page
  18. if ($pass != $info['password'] || $info['auth'] != 0)
  19. { header("Location: login.php");
  20. }
  21.  
  22. //otherwise they are shown the admin area
  23. else
  24. if($info['auth']==0)
  25. {
  26.  
  27. //This code runs if the form has been submitted
  28. if (isset($_POST['submit'])) {
  29.  
  30. //This makes sure they did not leave any fields blank
  31. if (!$_POST['name'] | !$_POST['surname'] | !$_POST['position'] ) {
  32. die('You did not complete all of the required fields');
  33. }
  34.  
  35. // now we insert it into the database
  36. $pos = $_POST['position'];
  37. $board = 0;
  38. if($pos == 'Voting Member')
  39. {
  40. $board = 1;
  41. }
  42. else
  43. if($pos == 'Secretary')
  44. {
  45. $board = 2;
  46. }
  47. else
  48. if($pos == 'Treasurer')
  49. {
  50. $board = 3;
  51. }
  52. else
  53. if($pos == 'MK Vicepresident')
  54. {
  55. $board = 4;
  56. }
  57. else
  58. if($pos == 'HR Vicepresident')
  59. {
  60. $board = 5;
  61. }
  62. else
  63. if($pos == 'President')
  64. {
  65. $board = 6;
  66. }
  67.  
  68.  
  69. $add=mysql_query("UPDATE people SET name='".$_POST['name']."', surname='".$_POST['surname']."', position='".$_POST['position']."', photo='".$_POST['photo']."', board='".$board."' WHERE pno='".$pno."' ")or die(mysql_error());
  70.  
  71.  
  72. ?>
  73. <h1>Member added</h1>
  74. <a href="admin.php">Back</a>
  75.  
  76. <?
  77. }
  78. else
  79. {
  80. $usertoedit = mysql_query("SELECT * FROM people WHERE pno = '".$pno."'")or die(mysql_error());
  81. $info = mysql_fetch_array($usertoedit);
  82. ?>
  83. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  84. <table border="0">
  85. <tr><td>Name:</td><td>
  86. <input type="text" name="name" value="<?php echo $info['name']; ?>" maxlength="60">
  87. </td></tr>
  88. <tr><td>Surname:</td><td>
  89. <input type="text" name="surname" value="<?php echo $info['surname']; ?>" maxlength="60">
  90. </td></tr>
  91. <tr><td>Position:</td></tr>
  92. <tr><td>
  93. <input type="radio" name="position" value="Alumnus">Alumnus<br>
  94. <input type="radio" name="position" value="Member">Member<br>
  95. <input type="radio" name="position" value="Former Member">Former Member<br>
  96. <input type="radio" name="position" value="Voting Member">Voting Member<br>
  97. <input type="radio" name="position" value="HR Vicepresident">HR Vicepresident<br>
  98. <input type="radio" name="position" value="MK Vicepresident">MK Vicepresident<br>
  99. <input type="radio" name="position" value="PR Responsible">PR Responsible<br>
  100. <input type="radio" name="position" value="President">President<br>
  101. <input type="radio" name="position" value="Secretary">Secretary<br>
  102. <input type="radio" name="position" value="Treasurer">Treasurer<br>
  103. </td></tr>
  104. <tr><td>Photo:</td><td>
  105. <input type="text" name="photo" value="<?php echo $info['photo']; ?>" maxlength="10">
  106. </td></tr>
  107. <tr><th colspan=2><input type="submit" name="submit" value="OK"></th></tr> </table>
  108. </form>
  109.  
  110. <?
  111. }
  112. }
  113. }
  114. }
  115. ?>
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 10
Reputation: joshmac is an unknown quantity at this point 
Solved Threads: 1
joshmac joshmac is offline Offline
Newbie Poster

Re: Update database issue

 
0
  #2
Dec 27th, 2008
Not sure if it will work for you, but I usually add something like this before the submit button:

  1. <input type="hidden" name="pno" value="<?php echo $pno; ?>" />

I am assuming that "pno" is the id of the content that should be updated.
Last edited by joshmac; Dec 27th, 2008 at 6:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: pupsaa is an unknown quantity at this point 
Solved Threads: 0
pupsaa pupsaa is offline Offline
Newbie Poster

Re: Update database issue

 
0
  #3
Dec 27th, 2008
Originally Posted by joshmac View Post
Not sure if it will work for you, but I usually add something like this before the submit button:

  1. <input type="hidden" name="pno" value="<?php echo $pno; ?>" />

I am assuming that "pno" is the id of the content that should be updated.
I know what you mean. Cause after I press submit, it basically reloads the page, but without retransmitting the argument.
Thanks very much. It works now
Last edited by pupsaa; Dec 27th, 2008 at 6:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 10
Reputation: joshmac is an unknown quantity at this point 
Solved Threads: 1
joshmac joshmac is offline Offline
Newbie Poster

Re: Update database issue

 
0
  #4
Dec 27th, 2008
You are welcome.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 30
Reputation: dilipv is an unknown quantity at this point 
Solved Threads: 4
dilipv dilipv is offline Offline
Light Poster

Re: Update database issue

 
0
  #5
Dec 29th, 2008
Originally Posted by pupsaa View Post
<input type="radio" name="position" value="Alumnus">Alumnus<br>
<input type="radio" name="position" value="Member">Member<br>
<input type="radio" name="position" value="Former Member">Former Member<br>
<input type="radio" name="position" value="Voting Member">Voting Member<br>
<input type="radio" name="position" value="HR Vicepresident">HR Vicepresident<br>
<input type="radio" name="position" value="MK Vicepresident">MK Vicepresident<br>
<input type="radio" name="position" value="PR Responsible">PR Responsible<br>
<input type="radio" name="position" value="President">President<br>
<input type="radio" name="position" value="Secretary">Secretary<br>
<input type="radio" name="position" value="Treasurer">Treasurer<br>
[/code]
hi, i think you have problem with primary key after postback it wont get primary key by which it will make updation , so use
<input type="hidden" name="pno" value="<?php echo $pno; ?>" />
and also you have difficult programming structure in above quote.
<input type="radio" name="position" value="1">Voting Member<br>
At value property of each radio button insert its corresponding value like you have checked for 0,1,2,3,4,5,6.
Which will ease your task.
Originally Posted by pupsaa View Post
$add=mysql_query("UPDATE people SET name='".$_POST['name']."', surname='".$_POST['surname']."', position='".$_POST['position']."', photo='".$_POST['photo']."', board='".$board."' WHERE pno='".$_POST['pno']."' ")or die(mysql_error());
[/code]
hope you understand my point
Thanks & Regards
Dilip Kumar Vishwakarma
Dilip Kumar Vishwakarma
Programmer
.Net Consulting
If this post is answer for your query then don't forget to mark as an answer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC