updating records using php and mysql

Reply

Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

updating records using php and mysql

 
0
  #1
Mar 23rd, 2007
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:

  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 55
Reputation: hunkychop is an unknown quantity at this point 
Solved Threads: 4
hunkychop's Avatar
hunkychop hunkychop is offline Offline
Junior Poster in Training

Re: updating records using php and mysql

 
0
  #2
Mar 23rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 2
Reputation: jgutierrez@mail is an unknown quantity at this point 
Solved Threads: 1
jgutierrez@mail jgutierrez@mail is offline Offline
Newbie Poster

Re: updating records using php and mysql

 
0
  #3
Mar 24th, 2007
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'';
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: updating records using php and mysql

 
0
  #4
Mar 24th, 2007
no worries guys it is solved:cheesy:
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: jicoro is an unknown quantity at this point 
Solved Threads: 0
jicoro jicoro is offline Offline
Newbie Poster

Re: updating records using php and mysql

 
0
  #5
Mar 1st, 2008
how did you solve it? I really need to know...our project is due next week,,,please...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 66
Reputation: silviuks is an unknown quantity at this point 
Solved Threads: 11
silviuks silviuks is offline Offline
Junior Poster in Training

Re: updating records using php and mysql

 
0
  #6
Mar 2nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: updating records using php and mysql

 
0
  #7
Mar 5th, 2008
as said above just use the where clause and u should be fine mate
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 11
Reputation: takeshi is an unknown quantity at this point 
Solved Threads: 0
takeshi takeshi is offline Offline
Newbie Poster
 
0
  #8
15 Days Ago
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..
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 142
Reputation: venkat0904 is an unknown quantity at this point 
Solved Threads: 14
venkat0904's Avatar
venkat0904 venkat0904 is online now Online
Junior Poster
 
0
  #9
15 Days Ago
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...

Originally Posted by takeshi View Post
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..
Gimme reputation points if u find my post helpful.
use [code] tags wherever applicable
dont start a new thread unless u cant find the topic already on forum.
mark a thread "solved" as soon as u get a solution
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 11
Reputation: takeshi is an unknown quantity at this point 
Solved Threads: 0
takeshi takeshi is offline Offline
Newbie Poster

yap..

 
0
  #10
15 Days Ago
Originally Posted by venkat0904 View Post
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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC