coupe-r Posted October 11, 2010 Share Posted October 11, 2010 Yea, just take your PHP and paste it to the top of the submit.php page. Then, on your form, change your action. action="" Before your PHP add this if(isset($_POST['submit_btn'])) { PHP GOES HERE } change submit_btn to whatever name your button is. This check to see if the button is pressed and if so, execute the code. Quote Link to comment Share on other sites More sharing options...
josuenerd Posted October 11, 2010 Author Share Posted October 11, 2010 I think I did something wrong. I tried to follow instructions but now it just shows this submit form and redirects back to the submit form. submit.php <?php if(isset($_POST['submit_button'])) { $con = mysql_connect("localhost","trend_learnu","asdfasdf"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("trend_learningdb", $con); $sql="INSERT INTO Peoples (Firstname, Lastname, Weight, Date) VALUES ('$_POST[Firstname]','$_POST[Lastname]','$_POST[Weight]','$_POST[Date]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; $db = mysql_connect("localhost","trend_learnu","asdfasdf"); mysql_select_db("trend_learningdb",$db); $result = mysql_query("SELECT * FROM Peoples ORDER BY number ASC"); $table = '<table width="608" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td width="207" bgcolor="#CCCCCC">Firstname</td> <td width="204" bgcolor="#CCCCCC">Lastname</td> <td width="197" bgcolor="#CCCCCC">Weight</td> <td width="197" bgcolor="#CCCCCC">Date</td> </tr>'; while($row=mysql_fetch_array($result)) { $firstname = $row['Firstname']; $lastname = $row['Lastname']; $weight = $row['Weight']; $date = $row['Date']; $table.= '<tr>'; $table.= '<td>'.$firstname.'</td>'; $table.= '<td>'.$lastname.'</td>'; $table.= '<td>'.$weight.'</td>'; $table.= '<td>'.$date.'</td>'; $table.= '</tr>'; } $table .= '</table>'; echo $table; mysql_close($con) ?> <html> <body> <?php $today = date("F j, Y"); } ?> <form action="" method="post"> <input name="Date" type="hidden" value="<?php PRINT "$today"; ?>" /> First Name: <input type="text" name="Firstname" /><br><br> Last Name: <input type="text" name="Lastname" /><br><br> Weight: <input type="text" name="Weight" /><br><br> <input name="button" type="submit" value="Submit Profile" /> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
coupe-r Posted October 12, 2010 Share Posted October 12, 2010 Try something like that. Remember, I can't test this. <?php $con = mysql_connect("localhost","trend_learnu","asdfasdf"); if (!$con) {die('Could not connect: ' . mysql_error());} mysql_select_db("trend_learningdb", $con); if(!mysql_query($sql,$con)) {die('Error: ' . mysql_error());} $db = mysql_connect("localhost","trend_learnu","asdfasdf"); mysql_select_db("trend_learningdb",$db); $result = mysql_query("SELECT * FROM Peoples ORDER BY number ASC"); $table = '<table width="608" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td width="207" bgcolor="#CCCCCC">Firstname</td> <td width="204" bgcolor="#CCCCCC">Lastname</td> <td width="197" bgcolor="#CCCCCC">Weight</td> <td width="197" bgcolor="#CCCCCC">Date</td> </tr>'; while($row=mysql_fetch_array($result)) { $firstname = $row['Firstname']; $lastname = $row['Lastname']; $weight = $row['Weight']; $date = $row['Date']; $table.= '<tr>'; $table.= '<td>'.$firstname.'</td>'; $table.= '<td>'.$lastname.'</td>'; $table.= '<td>'.$weight.'</td>'; $table.= '<td>'.$date.'</td>'; $table.= '</tr>'; } $table .= '</table>'; if(isset($_POST['submit_btn'])) { $firstname = $_POST['Firstname']; $lastname = $_POST['Lastname']; $weight = $_POST['Weigh']; mysql_query("INSERT INTO Peoples VALUES('$_POST[Firstname]','$_POST[Lastname]','$_POST[Weight]','$_POST[Date]')"); } ?> <!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=utf-8" /> <title>Untitled Document</title> </head> <body> <form action="" method="post"> <input name="Date" type="hidden" value="<?php $today; ?>" /> First Name: <input type="text" name="Firstname" /><br><br> Last Name: <input type="text" name="Lastname" /><br><br> Weight: <input type="text" name="Weight" /><br><br> <input name="submit_btn" type="submit" value="Submit Profile" id="submit_btn" /> </form> <p> </p> <?php if(isset($table)) {echo $table;} ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
josuenerd Posted October 12, 2010 Author Share Posted October 12, 2010 Tried it but it just shows this: Error: Query was empty Quote Link to comment Share on other sites More sharing options...
coupe-r Posted October 12, 2010 Share Posted October 12, 2010 take out if (!$con) {die('Could not connect: ' . mysql_error());} Quote Link to comment Share on other sites More sharing options...
josuenerd Posted October 12, 2010 Author Share Posted October 12, 2010 I got the same error. Not sure what's happening Quote Link to comment Share on other sites More sharing options...
josuenerd Posted October 12, 2010 Author Share Posted October 12, 2010 Below is the code I am using. The forum is under the tables how i wanted. All I want to do now is make it so that the form does not submit if (Weight) is left blank. Maybe showing a error message. This should stop some spam. insert.php <!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=utf-8" /> <title>Weight Log</title> </head> <body> <?php $con = mysql_connect("localhost","learnu","asdfasdf"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("learning", $con); $sql="INSERT INTO Peoples (Firstname, Lastname, Weight, Date) VALUES ('$_POST[Firstname]','$_POST[Lastname]','$_POST[Weight]','$_POST[Date]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; $db = mysql_connect("localhost","learnu","asdfasdf"); mysql_select_db("learning",$db); $result = mysql_query("SELECT * FROM Peoples ORDER BY number ASC"); $table = '<table width="608" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td width="207" bgcolor="#CCCCCC">Firstname</td> <td width="204" bgcolor="#CCCCCC">Lastname</td> <td width="197" bgcolor="#CCCCCC">Weight</td> <td width="197" bgcolor="#CCCCCC">Date</td> </tr>'; while($row=mysql_fetch_array($result)) { $firstname = $row['Firstname']; $lastname = $row['Lastname']; $weight = $row['Weight']; $date = $row['Date']; $table.= '<tr>'; $table.= '<td>'.$firstname.'</td>'; $table.= '<td>'.$lastname.'</td>'; $table.= '<td>'.$weight.'</td>'; $table.= '<td>'.$date.'</td>'; $table.= '</tr>'; } $table .= '</table>'; echo $table; mysql_close($con) ?> <?php $today = date("F j, Y"); ?> <table width="500" border="0" align="center" cellpadding="10" cellspacing="0"> <tr> <td> <form action="insert.php" method="post"> <input name="Date" type="hidden" value="<?php PRINT "$today"; ?>" /> First Name: <input type="text" name="Firstname" /><br><br> Last Name: <input type="text" name="Lastname" /><br><br> Weight: <input type="text" name="Weight" /><br><br> <input type="submit" value="Submit Profile" /> </form> </td> </tr> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
josuenerd Posted October 12, 2010 Author Share Posted October 12, 2010 I figured it out on a different thread Thanks to everyone here also for helping me get to where I am on my script. 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.