Jump to content

Code won't insert into database.


Chiaki

Recommended Posts

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.

Link to comment
Share on other sites

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());

Link to comment
Share on other sites

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>.";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.