gb75 Posted March 23, 2009 Share Posted March 23, 2009 Hi I'm a newbie to php and mysql and i'm having trouble with a query. Here's the issue First of all I have a form which when completed updates a table. Then I have a database page which first runs a SELECT query to retrieve the results of the form... What I need to do is then have the ability to update the retrieved form but this time I need to have an UPDATE query embedded into the code to update the table. So therefore a SELECT and UPDATE query in the same piece of code. Any help greatly appreciated.....Gary Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 23, 2009 Share Posted March 23, 2009 if i understand correctly, the form is used to pull information from the database, then you can update the information and submit it back into the database. if that is the case, when selecting the information to edit, also pull out the ID of the entry. when the information is changed and you want to put it back into the DB with the new values, just update the entry with that same ID with the new values Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted March 23, 2009 Share Posted March 23, 2009 something like this for an idea <?php $action= $_GET["action"]; if($action=="") { $action = "add"; } if ($action=="update") { retieve info from database etc sql="select name, age from table etc $id = $row["id"]; $name =$row["firstname"; } ?> <html> <head> </head> etc etc <body> <form name="form" method="post" action="submitform.php"> <input type="hidden" name="action" value="<?=$action?>" /> <input type="hidden" name="id" value="<?=$id?>" /> <input type="text" name="firstname" value="<?=$firstname?>" /> <input type="submit" name="submit" value="submit" /> </form> <a href="samepagenameasform.php?action=update">Update</a> </body> </html> //submitform.php <? $action= $_POST["action"]; $id= $_POST["id"]; $firstname = $_POST["firstname"]; if ($action=="add") { sql ="insert into table etc"; } if ($action=="update") { update tablename set firstname=firstname where id =$id"; etc etc } Quote Link to comment Share on other sites More sharing options...
gb75 Posted March 23, 2009 Author Share Posted March 23, 2009 Hi Shadiadiph & Lonewolf217 Thanks for the quick responses - Yes, the form is used to pull information from the database, then I update the information and submit it back into the database. I have tried to adapt the piece of code Shadiadiph posted but the 1st SELECT retrieves a blank (there is an entry in the table ) I have posted the updated code below $conn = mysql_connect("$location","$username","$password"); if (!$conn) die ("Could not connect MySQL"); mysql_select_db($database,$conn) or die ("Could not open database"); $action= $_GET["action"]; if($action=="") { $action = "add"; } if ($action=="update") { $query = "SELECT genid, requestorname FROM request_detail_t WHERE genid='16'"; $genid = $row["genid"]; $RequestorName =$row["RequestorName"]; } echo ' <form method="post"> <table width="600" border="0" cellpadding="1"> <tr> <td width="165">Identifier:</td> <td><input name="genid" type="text" size="52" value="' . $row['genid'] . '"></td> </tr> <tr> <td width="165">Requestor Name:</td> <td><input name="RequestorName" type="text" size="52" value="' . $row['RequestorName'] . '"></td> </tr> <tr> <td><input name="edit" type="submit" id="add" class="button" value="edit"></td> </tr> </form>'; $action= $_POST["action"]; $genid= $_POST["genid"]; $RequestorName = $_POST["RequestorName"]; if ($action=="update") { $query = "UPDATE request_detail_t SET genid ='$genid', RequestorName='$RequestorName' WHERE genid='$genid'"; } ?> Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted March 23, 2009 Share Posted March 23, 2009 try this i thought you wanted to insert or update but this will only get where genid-16 $conn = mysql_connect("$location","$username","$password"); if (!$conn) die ("Could not connect MySQL"); mysql_select_db($database,$conn) or die ("Could not open database"); $query = "SELECT genid, requestorname FROM request_detail_t WHERE genid='16'"; $genid = $row["genid"]; $RequestorName =$row["RequestorName"]; echo ' <form method="post" action="$php_self"> <table width="600" border="0" cellpadding="1"> <tr> <td width="165">Identifier:</td> <td><input name="genid" type="text" size="52" value="'.$genid.'"></td> </tr> <tr> <td width="165">Requestor Name:</td> <td><input name="RequestorName" type="text" size="52" value="'.$RequestorName.'"></td> </tr> <tr> <td><input name="submit" type="submit" class="button" value="edit"></td> </tr> </form>'; $genid= $_POST["genid"]; $RequestorName = $_POST["RequestorName"]; $update = "UPDATE request_detail_t SET genid ='$genid', RequestorName='$RequestorName' WHERE genid='$genid'"; $result = mysql_query($update); print "Updated"; ?> Quote Link to comment Share on other sites More sharing options...
gb75 Posted March 23, 2009 Author Share Posted March 23, 2009 Thanks Shadiadiph, I now have it working. Where the genid is specific was just to test, this will be a variable passed from the previous page where moved into the new page. Quote Link to comment Share on other sites More sharing options...
gb75 Posted March 23, 2009 Author Share Posted March 23, 2009 Sorry Shadiadiph, one more question - When the submit button is pressed to make the update, is there a way to point it back to the URL of the database. So - on submit go back to a URL Thanks Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 23, 2009 Share Posted March 23, 2009 what URL are you talking about ? if you want to redirect the user to another page upon submission then you need to use the header() function. be careful how you use it though, remember that there can be NO output on your page prior to executing a header redirect Quote Link to comment Share on other sites More sharing options...
gb75 Posted March 23, 2009 Author Share Posted March 23, 2009 Yes, I want to redirect the user to another page upon submission So basically once the update has been entered in the form, the submit button will update the database, but also at the same time it will take the users back to the database page (in this case crd_database.php) Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 23, 2009 Share Posted March 23, 2009 what you will need to do then is have any error checking done at the top of your page (without echo'ing anything to the page on success). Then you can do your submission to the database. then you have your redirect header("Location crd_database.php"); Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted March 23, 2009 Share Posted March 23, 2009 basically like this $conn = mysql_connect("$location","$username","$password"); if (!$conn) die ("Could not connect MySQL"); mysql_select_db($database,$conn) or die ("Could not open database"); $query = "SELECT genid, requestorname FROM request_detail_t WHERE genid='16'"; $genid = $row["genid"]; $RequestorName =$row["RequestorName"]; echo ' <form method="post" action="$php_self"> <table width="600" border="0" cellpadding="1"> <tr> <td width="165">Identifier:</td> <td><input name="genid" type="text" size="52" value="'.$genid.'"></td> </tr> <tr> <td width="165">Requestor Name:</td> <td><input name="RequestorName" type="text" size="52" value="'.$RequestorName.'"></td> </tr> <tr> <td><input name="submit" type="submit" class="button" value="edit"></td> </tr> </form>'; $genid= $_POST["genid"]; $RequestorName = $_POST["RequestorName"]; $update = "UPDATE request_detail_t SET genid ='$genid', RequestorName='$RequestorName' WHERE genid='$genid'"; $result = mysql_query($update); header("Location: crd_database.php"); exit; ?> Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 23, 2009 Share Posted March 23, 2009 no, that wont work because you have the header after it outputs the form Quote Link to comment Share on other sites More sharing options...
shadiadiph Posted March 23, 2009 Share Posted March 23, 2009 works on 100% of all update pages I have working right now Quote Link to comment Share on other sites More sharing options...
gb75 Posted March 24, 2009 Author Share Posted March 24, 2009 Thanks for your help yesterday however, I still have an issue. When coding the header("Location: crd_database.php"); after the SELECT it takes me straight to the crd_database page before the user has had a chance to update the fields from the SELECT. What I want is for the select to populate the form, then have the chance to update the form and then on the submit of the changes it updates the table again and goes off to the crd_database page. Coding the header("Location: crd_database.php"); at the end gives me an error - Warning: Cannot modify header information - headers already sent by etc etc Here is the current coding (apologies not sure how to block the code as per previous posts) $conn = mysql_connect("$location","$username","$password"); if (!$conn) die ("Could not connect MySQL"); mysql_select_db($database,$conn) or die ("Could not open database"); if (isset($_GET['id'])) { $id = $_GET['id']; $tor = $_GET['tor']; } $query = "SELECT genid, RequestorName FROM request_detail_t WHERE genid='$id'"; //$genid = $row["genid"]; //$RequestorName =$row["RequestorName"]; header("Location: crd_database.php"); echo ' <form method="post" action="'.$_SERVER["PHP_SELF"].'" /> <table width="600" border="0" cellpadding="1"> <tr> <td width="165">Identifier:</td> <td><input name="genid" type="text" size="52" value="'.$id.'"></td> </tr> <tr> <td width="165">Requestor Name:</td> <td><input name="RequestorName" type="text" size="52" value="'.$tor.'"></td> </tr> <tr> <td width="100"> </td> <td> </td> </tr> <tr> <td width="100"> </td> <td><input name="update" type="submit" id="add" class="button" value="edit" ></td> </tr> </form>'; $genid= $_POST["genid"]; $RequestorName = $_POST["RequestorName"]; $update = "UPDATE request_detail_t SET genid ='$genid', RequestorName='$RequestorName' WHERE genid='$genid'"; $result = mysql_query($update) or die('Error, update query failed'); ?> Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 24, 2009 Share Posted March 24, 2009 because you dont have any conditional around the header. you need to have some way of saying this (pseudo code) if (user submitted form) { verify contents; if (contents verified) { header redirect; } } for example, change your form to something like this <form method="post" action="'.$_SERVER["PHP_SELF"]."?action=Submit".'" /> then you can have this at the top of your page if(isset($_GET['action']) && $_GET['action']=='Submit') { verify the form contents; submit to database; header("Location: crd_database.php"); } Quote Link to comment Share on other sites More sharing options...
gb75 Posted March 24, 2009 Author Share Posted March 24, 2009 wow, now we are getting in there - this is all new for me. Can you explain and maybe provide an example of how to verify the contents of the form and the submit to the database. Thanks 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.