954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

updating records using php and mysql

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:

<html>
<?php

session_start();

$host="localhost"; // Host name
$username="user"; // Mysql username
$password="123456"; // Mysql password
$db_name="db"; // Database name
$tbl_name="member"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");  
    
    
$username = $_SESSION['myusername'];

if(!isset($_SESSION['myusername']))
	{
   header("location: main_login.php");
}
  else
{
	$query=" SELECT * FROM $tbl_name WHERE LoginName='$username'";
	$result=mysql_query($query);
	$num=mysql_numrows($result);
	mysql_close();

$i=0;
while ($i < $num) {
$phone=mysql_result($result,$i,"telephone");
$add=mysql_result($result,$i,"address");
$town=mysql_result($result,$i,"town");
$email=mysql_result($result,$i,"email");
$city=mysql_result($result,$i,"city");
$postcode=mysql_result($result,$i,"postcode");
++$i;
}
$u_phone=$_POST['phone'];
$u_add=$_POST['add'];
$u_town=$_POST['town'];
$u_city=$_POST['city'];
$u_postcode=$_POST['postcode'];
$u_email=$_POST['email'];


$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";
mysql_query($query1);
echo "Record Updated";
mysql_close();
}
?>
<form action="changedetails.php" method="post">
<table>
<tr>
	<td>Phone Number :</td>
	<td><input type="text" name="phone" size="30" value="<?php echo $phone; ?>"></td>
</tr>
<tr>
	<td>Address :</td>
	<td><input type="text" name="add" size="30" value="<?php echo $add; ?>"></td>
</tr>
<tr>
	<td>Town :</td>
	<td><input type="text" name="town" size="30" value="<?php echo $town; ?>"></td>
</tr>
<tr>
	<td>City :</td>
	<td><input type="text" name="city" size="30" value="<?php echo $city; ?>"></td>
</tr>
<tr>
	<td>Post Code :</td>
	<td><input type="text" name="postcode" size="30" value="<?php echo $postcode; ?>"></td>
</tr>
<tr>
	<td>E-mail Address :</td>
	<td><input type="text" name="email" size="30" value="<?php echo $email; ?>" ></td>
</tr>
</table>

 <p>

<input type="Submit" value="Update">
<input type="Submit" value="Cancel">
</form>
</html>


here is the table:

CREATE TABLE Member(
 memberNo       INT(8) UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
 LoginName 	VARCHAR(6)  NOT NULL UNIQUE,
 title          VARCHAR(5),
 firstName      VARCHAR(20) NOT NULL,
 lastName       VARCHAR(20) NOT NULL,
 gender         VARCHAR(6)  NOT NULL
                             CHECK(gender IN('MALE','FEMALE')),
 address        VARCHAR(30), 
 town           VARCHAR(20),
 city           VARCHAR(20),
 postcode       VARCHAR(8)  NOT NULL,
 telephone      INT(11),
 email          VARCHAR(50) NOT NULL,
 mshipType      VARCHAR(15) NOT NULL 
                             CHECK(mshipType IN('STUDENT','STAFF','ALUMNI')),
 password       VARCHAR(20) NOT NULL,
 PRIMARY KEY(memberNo)
);
sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

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

hunkychop
Junior Poster in Training
55 posts since Mar 2007
Reputation Points: 13
Solved Threads: 4
 

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'';

jgutierrez@mail
Newbie Poster
2 posts since Nov 2005
Reputation Points: 10
Solved Threads: 1
 

no worries guys it is solved:cheesy:

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

how did you solve it? I really need to know...our project is due next week,,,please...

jicoro
Newbie Poster
1 post since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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.

silviuks
Junior Poster in Training
96 posts since Apr 2006
Reputation Points: 10
Solved Threads: 15
 

as said above just use the where clause and u should be fine mate

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

is there any code of adding record using drop down menu? or should i say,the function?all i know is input text area..tnx..

takeshi
Junior Poster in Training
96 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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...

is there any code of adding record using drop down menu? or should i say,the function?all i know is input text area..tnx..
venkat0904
Junior Poster
190 posts since Oct 2009
Reputation Points: 13
Solved Threads: 21
 
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 while ($row= mysql_fetch_array($result)); before declaring the and it's value..there's an error with that. the error was "< and >" but i can't find which "< and >" is wrong.

takeshi
Junior Poster in Training
96 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
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...


>> or i mean

is this correct?

takeshi
Junior Poster in Training
96 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

well this code doesnt looks gud... could you be more specific with what code u r using... paste the exact code here and use [code] tags.. also post the exact error u r stuck with...
I will write an example to make myself clear

<select name="sample_select">
	  <option value="<?php echo $row['sample_field']; ?>">option1</option>
	  </select>


and when u trying to use the selected value just retrieve it like this..

$selected = $_POST['sample_select']; //POST or GET whatever u have used in ur form

this way u can use this variable $selected wherever required..
Hope this clarifies ur doubt..>> so the code will be while ($row= mysql_fetch_array($result)); before declaring the and it's value..there's an error with that. the error was "< and >" but i can't find which "< and >" is wrong.

venkat0904
Junior Poster
190 posts since Oct 2009
Reputation Points: 13
Solved Threads: 21
 

well this code doesnt looks gud... could you be more specific with what code u r using... paste the exact code here and use [code] tags.. also post the exact error u r stuck with... I will write an example to make myself clear

<select name="sample_select">
	  <option value="<?php echo $row['sample_field']; ?>">option1</option>
	  </select>

and when u trying to use the selected value just retrieve it like this..

$selected = $_POST['sample_select']; //POST or GET whatever u have used in ur form

this way u can use this variable $selected wherever required.. Hope this clarifies ur doubt..


>> hmmm i see..i saw the error..let me fix again..but here's my code..
<?php
include('connect-db.php');


$result = mysql_query("SELECT * FROM courses")
or die(mysql_error());

echo"          ";
while($row = mysql_fetch_array( $result )) {

echo '' . $row['course'] . '';

}

echo "";


?>

takeshi
Junior Poster in Training
96 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

well this code doesnt looks gud... could you be more specific with what code u r using... paste the exact code here and use [code] tags.. also post the exact error u r stuck with... I will write an example to make myself clear

<select name="sample_select">
	  <option value="<?php echo $row['sample_field']; ?>">option1</option>
	  </select>

and when u trying to use the selected value just retrieve it like this..

$selected = $_POST['sample_select']; //POST or GET whatever u have used in ur form

this way u can use this variable $selected wherever required.. Hope this clarifies ur doubt..


>> and the changes i made last night..
<?php
include('connect-db.php');


$result = mysql_query("SELECT * FROM courses")
or die(mysql_error());

echo"          
while($row = mysql_fetch_array( $result )) {

'>

}


?>

takeshi
Junior Poster in Training
96 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

use code tags when posting here... ur code is barely readable without it..

>> and the changes i made last night..

<?php
    include('connect-db.php');

   
    $result = mysql_query("SELECT * FROM courses") 
        or die(mysql_error());  
 
	echo"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="course">
    while($row = mysql_fetch_array( $result )) { 

        <option value=' <?php . $row['course'] . ?>'></option>
	
       }
        
    </select> 

    
?>


your selectbox should be created this way..

echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="course">';
   while($row = mysql_fetch_array( $result )) { 

      echo '<option value='.$row['course'].'>'.$row['course'].'</option>';  //the second $row['course'] is the text which is actually going to be displayed to user.. u can use course title or whatever u have in ur db
	
       }
        
   echo '</select>';

hope this will solve it

venkat0904
Junior Poster
190 posts since Oct 2009
Reputation Points: 13
Solved Threads: 21
 

use code tags when posting here... ur code is barely readable without it..

your selectbox should be created this way..

echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="course">';
   while($row = mysql_fetch_array( $result )) { 

      echo '<option value='.$row['course'].'>'.$row['course'].'</option>';  //the second $row['course'] is the text which is actually going to be displayed to user.. u can use course title or whatever u have in ur db
	
       }
        
   echo '</select>';

hope this will solve it

> oOpss sorry..this is the first time i used and joined in this forum...
ok i'll try to change my code on that part and try to run it.hope it wil run..
Parse Error: Unexpected syntax error '>'...tnx..

takeshi
Junior Poster in Training
96 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

use code tags when posting here... ur code is barely readable without it..

your selectbox should be created this way..

echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="course">';
   while($row = mysql_fetch_array( $result )) { 

      echo '<option value='.$row['course'].'>'.$row['course'].'</option>';  //the second $row['course'] is the text which is actually going to be displayed to user.. u can use course title or whatever u have in ur db
	
       }
        
   echo '</select>';

hope this will solve it

echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="course">';
   while($row = mysql_fetch_array( $result )) { 

      echo '<option value='.$row['course'].'>'.$row['course'].'</option>';  //the second $row['course'] is the text which is actually going to be displayed to user.. u can use course title or whatever u have in ur db
	
       }
        
   echo '</select>';

hope this will solve it[/QUOTE]

still the same error..here's the error.. Parse error: syntax error, unexpected '<' in C:\wamp\www\41e1\thesis_draft\add_students.php on line 138...

i used these code from you...still there's an error occured.

<?php
    include('connect-db.php');

   
    $result = mysql_query("SELECT * FROM courses") 
        or die(mysql_error());  
 
	<select name="course">
  
      while($row = mysql_fetch_array( $result )) {
     
   
   <option value="<?php echo .$row['course'].; ?>">.$row['course'].</option>//the second $row['course'] is the text which is actually going to be displayed to user.. u can use course title or whatever u have in ur db
   
      }
   
      </select>
?>
takeshi
Junior Poster in Training
96 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

u messed it up pretty bad...
Look at the code i pasted and then look what u have used... I can see hell lot of difference.
just try the code i provided and once u get it working then try to make whatever changes u want to have...
As of now ur syntax itself is wrong... where are all the echo statements i wrote? u cant use html tags without echo'ing them

echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="course">';
   while($row = mysql_fetch_array( $result )) { 

      echo '<option value='.$row['course'].'>'.$row['course'].'</option>';  //the second $row['course'] is the text which is actually going to be displayed to user.. u can use course title or whatever u have in ur db
	
       }
        
   echo '</select>';

hope this will solve it

<?php
    include('connect-db.php');

   
    $result = mysql_query("SELECT * FROM courses") 
        or die(mysql_error());  
 
	<select name="course">
  
      while($row = mysql_fetch_array( $result )) {
     
   
   <option value="<?php echo .$row['course'].; ?>">.$row['course'].</option>//the second $row['course'] is the text which is actually going to be displayed to user.. u can use course title or whatever u have in ur db
   
      }
   
      </select>
?>


[/QUOTE]

venkat0904
Junior Poster
190 posts since Oct 2009
Reputation Points: 13
Solved Threads: 21
 

u messed it up pretty bad... Look at the code i pasted and then look what u have used... I can see hell lot of difference. just try the code i provided and once u get it working then try to make whatever changes u want to have... As of now ur syntax itself is wrong... where are all the echo statements i wrote? u cant use html tags without echo'ing them

<?php
    include('connect-db.php');

   
    $result = mysql_query("SELECT * FROM courses") 
        or die(mysql_error());  
 
	<select name="course">
  
      while($row = mysql_fetch_array( $result )) {
     
   
   <option value="<?php echo .$row['course'].; ?>">.$row['course'].</option>//the second $row['course'] is the text which is actually going to be displayed to user.. u can use course title or whatever u have in ur db
   
      }
   
      </select>
?>

[/QUOTE]

yap..i know..i used your code just like what you've said..i already run it..but when i add new name and course,when i click add button,succesfully added appeared but the data didn't add from the database itself...that's why i change some code..(and that's the code i posted)sorry for statement(code).it's just a misinterpretation...
takeshi
Junior Poster in Training
96 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

then wy dont u post up the code used for database insertion... coz i guess thats wher d problem lies..

venkat0904
Junior Poster
190 posts since Oct 2009
Reputation Points: 13
Solved Threads: 21
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You