Chiaki Posted June 2, 2011 Share Posted June 2, 2011 Hello, I am trying to get a news script to work, but I can't get it to insert anything into the database. I cannot figure it out for the life of me. <form name="shout" action="post.php" method="post"> <p><b>Name:</b><br/><input type="text" name="name" size="15"/></p> <p><b>Message</b>:<br/> <textarea wrap="physical" name="message" rows="3" cols="25">News goes here! </textarea></p> <p><input type="submit" value="Shout now!"/> <input type="reset" value="Clear"/></p> </form> <? $name = $_POST["name"]; $message = $_POST["message"]; include("dbconnect.php"); $date = date("M j y"); $menu = MYSQL_QUERY("INSERT INTO adminnews (id,name,date,message)". "VALUES ('NULL', '$name', '$date', '$message')"); echo("Shout-out added! You will be redirected to the main page shortly. If you are not, click <a href='index.php'>here</a>."); ?> dbconnect.php is just the configuration for connecting to the database, and I have checked that all ready. A log-in script uses it just fine. Quote Link to comment https://forums.phpfreaks.com/topic/238171-code-wont-insert-into-database/ Share on other sites More sharing options...
mikesta707 Posted June 2, 2011 Share Posted June 2, 2011 you are inserting the string NULL into the id column, which I assume is of type int. Since you seem to want to pass NULL into it, I assume its some sort of primary or unique key that auto increments. If so, you can just leave it out of the INSERT query and it will be automatically generated. For example $menu = MYSQL_QUERY("INSERT INTO adminnews (name,date,message)". "VALUES ('$name', '$date', '$message')"); if that is still giving you trouble, you can try showing the mysql error if there is one like so $menu = MYSQL_QUERY("INSERT INTO adminnews (name,date,message)". "VALUES ('$name', '$date', '$message')"); or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/238171-code-wont-insert-into-database/#findComment-1223913 Share on other sites More sharing options...
Pikachu2000 Posted June 2, 2011 Share Posted June 2, 2011 See what errors it generates, and post them here if you still need help with it. $date = date("M j y"); $query = "INSERT INTO adminnews (id,name,date,message) VALUES ('NULL', '$name', '$date', '$message')"; $menu = mysql_query($query) or die( "<br>Query: $query<br>Failed with error: " . mysql_error() ); echo "Shout-out added! You will be redirected to the main page shortly. If you are not, click <a href='index.php'>here</a>."; Quote Link to comment https://forums.phpfreaks.com/topic/238171-code-wont-insert-into-database/#findComment-1223914 Share on other sites More sharing options...
PFMaBiSmAd Posted June 2, 2011 Share Posted June 2, 2011 Also, by not escaping $name and $message, you could have characters in those fields that can break the sql syntax, resulting in sql errors (and it also allows sql to be injected.) You should use a mysql DATE data type (YYYY-MM-DD) for your date field. The M j y format you have now will make it extra difficult to match data by the date and retrieve it in any date order. Quote Link to comment https://forums.phpfreaks.com/topic/238171-code-wont-insert-into-database/#findComment-1223917 Share on other sites More sharing options...
Chiaki Posted June 2, 2011 Author Share Posted June 2, 2011 *groan* Yes, that was the problem. I don't know why I didn't realize it before. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/238171-code-wont-insert-into-database/#findComment-1223918 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.