Jump to content

Problems with INSERT INTO mysql statement


DannyM

Recommended Posts

Hello.

 

I am currently writing a private messaging script, however, my INSERT INTO statement is failing to insert anything into the database. If anyone could help me find the solution, that'd be great.

 

Here is my code:

 


<?php

if(!$_COOKIE["user"]) header("Location: viewForum.php");


//Connect to the Database
$con = mysql_connect("----","---","---");

if(!$con)
{
die("Can not connect to the server.");
}
$db=mysql_select_db("project",$con);
if(!$db)
{
die("Can not connect to the database.");
}

$user=$_GET['user'];
$id=$_GET['id'];
//make sure the user is in their own message box
if($user != $_COOKIE["user"]) {header("Location: message.php?user=".$_COOKIE['user']);}


//Echo the option to write a message
echo "
<html>
<head>
<style type='text/CSS'>
.divTable{
	border: 4px solid #a8aa11;
	width:850;
	background-color:#a77a99;
}
</style>
</head>
<body>
<div class='divTable' align='right'>
<form method='POST' action='message.php?user=".$user."'>
<input type='submit' name='newMessage' value='Compose'/>

</form>
</div>
<br/>
";
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
////Here is the error//////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//If they hit send
if(isset($_POST['sendMessage']))
{
$to=strip_tags($_POST['sendTo']);
$subject=strip_tags($_POST['subJect']);
$message=strip_tags($_POST['message']);

echo $to . " " . $subject . " " . $message;

$sql="INSERT INTO pmessage VALUES ('$to')";
mysql_query($sql);
echo 'Message sent.';
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////



//If they chose to compose a new message
if(isset($_POST['newMessage'])){
echo "
<div class='divTable' align='left'>
<form method='POST' action='message.php?user=".$user."'>
To : <input type='text' size='35' name='sendTo'/>
<br/>
Subject : <input type='text' size='50' name='subJect'/>
<textarea cols='100' rows='15' name='message'>
Enter your message here...
</textarea>
<br/>
<input type='submit' value='Send' name='sendMessage' />
</form>
</div>
<br/>";
}



//Echo the users inbox
$result=@mysql_query("SELECT * FROM pmessage WHERE to='$user' ORDER BY id");
if(!$result) { die("Sorry, you have no new messages right now."); }
while($row=mysql_fetch_array($result)){
echo $row['body'];
echo '<br/>';
}





?>

 

The $_POST variables are being passed through when you click send, but they  refuse to be entered into the table pmessage, along with any other random value(such as '0'). I've already checked the database, and the pmessage table has access to all MySql functions...

Even when I reference to all, it still won't enter the data.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to,from,body,title) VALUES ('a','a','Enter your message here... a','a')' at line 1

 

However,  the code I used for inserting was copied from another piece of the project that works fine, so the syntax should be fine..

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.