DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   updating records using php and mysql (http://www.daniweb.com/forums/thread73172.html)

sam1 Mar 23rd, 2007 3:35 pm
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)
);

hunkychop Mar 23rd, 2007 11:08 pm
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

jgutierrez@mail Mar 24th, 2007 12:42 am
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'';

sam1 Mar 24th, 2007 9:50 am
Re: updating records using php and mysql
 
no worries guys it is solved:cheesy:

jicoro Mar 1st, 2008 10:55 pm
Re: updating records using php and mysql
 
how did you solve it? I really need to know...our project is due next week,,,please...

silviuks Mar 2nd, 2008 4:32 am
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.

sam1 Mar 5th, 2008 12:09 pm
Re: updating records using php and mysql
 
as said above just use the where clause and u should be fine mate

takeshi Nov 11th, 2009 7:09 am
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..

venkat0904 Nov 11th, 2009 8:12 am
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...

Quote:

Originally Posted by takeshi (Post 1043782)
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..


takeshi Nov 11th, 2009 8:44 am
yap..
 
Quote:

Originally Posted by venkat0904 (Post 1043852)
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.


All times are GMT -4. The time now is 9:19 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC