944,126 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 17567
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 23rd, 2007
0

updating records using php and mysql

Expand Post »
hi,

I am creating a form for user to update their details which is saved in the database. So far i can show their details in the form where i have a submit button but the updating part doesnt work(it doesnt update at all). here is the code:

PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <?php
  3.  
  4. session_start();
  5.  
  6. $host="localhost"; // Host name
  7. $username="user"; // Mysql username
  8. $password="123456"; // Mysql password
  9. $db_name="db"; // Database name
  10. $tbl_name="member"; // Table name
  11.  
  12. // Connect to server and select databse.
  13. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  14. mysql_select_db("$db_name")or die("cannot select DB");
  15.  
  16.  
  17. $username = $_SESSION['myusername'];
  18.  
  19. if(!isset($_SESSION['myusername']))
  20. {
  21. header("location: main_login.php");
  22. }
  23. else
  24. {
  25. $query=" SELECT * FROM $tbl_name WHERE LoginName='$username'";
  26. $result=mysql_query($query);
  27. $num=mysql_numrows($result);
  28. mysql_close();
  29.  
  30. $i=0;
  31. while ($i < $num) {
  32. $phone=mysql_result($result,$i,"telephone");
  33. $add=mysql_result($result,$i,"address");
  34. $town=mysql_result($result,$i,"town");
  35. $email=mysql_result($result,$i,"email");
  36. $city=mysql_result($result,$i,"city");
  37. $postcode=mysql_result($result,$i,"postcode");
  38. ++$i;
  39. }
  40. $u_phone=$_POST['phone'];
  41. $u_add=$_POST['add'];
  42. $u_town=$_POST['town'];
  43. $u_city=$_POST['city'];
  44. $u_postcode=$_POST['postcode'];
  45. $u_email=$_POST['email'];
  46.  
  47.  
  48. $query1="UPDATE $tbl_name SET memberNo = NULL, LoginName=NULL, title=NULL, firstName=NULL, lastName=NULL, gender= NULL, address='$u_add', town='$u_town', city='$u_city', postcode='$u_postcode', telephone='$u_phone', email='$u_email', mshipType = NULL";
  49. mysql_query($query1);
  50. echo "Record Updated";
  51. mysql_close();
  52. }
  53. ?>
  54. <form action="changedetails.php" method="post">
  55. <table>
  56. <tr>
  57. <td>Phone Number :</td>
  58. <td><input type="text" name="phone" size="30" value="<?php echo $phone; ?>"></td>
  59. </tr>
  60. <tr>
  61. <td>Address :</td>
  62. <td><input type="text" name="add" size="30" value="<?php echo $add; ?>"></td>
  63. </tr>
  64. <tr>
  65. <td>Town :</td>
  66. <td><input type="text" name="town" size="30" value="<?php echo $town; ?>"></td>
  67. </tr>
  68. <tr>
  69. <td>City :</td>
  70. <td><input type="text" name="city" size="30" value="<?php echo $city; ?>"></td>
  71. </tr>
  72. <tr>
  73. <td>Post Code :</td>
  74. <td><input type="text" name="postcode" size="30" value="<?php echo $postcode; ?>"></td>
  75. </tr>
  76. <tr>
  77. <td>E-mail Address :</td>
  78. <td><input type="text" name="email" size="30" value="<?php echo $email; ?>" ></td>
  79. </tr>
  80. </table>
  81.  
  82. <p>
  83.  
  84. <input type="Submit" value="Update">
  85. <input type="Submit" value="Cancel">
  86. </form>
  87. </html>

here is the table:
PHP Syntax (Toggle Plain Text)
  1. CREATE TABLE Member(
  2. memberNo INT(8) UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
  3. LoginName VARCHAR(6) NOT NULL UNIQUE,
  4. title VARCHAR(5),
  5. firstName VARCHAR(20) NOT NULL,
  6. lastName VARCHAR(20) NOT NULL,
  7. gender VARCHAR(6) NOT NULL
  8. CHECK(gender IN('MALE','FEMALE')),
  9. address VARCHAR(30),
  10. town VARCHAR(20),
  11. city VARCHAR(20),
  12. postcode VARCHAR(8) NOT NULL,
  13. telephone INT(11),
  14. email VARCHAR(50) NOT NULL,
  15. mshipType VARCHAR(15) NOT NULL
  16. CHECK(mshipType IN('STUDENT','STAFF','ALUMNI')),
  17. password VARCHAR(20) NOT NULL,
  18. PRIMARY KEY(memberNo)
  19. );
Last edited by sam1; Mar 23rd, 2007 at 3:40 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Mar 23rd, 2007
0

Re: updating records using php and mysql

i ran a search in the php documentation.

i dont know if this will help, but its worth checking out.

http://us2.php.net/manual/en/function.newt-refresh.php
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
hunkychop is offline Offline
55 posts
since Mar 2007
Mar 24th, 2007
0

Re: updating records using php and mysql

According to the UPDATE syntax, I observe that your $query1 is missing the WHERE clause to select the record or records to be updated.

The specification says that if the WHERE is missing, every row in the table will be updated!.

I do not thik that that is what you want.

i i am correct, you should add to $query1 the following text:
WHERE LoginName='$username'';
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jgutierrez@mail is offline Offline
2 posts
since Nov 2005
Mar 24th, 2007
0

Re: updating records using php and mysql

no worries guys it is solved:cheesy:
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Mar 1st, 2008
0

Re: updating records using php and mysql

how did you solve it? I really need to know...our project is due next week,,,please...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jicoro is offline Offline
1 posts
since Mar 2008
Mar 2nd, 2008
0

Re: updating records using php and mysql

Hi,

if you don't have the WHERE clause in your sql script, your entire table will be update, but if you don't see this you probably have some other errors. try use:
mysql_query($sql) or die(mysql_error());

also try to use addslashes() function to escape some chars.
Reputation Points: 10
Solved Threads: 15
Junior Poster in Training
silviuks is offline Offline
95 posts
since Apr 2006
Mar 5th, 2008
0

Re: updating records using php and mysql

as said above just use the where clause and u should be fine mate
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
Nov 11th, 2009
0
Re: updating records using php and mysql
is there any code of adding record using drop down menu? or should i say,the <select><option> function?all i know is input text area..tnx..
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
takeshi is offline Offline
96 posts
since Nov 2009
Nov 11th, 2009
0
Re: updating records using php and mysql
its the same as u would do for input text...
just access the data from ur post variable as $_POST["yourSelectName"] and u can very well add it to ur record as u do for text. the post variable will contain the value of option which user selected...

Click to Expand / Collapse  Quote originally posted by takeshi ...
is there any code of adding record using drop down menu? or should i say,the <select><option> function?all i know is input text area..tnx..
Reputation Points: 13
Solved Threads: 21
Junior Poster
venkat0904 is offline Offline
186 posts
since Oct 2009
Nov 11th, 2009
0

yap..

Click to Expand / Collapse  Quote originally posted by venkat0904 ...
its the same as u would do for input text...
just access the data from ur post variable as $_POST["yourSelectName"] and u can very well add it to ur record as u do for text. the post variable will contain the value of option which user selected...

>> so the code will be <select name="sample here">
<option value="<?php echo $row_sample here['sample here']?>" ?
i used the this array > while ($row= mysql_fetch_array($result)); before declaring the <select name =" "> and it's value..there's an error with that. the error was "< and >" but i can't find which "< and >" is wrong.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
takeshi is offline Offline
96 posts
since Nov 2009

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: help me!
Next Thread in PHP Forum Timeline: Extracting data from database - needs help!





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


Follow us on Twitter


© 2011 DaniWeb® LLC