943,865 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 632
  • PHP RSS
Dec 27th, 2008
0

Update database issue

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

php Syntax (Toggle Plain Text)
  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. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pupsaa is offline Offline
2 posts
since Dec 2008
Dec 27th, 2008
0

Re: Update database issue

Not sure if it will work for you, but I usually add something like this before the submit button:

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 16
Solved Threads: 1
Light Poster
joshmac is offline Offline
29 posts
since Apr 2008
Dec 27th, 2008
0

Re: Update database issue

Click to Expand / Collapse  Quote originally posted by joshmac ...
Not sure if it will work for you, but I usually add something like this before the submit button:

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pupsaa is offline Offline
2 posts
since Dec 2008
Dec 27th, 2008
0

Re: Update database issue

You are welcome.
Reputation Points: 16
Solved Threads: 1
Light Poster
joshmac is offline Offline
29 posts
since Apr 2008
Dec 29th, 2008
0

Re: Update database issue

Click to Expand / Collapse  Quote originally posted by pupsaa ...
<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.
Click to Expand / Collapse  Quote originally posted by pupsaa ...
$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
Reputation Points: 10
Solved Threads: 4
Light Poster
dilipv is offline Offline
30 posts
since Feb 2008

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: phpadmin v3.1
Next Thread in PHP Forum Timeline: date problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC