How do i display a record of MySQL database in order to edit it?
01-24-2009, 01:41 PM
|
How do i display a record of MySQL database in order to edit it?
|
Posts: 7
Name: Fazil Baksh
Location: Ottawa, Canada
|
I have the following code and would like to be able to retrieve a record (any one row) in my CONTACTS database and then edit and update it. Can someone please help me, i am not sure of what i am doing. See a sample of the table below. THANKS
PHP Code:
<?php $CID=$_GET['CID']; $con = mysql_connect("mysqlhost.xxxxxxxx.xxx:xxxx","xxxxxxxxx","xxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("d99999999", $con); $query="SELECT * FROM mypersonalcontacts WHERE CID='$CID'"; $result=mysql_query($query); $num=mysql_numrows($result); $i=$CID; $fname=mysql_result($result,$i,"fname"); $lname=mysql_result($result,$i,"lname"); $hphone=mysql_result($result,$i,"hphone"); $bphone=mysql_result($result,$i,"bphone"); $cphone=mysql_result($result,$i,"cphone"); $email=mysql_result($result,$i,"email"); $address=mysql_result($result,$i,"address"); mysql_close($con); ?>
HTML Code:
<form action="updated.php" method="post">
<input type="hidden" name="ud_CID" value="<? echo $CID; ?>">
First Name: <input type="text" name="ud_fname" value="<? echo $fname; ?>"><br>
Last Name: <input type="text" name="ud_lname" value="<? echo $lname; ?>"><br>
Home Number: <input type="text" name="ud_hphone" value="<? echo $hphone; ?>"><br>
Work Number: <input type="text" name="ud_bphone" value="<? echo $bphone; ?>"><br>
Cell Number: <input type="text" name="ud_cphone" value="<? echo $cphone; ?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo $email; ?>"><br>
Home Address: <input type="text" name="ud_address" value="<? echo $address; ?>"><br>
<input type="Submit" value="Update">
</form>
</body>
</html>
First NameLast NameHome PhoneBusiness PhoneMobile PhoneemailAddressUpdateEdit64Edit record65Edit recordAddySatellite613 907 8782(613) 240-8494McArthur Ave, Ottawa9
Last edited by hummingbird; 01-26-2009 at 09:06 PM..
|
|
|
|
01-24-2009, 01:51 PM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 48
Name: Adam Hosker
Location: West Yorkshire, UK
|
Welcome to Webmaster Talk hummingbird,
Please can you use Code Tags in future, makes it a lot easier.
I have coded the bellow for you, it is not secure (as in a user may exploit the code) and the names may be incorrect, so you may need to check.
updated.php
PHP Code:
<? $CID=$_GET['CID']; $con = mysql_connect("mysqlhost.xxxxxxxx.xxx:xxxx","xxxxx xxxx","xxxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("d99999999", $con);
$query = 'UPDATE mypersonalcontacts SET fname = "'.$_POST["ud_fname"].'", lname = "'.$_POST["ud_lname"].'", hphone = "'.$_POST["ud_hphone"].'", bphone = "'.$_POST["ud_bphone"].'", cphone = "'.$_POST["ud_cphone"].'", email = "'.$_POST["ud_email"].'", address = "'.$_POST["ud_address"].'" WHERE cid='.$_POST["ud_CID"]; $result=mysql_query($query); ?>
I hope I have been of some help,
PHP_Adam
Last edited by PHP_Adam; 01-24-2009 at 01:52 PM..
|
|
|
|
01-24-2009, 09:01 PM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 7
Name: Fazil Baksh
Location: Ottawa, Canada
|
Please, can you tell me what is Code Tags? Maybe give an example
Thanks
|
|
|
|
01-25-2009, 08:38 AM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 7
Name: Fazil Baksh
Location: Ottawa, Canada
|
My UPDATED.PHP is exactly as you show. Thanks. But, I have a table of 9 columns. The last 2 columns are hyperlinks and when i click on any one row i would like for all of the information in that row to be displayed in a form format so that i can edit or update that contact information. In my form section of the initial code when i click on edit the field is filled with "<? echo $fname; ?>" for example, and not the actual firstname of my contact for that row. How do i let the program know which row i am selecting to edit/update.
Thanks for your help.
|
|
|
|
01-25-2009, 09:31 AM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
|
Quote:
Originally Posted by hummingbird
Please, can you tell me what is Code Tags? Maybe give an example
Thanks
|
"Stickied" thread at top of this forum
http://www.webmaster-talk.com/php-fo...st-my-php.html
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
01-25-2009, 12:27 PM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 116
Name: Michele T.
Location: Ny, Ny
|
So it looks like you already have the right idea with the $_GET['CID'] but you don't define it anywhere. If you want to use a get you need to add it to the edit URL so that it includes the CID (not sure what your code looks like so here's an example):
PHP Code:
echo '<a href="page.php?CID='.$CID.'">'.$name.'</a>';
But you will probably need to change a few other things. For one, you need to do a separate query to get all of the contacts once $CID is set b/c it won't cycle through (due to the fact that one row will be returned with the CID of $CID. Also you should change your variable names so that what you use for the form is different from what you use to display the contacts. Sanitizing your input is also a good idea, that way someone can't type something random in the URL and delete the whole table xD
__________________
Freelance web+graphic designer and PHP developer.
Please login or register to view this content. Registration is FREE
|
|
|
|
01-26-2009, 09:09 PM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 7
Name: Fazil Baksh
Location: Ottawa, Canada
|
I don't understand. Can you please elaborate on
echo '<a href="page.php?CID='.$CID.'">'.$name.'</a>';
Thanks
|
|
|
|
01-26-2009, 10:11 PM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 116
Name: Michele T.
Location: Ny, Ny
|
Basically what you need to do is add an argument to the URL that is the contact id. That's all that that statement does. Since in your script you want to use $_GET[CID] you need to add CID=$somevalue to the URL. $_GET grabs things from the URL. Without anything in the URL, $_GET returns nothing, and your code doesn't work right.
If you're still confused here's a better explanation. Sometimes I'm not very good at explaining.
__________________
Freelance web+graphic designer and PHP developer.
Please login or register to view this content. Registration is FREE
|
|
|
|
02-02-2009, 10:32 PM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 7
Name: Fazil Baksh
Location: Ottawa, Canada
|
1. OK, I am still having some problems with this. This is the form i am using
HTML Code:
<html>
<head></head>
<title>Personal Contacts</title>
<script type="text/javascript">
function printpage()
{
window.print()
}
</script>
</head>
<body>
<!--------------code to mark the beginning of the form--------------->
<form action="Contacts_insert.php" method="post">
<table border=0 cellpadding=2 cellspacing=2 width="100%">
<tr>
<td colspan=4 bgcolor="gray"><font size=+2 color=#ffffff>Add Contact Information</font></td>
</tr>
<tr>
<td valign=top align=right>First Name</td>
<td valign=top><input type="text" name="fname" size="35"><br></td>
</tr>
<tr>
<td valign=top align=right>Last Name</td>
<td valign=top><input type="text" name="lname" size="35"><br></td>
</tr>
<tr>
<td valign=top align=right>Home Phone</td>
<td valign=top><input type="text" name="hphone" size="35"><br></td>
</tr>
<tr>
<td valign=top align=right>Business Phone</td>
<td valign=top><input type="text" name="bphone" size="35"><br></td>
</tr>
<tr>
<td valign=top align=right>Cell Phone</td>
<td valign=top><input type="text" name="cphone" size="35"><br>
</tr>
<tr>
<td valign=top align=right>email</td>
<td valign=top><input type="text" name="email" size="35"><br>
</tr>
<tr>
<tr>
<td valign=top align=right colspan=1>Address</td>
<td valign=top><textarea name="address" rows="5" cols="27" wrap=virtual>
</textarea><br>
<font size=2 color=#000099>or notes</font></td>
</tr>
<tr>
<td colspan=4 bgcolor="gray"><font size=+2 color=#ffffff>Submit Contact Information</font></td>
</tr>
<td><p align=left><font face="Arial,Helvetica,Geneva,Sans-serif,sans-serif" size=-2 color=gray><b>M.Fazil Baksh<br></b>207-161 Augusta St 207<br>Ottawa, Ontario, K1N 8B6<br>613-695-0734<br></font></p></td>
</td>
</tr>
<!----printing the form----->
<tr>
<td valign=top align=left> </td>
<td valign=top>
<input type="submit" value="SUBMIT">
<input type="reset" value="Erase">
</tr>
</table>
</form>
<hr></hr>
</body>
</html>
2. BELOW IS STEP 2, INSERTING DATA INTO THE DATABASE
HTML Code:
<html>
<body>
[php]
<?php
$con = mysql_connect("mysqlhost.netfirms.com:XXXX","XXXXXXXXX","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dXXXXXXXX", $con);
$sql="INSERT INTO mypersonalcontacts (fname, lname, hphone, bphone, cphone, email, address)
VALUES
('$_POST[fname]','$_POST[lname]','$_POST[hphone]','$_POST[bphone]','$_POST[cphone]','$_POST[email]', '$_POST[address]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added to your personal contacts, Thank you for the update";
mysql_close($con)
?>
[/php]
</body>
</html>
3. DISPLAYING THE DATA ON A WEB PAGE
<FONT color=black>
HTML Code:
<html>
<head>
<style type="text/css">
table.one
{
table-layout: automatic
}
table.two
{
table-layout: fixed
}
</style>
</head>
<body>
<table border=0 cellpadding=0 cellspacing=0 width="100%">
<td align=center bgcolor=blue><font size=-1 color=#ff8c00>Fazil Baksh - Christianne Malapaya</font></td>
</table>
[php]
<?php
$con = mysql_connect("mysqlhost.netfirms.com:3306","uXXXXXXX","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dXXXXXXX", $con);
$query="SELECT * FROM mypersonalcontacts ORDER BY fname ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);
echo "<table class='one' width='100%' border='1'>
<tr>
<th><font size=-1 style='font-family:verdana'><a href='orderfname.php'>First Name</font></th>
<th><font size=-1 style='font-family:verdana'><a href='orderlname.php'>Last Name</font></th>
<th><font size=-1 style='font-family:verdana'><a href='orderhphone.php'>Home Phone</font></th>
<th><font size=-1 style='font-family:verdana'><a href='orderbphone.php'>Business Phone</font></th>
<th><font size=-1 style='font-family:verdana'><a href='ordercphone.php'>Mobile Phone</font></th>
<th><font size=-1 style='font-family:verdana'><a href='orderemail.php'>email</font></th>
<th><font size=-1 style='font-family:verdana'><a href='orderaddress.php'>Address</font></th>
<th><font size=-4 style='font-family:verdana'>click record to edit</font></th>
</tr>";
if ($num==0) {
echo "The database contains no contacts yet";
}else
$i=0;
while ($i < $num)
{
$fname=mysql_result($result,$i,"fname");
$lname=mysql_result($result,$i,"lname");
$hphone=mysql_result($result,$i,"hphone");
$bphone=mysql_result($result,$i,"bphone");
$cphone=mysql_result($result,$i,"cphone");
$email=mysql_result($result,$i,"email");
$address=mysql_result($result,$i,"address");
$CID=mysql_result($result,$i,"CID");
echo "<tr>
<td><font size=-1 style='font-family:verdana'>$fname</font></td>
<td><font size=-1 style='font-family:verdana'>$lname</font></td>
<td><font size=-1 style='font-family:verdana'>$hphone</font></td>
<td><font size=-1 style='font-family:verdana'>$bphone</font></td>
<td><font size=-1 style='font-family:verdana'>$cphone</font></td>
<td><font size=-1 style='font-family:verdana'><a href='mailto:$email'>$email</a></font></td>
<td><font size=-1 style='font-family:verdana'>$address</font></td>
<th><font size=-4 style='font-family:verdana'><a href='update_page.html?CID=$CID'>$CID</a></font></th>
</tr>";
$i++;
}
echo "</table>";
mysql_close($con);
?>
[/php]
</body>
</html>
4. READING THE DATA TO A FORM FOR EDITING....HELP!!!!
HTML Code:
<html>
<head></head>
<body>
[php]
<?php
//$CID=$_GET['CID'];
$con = mysql_connect("mysqlhost.netfirms.com:3306","uXXXXXXX","XXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dXXXXXXXX", $con);
$query="SELECT * FROM mypersonalcontacts WHERE CID='$CID'";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num)
{
$CID=mysql_result($result,$i,"CID");
$fname=mysql_result($result,$i,"fname");
$lname=mysql_result($result,$i,"lname");
$hphone=mysql_result($result,$i,"hphone");
$bphone=mysql_result($result,$i,"bphone");
$cphone=mysql_result($result,$i,"cphone");
$email=mysql_result($result,$i,"email");
$address=mysql_result($result,$i,"address");
$i++;
}
mysql_close($con);
?>
[/php]
<form action="updated.php" method="post">
<table border=0 cellpadding=2 cellspacing=2 width="100%">
<tr>
<td colspan=4 bgcolor="red"><font size=+2 color=#ffffff>Edit Contact Information</font></td>
</tr>
<tr>
<td width="50%" align=right>CID: <input type="text" name="up_CID" value="<? echo $CID; ?>" size="35"><br></td>
<td width="20%"></td>
</tr>
<tr>
<td width="50%" align=right>First Name: <input type="text" name="ud_fname" value="<? echo $fname; ?>" size="35"><br></td>
<td width="20%"></td>
</tr>
<tr>
<td width="50%" align=right>Last Name: <input type="text" name="ud_lname" value="<? echo $lname; ?>" size="35"><br></td>
<td width="20%"></td>
</tr>
<tr>
<td width="50%" align=right>Home Number: <input type="text" name="ud_hphone" value="<? echo $hphone; ?>" size="35"><br></td>
<td width="20%"></td>
</tr>
<tr>
<td width="50%" align=right>Work Number: <input type="text" name="ud_bphone" value="<? echo $bphone; ?>" size="35"><br></td>
<td width="20%"></td>
</tr>
<tr>
<td width="50%" align=right>Cell Number: <input type="text" name="ud_cphone" value="<? echo $cphone; ?>" size="35"><br></td>
<td width="20%"></td>
</tr>
<tr>
<td width="50%" align=right>E-mail Address: <input type="text" name="ud_email" value="<? echo $email; ?>" size="35"><br></td>
<td width="20%"></td>
</tr>
<tr>
<td width="50%" valign=top align=right>Address: <textarea name="ud_address" value="<? echo $address; ?>" rows="5" cols="27" wrap=virtual></textarea><br><font size=2 color=#000099>or notes</font></td><br>
<td width="20%"></td>
</tr>
<tr>
<td colspan=4 bgcolor="red"><font size=+2 color=#ffffff>Submit New Information</font></td>
</tr>
<tr>
<td><p valign=left><font face="Arial,Helvetica,Geneva,Sans-serif,sans-serif" size=-2 color=gray><b>M.Fazil Baksh<br></b>207-161 Augusta St 207<br>Ottawa, Ontario, K1N 8B6<br>613-695-0734<br></font></p></td>
</tr>
<!----printing the form----->
<tr>
<td width="50%" align=right><input type="submit" value="SUBMIT"></td>
</tr>
</table>
</form>
echo $CID;
<hr></hr>
</body>
</html>
PLEASE HELP ME TO GET THE SELECTED ROW OF DATA BACK INTO A FORM FOR EDITING/UPDATING.
I REALLY NEED SOME HELP
THANKS,
Fazil Baksh: email: fazilbaksh@rogers.com
Last edited by hummingbird; 02-02-2009 at 10:40 PM..
|
|
|
|
02-03-2009, 12:05 AM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 203
Name: Andy
Location: N.Ireland
|
Hi there,
a lot of your code was wrong and your get was echoed out...
try this, just change the mysql login details, to get the entry you'll need to call it via your url so for example scriptname.php?cid=23
If you have any problems, or errors, post them here :P It's like 11pm so i'm tired, it may not work lol
PHP Code:
<?
///////////////////////// // MySQL LOGIN DETAILS // /////////////////////////
$dbuser="username"; //your username $dbpass="password"; //your password
////////////////////////// // MySQL SERVER DETAILS // //////////////////////////
$dbname="dbname"; //name of your database $dbhost="localhost"; //address to your db $dbport="3306"; //3306 is default
<?php
$CID = $_GET['CID'];
//////////////////////////////////////////// ////////////////SAVE DATA////////////////// //////////////////////////////////////////
// if we click post if(isset($_POST['add'])) {
//we open a connection $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); @mysql_select_db($dbname) or die ('Can\'t find database'); // then get data from the form $fname = $_POST['fname']; $lname = $_POST['lname']; $hphone = $_POST['hphone']; $bphone = $_POST['bphone']; $cphone = $_POST['cphone']; $email = $_POST['email']; // replace all ' with \' so mysql doesn't puke $bad = array("'","%"); $good = array("´","%"); $fname = str_replace($bad, $good, $fname); $lname= str_replace($bad, $good, $lname);
// update the db $query = "UPDATE dbname SET fname = '$fname', lname = '$lname', hphone = '$hphone', bphone = '$bphone', cphone = '$cphone', email = '$email', address = '$address' WHERE cid = '$cid'"; mysql_query($query) or die(mysql_error());
// then we close the connection mysql_close($conn);
// and finish with a nice message echo "
<b>$fname</b>'s entry on the database was edited.<br /><br /> "; } else {
//////////////////////////////////////////// ////////////////GET DATA/////////////////// //////////////////////////////////////////
// we open a connection $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); @mysql_select_db($dbname) or die ('Can\'t find database'); // select the data from the db $query = "SELECT * FROM yourdbname WHERE cid = $cid"; $result = mysql_query($query);
// then close the connection mysql_close($conn);
// prepare the data to be place in the forms while($row = mysql_fetch_assoc($result)) { $fname = "{$row['fname']}"; $lname = "{$row['lname']}"; $hphone = "{$row['hphone']}"; $bphone = "{$row['bphone']}"; $cphone = "{$row['cphone']}"; $email = "{$row['email']}"; $address = "{$row['address']}"; } ?>
Edit Entry
<form method="post" />
<table border=0 cellpadding=2 cellspacing=2 width="100%"> <tr> <td colspan=4 bgcolor="red"><font size=+2 color=#ffffff>Edit Contact Information</font></td> </tr> <tr> <td width="50%" align=right>CID: <input type="text" name="CID" value="<? echo $CID; ?>" size="35"><br></td> <td width="20%"></td> </tr> <tr> <td width="50%" align=right>First Name: <input type="text" name="fname" value="<? echo $fname; ?>" size="35"><br></td> <td width="20%"></td> </tr> <tr> <td width="50%" align=right>Last Name: <input type="text" name="lname" value="<? echo $lname; ?>" size="35"><br></td> <td width="20%"></td> </tr> <tr> <td width="50%" align=right>Home Number: <input type="text" name="hphone" value="<? echo $hphone; ?>" size="35"><br></td> <td width="20%"></td> </tr> <tr> <td width="50%" align=right>Work Number: <input type="text" name="bphone" value="<? echo $bphone; ?>" size="35"><br></td> <td width="20%"></td> </tr> <tr> <td width="50%" align=right>Cell Number: <input type="text" name="cphone" value="<? echo $cphone; ?>" size="35"><br></td> <td width="20%"></td> </tr> <tr> <td width="50%" align=right>E-mail Address: <input type="text" name="email" value="<? echo $email; ?>" size="35"><br></td> <td width="20%"></td> </tr> <tr> <td width="50%" valign=top align=right>Address: <textarea name="address" value="<? echo $address; ?>" rows="5" cols="27" wrap=virtual></textarea><br><font size=2 color=#000099>or notes</font></td><br> <td width="20%"></td> </tr> <tr> <td colspan=4 bgcolor="red"><font size=+2 color=#ffffff>Submit New Information</font></td> </tr> <tr> <td><p valign=left><font face="Arial,Helvetica,Geneva,Sans-serif,sans-serif" size=-2 color=gray><b>M.Fazil Baksh<br></b>207-161 Augusta St 207<br>Ottawa, Ontario, K1N 8B6<br>613-695-0734<br></font></p></td> </tr> <!----printing the form-----> <tr> <td width="50%" align=right><input type="submit" value="SUBMIT"></td> </tr> </table> </form>
<?php } ?>
|
|
|
|
02-03-2009, 11:03 AM
|
Re: How do i display a record of MySQL database in order to edit it?
|
Posts: 51
|
The methods everyone is taking about is posting a variable to a PHP script and then rendering the entire page again.
Seems to be a bit of a waste, especially with all the free AJAX libraries out there. If you are only updating one record, then why not just post the changes using xmlhttprequest and then upon confirmation just update that one row.
the only changes you would need is for each row, give it it's own unique ID and then update that one row when finished.
There will be less bandwidth used, less/no user interruption, and it is a lot simpler than you think.
Check out JQuery... very easy to use.
|
|
|
|
|
« Reply to How do i display a record of MySQL database in order to edit it?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|