masteroleary Posted August 21, 2006 Share Posted August 21, 2006 Hello everyone. I'm stuck. I'm new to php and have setup an html form to read and change data within a mysql database. Instead of sending the data to another php page, I simply want to collect it from the html form and update the database. In the beginning of the first php tag you will find a line of code I commented out that I am having a problem with (mysql_query($submitQuery) or die(mysql_error());) This line is the last statement in the mySubmit function which is to be called when the form is submitted (<form method="post" onSubmit="<?php mySubmit(); ?>">). I am having trouble figuring out which of these two are giving me such a problem. You can see this page at www.matthew-oleary.com/form.php.form.php loads results.php(this document) into an i frame and sends it the development table and lot # to use to collect its data. I know there is no problem there. Everthing was working until I tried to UPDATE the database and again im not sure if the onSubmit event is flawed but the line commented out always makes this page appear blank. HELP!?![code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Results</title><style type="text/css"><!--body { margin: 0px; vertical-align: top; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: .8em;}--></style></head><body><?php $dbh = mysql_connect("localhost", "myuser", "mypass") or die ('Cannot connect to database : ' . mysql_error()); mysql_select_db ("creator_ionianconstruction") or die('Could not select database'); // // HERE IS WHERE I WROTE A FUNCTION TO BE CALLED WHEN THE FORM IS SUBMITTED, ITS PURPOSE IS TO CHANGE THE DATABASE function mySubmit(){ $submitQuery = "UPDATE $devNameTable SET modelNum=".$_POST['modelRadio'].", statusNum=".$_POST['statusRadio']." WHERE lotID = ".$_GET['lot']; // THIS LINE (WHEN NOT COMMENTED OUT) CAUSES NOTHING TO APPEAR IN THE FRAME //mysql_query($submitQuery) or die(mysql_error()); } // $devQuery = "SELECT devName FROM developmentData WHERE devID =". $_GET['dev']; $devResult = mysql_query($devQuery) or die(mysql_error()); $devName = mysql_fetch_array($devResult); $devNameTable = "dev".$devName['devName']."Data"; // $modelQuery = "SELECT modelID,modelName FROM modelData"; $modelResult = mysql_query($modelQuery) or die(mysql_error()); // $statusQuery = "SELECT statusID,statusName FROM statusData"; $statusResult = mysql_query($statusQuery) or die(mysql_error()); // $query = "SELECT modelNum,statusNum FROM $devNameTable WHERE lotID = ".$_GET['lot']; $result = mysql_query($query) or die(mysql_error()); $myResults = mysql_fetch_array($result); //// THE FORM ELEMENT BELOW USES THE ONSUBMIT EVENT HANDLER TO CALL THE PHP FUNCTION?><form method="post" onSubmit="<?php mySubmit(); ?>"><table width="100%" border="1"> <tr><td>Status: <?php while($myStatusResults = mysql_fetch_array($statusResult)) { if($myStatusResults['statusID'] == $myResults['statusNum']) { echo " <input name=\"statusRadio\" type=\"radio\" value=\"".$myStatusResults['statusID']."\" checked / >\n"; echo $myStatusResults['statusName']."\n"; } else{ echo " <input name=\"statusRadio\" type=\"radio\" value=\"".$myStatusResults['statusID']."\" />\n"; echo $myStatusResults['statusName']."\n"; } } echo "</td><td> </td></tr><tr><td>Model: \n"; while($myModelResults = mysql_fetch_array($modelResult)) { if($myModelResults['modelID'] == $myResults['modelNum']) { echo " <input name=\"modelRadio\" type=\"radio\" value=\"".$myModelResults['modelID']."\" checked / >\n"; echo $myModelResults['modelName']."\n"; } else{ echo " <input name=\"modelRadio\" type=\"radio\" value=\"".$myModelResults['modelID']."\" />\n"; echo $myModelResults['modelName']."\n"; } }?></td><td><input name="Make Changes" type="submit" class="button" value="Submit" /></td></tr></table></form></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
masteroleary Posted August 21, 2006 Author Share Posted August 21, 2006 I'm not sure what i'm doing wrong here. This is my latest attempt to submit changes to a mySQL database. In the following code I simple put php inside the onSubmit form event handler. But again, nothing appears on the page when I add this line <form method="post" onSubmit="<?php mysql_query("UPDATE $devNameTable SET modelNum=".$_POST['modelRadio'].", statusNum=".$_POST['statusRadio']." WHERE lotID = ".$_GET['lot']) or die(mysql_error()); ?>">No errors or anything. Ideas?[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Results</title><style type="text/css"><!--body { margin: 0px; vertical-align: top; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: .8em;}--></style></head><body><?php $dbh = mysql_connect("localhost", "myuser", "mypswd") or die ('Cannot connect to database : ' . mysql_error()); mysql_select_db ("creator_ionianconstruction") or die('Could not select database'); // $devQuery = "SELECT devName FROM developmentData WHERE devID =". $_GET['dev']; $devResult = mysql_query($devQuery) or die(mysql_error()); $devName = mysql_fetch_array($devResult); $devNameTable = "dev".$devName['devName']."Data"; // $modelQuery = "SELECT modelID,modelName FROM modelData"; $modelResult = mysql_query($modelQuery) or die(mysql_error()); // $statusQuery = "SELECT statusID,statusName FROM statusData"; $statusResult = mysql_query($statusQuery) or die(mysql_error()); // $query = "SELECT modelNum,statusNum FROM $devNameTable WHERE lotID = ".$_GET['lot']; $result = mysql_query($query) or die(mysql_error()); $myResults = mysql_fetch_array($result); //// THE FORM ELEMENT BELOW USES THE ONSUBMIT EVENT HANDLER TO CALL THE PHP FUNCTION?><form method="post" onSubmit="<?php mysql_query("UPDATE $devNameTable SET modelNum=".$_POST['modelRadio'].", statusNum=".$_POST['statusRadio']." WHERE lotID = ".$_GET['lot']) or die(mysql_error()); ?>"><table width="100%" border="1"> <tr><td>Status: <?php while($myStatusResults = mysql_fetch_array($statusResult)) { if($myStatusResults['statusID'] == $myResults['statusNum']) { echo " <input name=\"statusRadio\" type=\"radio\" value=\"".$myStatusResults['statusID']."\" checked / >\n"; echo $myStatusResults['statusName']."\n"; } else{ echo " <input name=\"statusRadio\" type=\"radio\" value=\"".$myStatusResults['statusID']."\" />\n"; echo $myStatusResults['statusName']."\n"; } } echo "</td><td> </td></tr><tr><td>Model: \n"; while($myModelResults = mysql_fetch_array($modelResult)) { if($myModelResults['modelID'] == $myResults['modelNum']) { echo " <input name=\"modelRadio\" type=\"radio\" value=\"".$myModelResults['modelID']."\" checked / >\n"; echo $myModelResults['modelName']."\n"; } else{ echo " <input name=\"modelRadio\" type=\"radio\" value=\"".$myModelResults['modelID']."\" />\n"; echo $myModelResults['modelName']."\n"; } }?></td><td><input name="Make Changes" type="submit" class="button" value="Submit" /></td></tr></table></form></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 22, 2006 Share Posted August 22, 2006 You can't call a serverside script once a file has been served in way you have attempted.The onWhatever event handlers provide a vehicle to initiate client side code i.e. javascript.you can however use ajax to let client side scripts call a serverside script to do the update for you.BUT in this case i see no reason why you cannot submit the script to the same page and let that do all the update. just set action="<?php echo $_SERVER['PHP_SELF']; ?>" and an if statement to check that $_POST array has sufficient info in it to complete the request and formulate a query string on that info. Quote Link to comment Share on other sites More sharing options...
masteroleary Posted August 22, 2006 Author Share Posted August 22, 2006 Ok I tried what you said. Now keep in mind that this form is to set the status of several different lots and models of many developments and so they can change two different radio button groups several times and hit submit more than once if they make a mistake or change their mind.At the bottom of the code are the if() statements that I use to check two things:[code]if($_POST['modelRadio'] && $_POST['modelRadio'] != $myModelResults['modelID'])[/code]First does the global variable modelRadio (statusRadio on the second if statement) exist and secondly is it different from modelID which is the data that will be updated. But when I hit submit it says to check the mysql manual for syntax. Any suggestions how to improve this script?[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Results</title><style type="text/css"><!--body { margin: 0px; vertical-align: top; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: .8em;}--></style></head><body><?php $dbh = mysql_connect("localhost", "creator_ionian", "construction") or die ('Cannot connect to database : ' . mysql_error()); mysql_select_db ("creator_ionianconstruction") or die('Could not select database'); // $devQuery = "SELECT devName FROM developmentData WHERE devID =". $_GET['dev']; $devResult = mysql_query($devQuery) or die(mysql_error()); $devName = mysql_fetch_array($devResult); $devNameTable = "dev".$devName['devName']."Data"; // $modelQuery = "SELECT modelID,modelName FROM modelData"; $modelResult = mysql_query($modelQuery) or die(mysql_error()); // $statusQuery = "SELECT statusID,statusName FROM statusData"; $statusResult = mysql_query($statusQuery) or die(mysql_error()); // $query = "SELECT modelNum,statusNum FROM $devNameTable WHERE lotID = ".$_GET['lot']; $result = mysql_query($query) or die(mysql_error()); $myResults = mysql_fetch_array($result); //?><form method="post" action="results.php"><input name="lot" type="hidden" value="<?php $_GET['lot']; ?>"<table width="100%" border="1"> <tr><td>Status: <?php while($myStatusResults = mysql_fetch_array($statusResult)) { if($myStatusResults['statusID'] == $myResults['statusNum']) { echo " <input name=\"statusRadio\" type=\"radio\" value=\"".$myStatusResults['statusID']."\" checked / >\n"; echo $myStatusResults['statusName']."\n"; } else{ echo " <input name=\"statusRadio\" type=\"radio\" value=\"".$myStatusResults['statusID']."\" />\n"; echo $myStatusResults['statusName']."\n"; } } echo "</td><td> </td></tr><tr><td>Model: \n"; while($myModelResults = mysql_fetch_array($modelResult)) { if($myModelResults['modelID'] == $myResults['modelNum']) { echo " <input name=\"modelRadio\" type=\"radio\" value=\"".$myModelResults['modelID']."\" checked / >\n"; echo $myModelResults['modelName']."\n"; } else{ echo " <input name=\"modelRadio\" type=\"radio\" value=\"".$myModelResults['modelID']."\" />\n"; echo $myModelResults['modelName']."\n"; } } if($_POST['modelRadio'] && $_POST['modelRadio'] != $myModelResults['modelID']) { $submitQuery = "UPDATE $devNameTable SET modelNum=".$_POST['modelRadio']." WHERE lotID = ".$_POST['lot']; $submitResult = mysql_query($submitQuery) or die(mysql_error()); } if($_POST['statusRadio'] && $_POST['statusRadio'] != $myStatusResults['statusID']) { $submitQuery = "UPDATE $devNameTable SET statusNum=".$_POST['statusRadio']." WHERE lotID = ".$_POST['lot']; $submitResult = mysql_query($submitQuery) or die(mysql_error()); }?></td><td><input name="Make Changes" type="submit" class="button" value="Submit" /></td></tr></table></form></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 22, 2006 Share Posted August 22, 2006 Well - the way you have laid it out I can see lots of problems with your radio button values being detected properly - that is because they all have the same name; So each project listed will not have a unique name given to the inputs that relate to that project - I suggest that you append the project id to the name of their respective radio buttons OR anything that can discriminate between projects in yoru script... Quote Link to comment Share on other sites More sharing options...
masteroleary Posted August 22, 2006 Author Share Posted August 22, 2006 Actually there are two radio button groups named modelRadio and statusRadio so when the form submits it should submit the name of the group with the value of the one checked. I dont think there is a problem there. I am getting stuck when it comes to the if statement. www.matthew-oleary.com/form.php will show a page that loads this page into an iframe. The current database values are reflected by the pre selected radio buttons. The problem is that when I select a different radio button and hit submit the if() statements at the end dont do what they are supposed to and I dont know why. 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.