kevinritt Posted December 17, 2008 Share Posted December 17, 2008 I tried to solve this on my own but I can't seem to figure out what is wrong. I have a form to submit data into a database - when I submit the form, I do not get an error message but the data does not go into the database. Any help would be greatly appreciated. See code below ... <? require_once('./Connections/db.php'); ?> <html> <head> <title>Add to detention</title> </head> <body> <?php if ($_POST['submit']) { // data from form $fname = $_POST['fname']; $lname = $_POST['lname']; $date = $_POST['date']; $days = $_POST['days']; $reason = $_POST['reason']; $comments = $_POST['comments']; # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE $query ="INSERT INTO detention (detentionID, fname, lname, date, days, reason, comments) VALUES (NULL, '$fname', '$lname', '$date', '$days', '$reason', '$comments')"; $result = mysql_query($query) or die ('Data not entered'); echo 'All set'; } else { ?> <h3>Add a student to detention database:</h3> <p> </p> <form action="" method="POST" name="form1" id="form1"> <table align="center"> <tr valign="baseline"> <td nowrap="nowrap" align="right">First name:</td> <td><input type="text" name="fname" value="" size="32" /></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right">Last name:</td> <td><input type="text" name="lname" value="" size="32" /></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right">Date:</td> <td><input type="text" name="date" value="<?php echo date("Y-m-d"); ?>" size="32" /></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right" valign="top">Reason for detention:</td> <td><textarea name="reason" cols="50" rows="5"></textarea> </td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right">Number of assigned detention days:</td> <td><input type="text" name="days" value="" size="32" /></td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right" valign="top">Comments:</td> <td><textarea name="comments" cols="50" rows="5"></textarea> </td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right"> </td> <td><input type="submit" value="submit" /></td> </tr> </table> </form> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137370-need-help-please/ Share on other sites More sharing options...
.josh Posted December 17, 2008 Share Posted December 17, 2008 you didn't name your submit button so your condition never evaluates true. Quote Link to comment https://forums.phpfreaks.com/topic/137370-need-help-please/#findComment-717726 Share on other sites More sharing options...
kevinritt Posted December 17, 2008 Author Share Posted December 17, 2008 Thank you for the quick response. I had it in my head the 'value' would work. I get the dumbass award for today. Quote Link to comment https://forums.phpfreaks.com/topic/137370-need-help-please/#findComment-717731 Share on other sites More sharing options...
Brinn Posted December 17, 2008 Share Posted December 17, 2008 Try this. Instead of if ($_POST['submit']) try if ($_POST) that should work for you. Otherwise you have to name your submit button submit or have a hidden field in there called submit. Quote Link to comment https://forums.phpfreaks.com/topic/137370-need-help-please/#findComment-717734 Share on other sites More sharing options...
redarrow Posted December 17, 2008 Share Posted December 17, 2008 <td><input type="submit" value="submit" /></td> to <td><input type="submit" name="submit" value="submit" /></td> and it <?php if(isset($_POST['submit'])){ //code } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137370-need-help-please/#findComment-717739 Share on other sites More sharing options...
DimitriDV Posted December 17, 2008 Share Posted December 17, 2008 also change $query ="INSERT INTO detention (detentionID, fname, lname, date, days, reason, comments) VALUES (NULL, '$fname', '$lname', '$date', '$days', '$reason', '$comments')"; into $query ="INSERT INTO detention (fname, lname, date, days, reason, comments) VALUES ('$fname', '$lname', '$date', '$days', '$reason', '$comments')"; if your detentionID is primary key, autoincrement Quote Link to comment https://forums.phpfreaks.com/topic/137370-need-help-please/#findComment-717744 Share on other sites More sharing options...
kevinritt Posted December 17, 2008 Author Share Posted December 17, 2008 Thanks to all the replies. This has been a very big help Quote Link to comment https://forums.phpfreaks.com/topic/137370-need-help-please/#findComment-717825 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.