suttercain Posted March 31, 2007 Share Posted March 31, 2007 Hi everyone, I have a form that INSERTs information into my database. This one works.... <?php extract($_REQUEST); if ( isset($subject) && isset($body) ) { mysql_connect("localhost","root"); mysql_select_db( "messages" ); mysql_query( "INSERT INTO messages (subject, body) ". "VALUES ('$subject', '$body' )" ); $status = "Message <i>$subject</i> has been posted<br><br>"; } ?> <html><head><title>message</title></head> <body> <h1>Post a Message</h1> <strong><?php echo $status ?></strong> <form action="<?php echo $_SERVER['SELF'] ?>"> Subject <input type="text" name="subject" size="35"> <br><br> <textarea name="body" cols="40" rows="15"> </textarea> <br><br> <input type="submit" value="Post"> </form> </body> </html> But when I change a little bit of it and use another database... this one doesn't work... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <?php extract($_REQUEST); if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) { mysql_connect("localhost","root", NULL); mysql_select_db( "comics" ); mysql_query ("INSERT INTO comics (title, text, plot, cover_artists) " . "VALUES ('$title', '$date', '$text', '$plot', '$cover_artists')"); $status = "Message $title has been uploaded"; } ?> <strong><?php echo $status ?></strong> <form action="<?php echo $_SERVER['SELF'] ?>"> Title: <input type="text" name="title" size="35"><br><br> Text: <input type="text" name="text" size="35"><br><br> Plot: <input type="text" name="plot" size="35"><br><br> Cover Artists: <input type="cover_artists" name="title" size="35"><br><br> <input type="submit" value="Post"> </form> </body> </html> Why is the first one working... yet, the second one isn't? Thanks Link to comment https://forums.phpfreaks.com/topic/45062-solved-basic-form-insert-into-one-works-another-doesnt/ Share on other sites More sharing options...
shocker-z Posted March 31, 2007 Share Posted March 31, 2007 Try this to return an error of some sort for us to work from: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <?php extract($_REQUEST); if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) { mysql_connect("localhost","root", NULL) or die('Error Connecting: '.mysql_error()); mysql_select_db( "comics" ) or die('Error Selecting DB: '.mysql_error());; mysql_query ("INSERT INTO comics (title, text, plot, cover_artists) " . "VALUES ('$title', '$date', '$text', '$plot', '$cover_artists')") or die('Error In Query: '.mysql_error());; $status = "Message $title has been uploaded"; } ?> <strong><?php echo $status ?></strong> <form action="<?php echo $_SERVER['SELF'] ?>"> Title: <input type="text" name="title" size="35"><br><br> Text: <input type="text" name="text" size="35"><br><br> Plot: <input type="text" name="plot" size="35"><br><br> Cover Artists: <input type="cover_artists" name="title" size="35"><br><br> <input type="submit" value="Post"> </form> </body> </html> This is always a good way to retrieve the error without having to look over you code 100 times and a way to self help yourself as most errors are self explanatory. Regards Liam Link to comment https://forums.phpfreaks.com/topic/45062-solved-basic-form-insert-into-one-works-another-doesnt/#findComment-218761 Share on other sites More sharing options...
suttercain Posted March 31, 2007 Author Share Posted March 31, 2007 Hi Shocker, I tried that code and it did the same thing. No errors, it just reloads the form without entering the data into the database. Link to comment https://forums.phpfreaks.com/topic/45062-solved-basic-form-insert-into-one-works-another-doesnt/#findComment-218763 Share on other sites More sharing options...
shocker-z Posted March 31, 2007 Share Posted March 31, 2007 Try this to see if you if statement is correct.. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <?php extract($_REQUEST); if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) { echo ("Correct!!!"); } else { echo ("Failed!!!"); } ?> </body> </html> Liam Link to comment https://forums.phpfreaks.com/topic/45062-solved-basic-form-insert-into-one-works-another-doesnt/#findComment-218768 Share on other sites More sharing options...
suttercain Posted March 31, 2007 Author Share Posted March 31, 2007 I ran this: <?php extract($_REQUEST); if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) { echo ("Correct!!!"); } else { echo ("Failed!!!"); } if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) { mysql_connect("localhost","root", NULL) or die('Error Connecting: '.mysql_error()); mysql_select_db( "comics" ) or die('Error Selecting DB: '.mysql_error());; mysql_query ("INSERT INTO comics (title, text, plot, cover_artists) " . "VALUES ('$title', '$date', '$text', '$plot', '$cover_artists')") or die('Error In Query: '.mysql_error());; $status = "Message $title has been uploaded"; } ?> <strong><?php echo $status ?></strong> <form action="<?php echo $_SERVER['SELF'] ?>"> Title: <input type="text" name="title" size="35"><br><br> Text: <input type="text" name="text" size="35"><br><br> Plot: <input type="text" name="plot" size="35"><br><br> Cover Artists: <input type="cover_artists" name="title" size="35"><br><br> <input type="submit" value="Post"> </form> And it echoed failed! and reloaded the form hmm.... Link to comment https://forums.phpfreaks.com/topic/45062-solved-basic-form-insert-into-one-works-another-doesnt/#findComment-218770 Share on other sites More sharing options...
suttercain Posted March 31, 2007 Author Share Posted March 31, 2007 I fexed the form html, there was an error with Cover Artists. Once I did that ran the code and got this error: Correct!!!Error In Query: Column count doesn't match value count at row 1 Link to comment https://forums.phpfreaks.com/topic/45062-solved-basic-form-insert-into-one-works-another-doesnt/#findComment-218771 Share on other sites More sharing options...
hellouthere Posted March 31, 2007 Share Posted March 31, 2007 (title, text, plot, cover_artists) 4 columns... ('$title', '$date', '$text', '$plot', '$cover_artists') 5 values... i think thats ur problem... Link to comment https://forums.phpfreaks.com/topic/45062-solved-basic-form-insert-into-one-works-another-doesnt/#findComment-218773 Share on other sites More sharing options...
suttercain Posted March 31, 2007 Author Share Posted March 31, 2007 I got it working, my bad. Thanks shocker for the help. Quick question though... does anyone know how to do a "Preview" page before actually submitting it the user enters the info, hits submit, verifies the information is correct, hit's submit again to enter into MySQL? Here is the working code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> </head> <body> <?php extract($_REQUEST); if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) { echo ("Correct!!!"); } else { echo ("Failed!!!"); } if (isset($title) && isset($text) && isset($plot) && isset($cover_artists)) { mysql_connect("localhost","root", NULL) or die('Error Connecting: '.mysql_error()); mysql_select_db( "comics" ) or die('Error Selecting DB: '.mysql_error());; mysql_query ("INSERT INTO comics (title, text, plot, cover_artists) " . "VALUES ('$title', '$text', '$plot', '$cover_artists')") or die('Error In Query: '.mysql_error());; $status = "Message $title has been uploaded"; } ?> <strong><?php echo $status ?></strong> <form action="<?php echo $_SERVER['SELF'] ?>"> Title: <input type="text" name="title" size="35"><br><br> Text: <input type="text" name="text" size="35"><br><br> Plot: <input type="text" name="plot" size="35"><br><br> Cover Artists: <input type="text" name="cover_artists" size="35"><br><br> <input type="submit" value="Post"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/45062-solved-basic-form-insert-into-one-works-another-doesnt/#findComment-218774 Share on other sites More sharing options...
shocker-z Posted March 31, 2007 Share Posted March 31, 2007 no Problem You could store it in a tmp table then transfer or you could hold all the info in session variables and on submit you actually insert the information.. Better using session variables though. Liam Link to comment https://forums.phpfreaks.com/topic/45062-solved-basic-form-insert-into-one-works-another-doesnt/#findComment-218777 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.