johnbruce Posted April 17, 2007 Share Posted April 17, 2007 Hi all, I've currently got a script which allows a user to search for a record in a mysql database and then returns the record into a html text field. What I'm wanting to do is allow the user to edit this record and submit it back to the database and update the record. The code i'm currently using is; <? $ud_id=$_GET['id']; $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password); $query="UPDATE gosscustomers SET title='$ud_title',first_name='$ud_first_name', surname='$ud_surname', company='$ud_company', address_1='$ud_address_1', address_2='$ud_address_2', address_3='$ud_address_3', postcode='$ud_postcode', country='$ud_country', email_address='$ud_email_address' WHERE id='$ud_id'"; mysql_query($query); echo "Record Updated"; mysql_close(); ?> The code from the page that shows the search results is here: <html> <style type="text/css"> <!-- .style1 { font-family: Arial, Helvetica, sans-serif; font-size: 10px; } --> </style> <? $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM gosscustomers WHERE id='$id'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "<b><center>Search Results</center></b><br><br>"; $i=0; while ($i < $num) { $id=mysql_result($result,$i,"id"); $title=mysql_result($result,$i,"title"); $first_name=mysql_result($result,$i,"first_name"); $surname=mysql_result($result,$i,"surname"); $company=mysql_result($result,$i,"company"); $address_1=mysql_result($result,$i,"address_1"); $address_2=mysql_result($result,$i,"address_2"); $address_3=mysql_result($result,$i,"address_3"); $postcode=mysql_result($result,$i,"postcode"); $country=mysql_result($result,$i,"country"); $email_address=mysql_result($result,$i,"email_address"); ?> <span class="style1"><br /> </span> <form id="form1" name="form1" method="post" action="update.php"> <span class="style1">Record ID:<br /> <input name="ud_id" type="text" id="ud_id" value="<?php echo $id; ?>" size="40" /> <br /> Title: <br /> <input name="ud_title" type="text" id="ud_title" value="<?php echo $title; ?>" size="40" /> <br /> First name: <br /> <input name="ud_first_name" type="text" id="ud_first_name" value="<?php echo $first_name; ?>" size="40" /> <br /> Surname: <br /> <input name="ud_surname" type="text" id="ud_surname" value="<?php echo $surname; ?>" size="40" /> <br /> Company: <br /> <input name="ud_company" type="text" id="ud_company" value="<?php echo $company; ?>" size="40" /> <br /> Address Line 1:<br /> <input name="ud_address_1" type="text" id="ud_address_1" value="<?php echo $address_1; ?>" size="40" /> <br /> Address Line 2: <br /> <input name="ud_address_2" type="text" id="ud_address_2" value="<?php echo $address_2; ?>" size="40" /> <br /> Address Line 3: <br /> <input name="ud_address_3" type="text" id="ud_address_3" value="<?php echo $address_3; ?>" size="40" /> <br /> Postcode: <br /> <input name="ud_postcode" type="text" id="ud_postcode" value="<?php echo $postcode; ?>" size="40" /> <br /> Country: <br /> <input name="ud_country" type="text" id="ud_country" value="<?php echo $country; ?>" size="40" /> <br /> E-Mail address:<br /> <input name="ud_email_address" type="text" id="ud_email_address" value="<?php echo $email_address; ?>" size="40" /> </span> <br /> <br /> <input type="submit" name="Submit" value="Update" /> </form> <span class="style1"><br /> <br /> <br /> <? $i++; } ?> </span> </html> Any help would be greatly appreciated! Thanks, Quote Link to comment Share on other sites More sharing options...
mendoz Posted April 17, 2007 Share Posted April 17, 2007 Can acyou establish a connection? if not you may want to use your queries like this: <?php $connection=mysql_connect(localhost,$username,$password); $query="UPDATE gosscustomers SET title='$ud_title' WHERE id='$ud_id'"; mysql_query($query,$connection); ?> Quote Link to comment Share on other sites More sharing options...
johnbruce Posted April 17, 2007 Author Share Posted April 17, 2007 I presume I can establish a connection, I can add and delete records using the script i've writtn but not update them and it's driving me crazy! I can't figure out whats wrong. Quote Link to comment Share on other sites More sharing options...
mendoz Posted April 17, 2007 Share Posted April 17, 2007 You are just using variables without giving them values. some thing like: <?php $ud_title = $_POST['ud_title']; ?> in here <?php $ud_id=$_GET['id']; $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password); $query="UPDATE gosscustomers SET title='$ud_title',first_name='$ud_first_name', surname='$ud_surname', company='$ud_company', address_1='$ud_address_1', address_2='$ud_address_2', address_3='$ud_address_3', postcode='$ud_postcode', country='$ud_country', email_address='$ud_email_address' WHERE id='$ud_id'"; mysql_query($query); echo "Record Updated"; mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
johnbruce Posted April 17, 2007 Author Share Posted April 17, 2007 Sorry, maybe i'm being stupid but i'm not sure what you mean? where do I use the $ud_title = $_POST['ud_title']; I'm just starting out with php/mysql as you can tell! Quote Link to comment Share on other sites More sharing options...
mendoz Posted April 17, 2007 Share Posted April 17, 2007 Remember when you used this: <?php $ud_id=$_GET['id']; ?> First of all, You should have used $_POST instead of $_GET $_GET is when you have something like this: mypage.php?id=3 so for every input in your form you should pass it's value to a variable example: <input name="id" value="3"> $id = $_POST["id"] example: <input name="mytext" > $mytext = $_POST["mytext"] You would like to get all the data which you just sent. Quote Link to comment Share on other sites More sharing options...
johnbruce Posted April 17, 2007 Author Share Posted April 17, 2007 So do I have to change the following code?? <input name="ud_id" type="text" id="ud_id" value="<?php echo $id; ?>" size="40" /> Quote Link to comment Share on other sites More sharing options...
johnbruce Posted April 17, 2007 Author Share Posted April 17, 2007 I've added this to the search.php page; <?php $_POST['ud_first_name']; ?> Will this post the value in the text field to the update.php page so the script can update the db?? Quote Link to comment Share on other sites More sharing options...
mendoz Posted April 17, 2007 Share Posted April 17, 2007 You use $_POST in the page where you receive the data from the form Quote Link to comment Share on other sites More sharing options...
johnbruce Posted April 18, 2007 Author Share Posted April 18, 2007 Grrr! Still nothing happening, I'm also still not gettting any error messages! I've attached the two files, if anyone could take a look and let me know why it's not writing back to the database i'd be very gratefull. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.