mishuk Posted February 14, 2007 Share Posted February 14, 2007 Hi. I am trying to create a script that encorporates self submission. When i run the script i am given the "add student form heading" but then nothing else why is this ? <?php if($_POST['submit'] == 'submit') { // Check all fields have been filled out if (!$_POST['forename'] || $_POST['forename'] == "") { $message = '<p> Please select a forname <p>'; } else { // connect to database $conn = mysql_connect("....", ".....", "......") or die(mysql_error()); mysql_select_db(".......", $conn) or die(mysql_error()); //assign variables to $_POST $forename =($_POST['forename']); // insert data into tbl_student $addstudent = "INSERT INTO tbl_student (forename) VALUES ('$forename')"; $result = mysql_query($addstudent) or die(mysql_error()); if (mysql_affected_rows() == 1) { $message = '<p>Entry added. Would you like to <a href=\"student.php\">add another</a></p>'; } else { error_log(mysql_error()); $message = '<p>Something went wrong</p>'; } } // show the form in every case except successful submission if (!$noform_var) { $thisfile = $_SERVER['PHP_SELF']; $message .= <<< EOMSG <FORM method = "post", action="$thisfile"> <input type="text" size="20" name="forename"> <input name="submit" type="submit" value="Add Student"> </FORM> EOMSG; } } ?> <html> <head> <style type="text/css"></style> </head> <body> <table border=0 cellpadding=10 width=100%> <TR> <TD><H1>Add Student Form</H1> <br><br> <?php echo $message; ?> </body> </html> Thanks Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 15, 2007 Share Posted February 15, 2007 I think you have a curly bracket in the wrong place...and where is $noform_var being set? Try this... <?php if($_POST['submit'] == 'submit') { if (!$_POST['forename'] || $_POST['forename'] == "") { $message = '<p> Please select a forname <p>'; } else { $conn = mysql_connect("....", ".....", "......") or die(mysql_error()); mysql_select_db(".......", $conn) or die(mysql_error()); $forename = ($_POST['forename']); $addstudent = "INSERT INTO tbl_student (forename) VALUES ('$forename')"; $result = mysql_query($addstudent) or die(mysql_error()); if (mysql_affected_rows() == 1) { $message = '<p>Entry added. Would you like to <a href=\"student.php\">add another</a></p>'; } else { error_log(mysql_error()); $message = '<p>Something went wrong</p>'; } } } else { $message .= ' <FORM method="post" action="' . $_SERVER['PHP_SELF'] . '"> <input type="text" size="20" name="forename"> <input name="submit" type="submit" value="Add Student"> </FORM>'; } ?> <html> <head> <style type="text/css"></style> </head> <body> <table border=0 cellpadding=10 width=100%> <TR> <TD><H1>Add Student Form</H1> <br><br> <?php echo $message; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
mishuk Posted February 15, 2007 Author Share Posted February 15, 2007 Cheers that seems to have done the trick. however I am not trying to do a drop down menu that selects from a database. It doesnt seem to work. I know its something to do with putting the code into the table however I am unsure how to fix it. Is it to do with quotes Thanks <?php if($_POST['submit'] == 'Add Student') { if (!$_POST['forename'] || $_POST['forename'] == "") { $message = '<p> Please select a forname <p>'; } else { $conn = mysql_connect("....", ".....", "......") or die(mysql_error()); mysql_select_db(".......", $conn) or die(mysql_error()); $forename = ($_POST['forename']); $addstudent = "INSERT INTO tbl_student (forename) VALUES ('$forename')"; $result = mysql_query($addstudent) or die(mysql_error()); if (mysql_affected_rows() == 1) { $message = '<p>Entry added. Would you like to <a href= "student.php">add another</a></p>'; } else { error_log(mysql_error()); $message = '<p>Something went wrong</p>'; } } } else { $message .= ' <FORM method="post" action="' . $_SERVER['PHP_SELF'] . '"> <TABLE BORDER="0" cellspacing="5"> <TR> <TD>Forename:</TD> <TD><input type="text" size="20" name="forename"></TD> </TR> <TR> <TD>Ethnicity:</TD> <TD> echo <select name="ethnicity">; $res=mysql_query("select * from tbl_ethnicity"); if(mysql_num_rows($res)==0) echo "there is no data in table.."; else echo"<option>Select:</option>"; for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); echo"<option>$row[ethnicity]</option>"; } echo </select>; </TD> </TR> <TR> <TD><input name="submit" type="submit" value="Add Student"></TD> </TR> </TABLE> </FORM>'; } ?> <html> <head> <style type="text/css"> </head> <body> <H1>Add Student Form</H1> <br><br> <?php echo $message; ?> </body> </html> 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.