dadamssg Posted January 18, 2009 Share Posted January 18, 2009 Ok..My objective is to provide a simple form....im trying to learn php....where the user can fill out three fields. My theory is to display the form...have them enter the information, when they do this my switch ($_POST['do']) case "check": will display a table with their entered information for them to check over before submitting. This table will also have a submit button witch should activate the case "post": which will submit the data to the database. Problem...it will display the form, but when i press the button it basically refreshes the form with no info, and no table for them to check...and no data in my db....i feel like im close but im not sure if the "switch" function is the right one to use. <?php /* File: processtest.php*/ include("caneck.inc"); switch (@$_POST['do']) { case "check": $title = $_POST['title']; #10 $description = $_POST['description']; $event = $_POST['event']; Print "<table border='7'> <tr> <td><b>Title: </b></td><td>$title</td> </tr> #20 <tr> <td><b>Description: </b></td><td>$description</td> </tr> <tr> <td><b>Event Type: </b></td><td>$event</td> </tr> <tr> <td colspan=2><center><input type='submit' name='do' value='post'> <input type='hidden' name='do' value='post'></center> </td> #30 </tr> </table>"; break; case "post": $title = $_POST['title']; #10 $description = $_POST['description']; $event = $_POST['event']; $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die("Couldn't connect"); #40 $sql = "INSERT INTO test (title, description, event) VALUES ('$title','$description','$event')"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query."); include("testdisplay.inc"); break; #50 default: include("testdisplay.inc"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141302-my-theory-isnt-working/ Share on other sites More sharing options...
lokie538 Posted January 18, 2009 Share Posted January 18, 2009 how about a simple, if(isset($_POST['do'])) { /// display table and such } Quote Link to comment https://forums.phpfreaks.com/topic/141302-my-theory-isnt-working/#findComment-739579 Share on other sites More sharing options...
dadamssg Posted January 18, 2009 Author Share Posted January 18, 2009 ok heres what i got now..using the ifset function...it displays the form..i enter...and it just sends the info straight to my database...no table for me to check and then submit. any suggestions? <?php /* File: processtest.php*/ include("caneck.inc"); if(isset($_POST['check'])) { $title = $_POST['title']; #10 $description = $_POST['description']; $event = $_POST['event']; Print "<table border='7'> <tr> <td><b>Title: </b></td><td>$title</td> </tr> #20 <tr> <td><b>Description: </b></td><td>$description</td> </tr> <tr> <td><b>Event Type: </b></td><td>$event</td> </tr> <tr> <td colspan=2><center><form action='process.php method='POST'><input type='submit' name='post' value='post'> <input type='hidden' name='do' value='post'></center></form> </td> #30 </tr> </table>"; } if(isset($_POST['post'])) { $title = $_POST['title']; #10 $description = $_POST['description']; $event = $_POST['event']; $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die("Couldn't connect"); #40 $sql = "INSERT INTO test (title, description, event) VALUES ('$title','$description','$event')"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query."); include("testdisplay.inc"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141302-my-theory-isnt-working/#findComment-739591 Share on other sites More sharing options...
Daniel0 Posted January 18, 2009 Share Posted January 18, 2009 You are only displaying the form if $_POST['do'] is set in your script. You need to do "if $_POST['do'] is set then insert stuff, else display the form". You'll also want to escape the values from the user prior to using them in your query. In your case you should use the function called mysqli_real_escape_string. Quote Link to comment https://forums.phpfreaks.com/topic/141302-my-theory-isnt-working/#findComment-739627 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.