DannyM Posted May 27, 2008 Share Posted May 27, 2008 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... Link to comment https://forums.phpfreaks.com/topic/107489-problems-with-insert-into-mysql-statement/ Share on other sites More sharing options...
BlueSkyIS Posted May 27, 2008 Share Posted May 27, 2008 mysql_query($sql) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/107489-problems-with-insert-into-mysql-statement/#findComment-550948 Share on other sites More sharing options...
DannyM Posted May 27, 2008 Author Share Posted May 27, 2008 Whoops, sorry. I forget stuff sometimes. Here is the error - Column count doesn't match value count at row 1 What does it mean? Link to comment https://forums.phpfreaks.com/topic/107489-problems-with-insert-into-mysql-statement/#findComment-550953 Share on other sites More sharing options...
revraz Posted May 27, 2008 Share Posted May 27, 2008 Means you only reference one VALUE ($to) and there are more fields in your table than just that one. Link to comment https://forums.phpfreaks.com/topic/107489-problems-with-insert-into-mysql-statement/#findComment-550959 Share on other sites More sharing options...
DannyM Posted May 27, 2008 Author Share Posted May 27, 2008 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.. Link to comment https://forums.phpfreaks.com/topic/107489-problems-with-insert-into-mysql-statement/#findComment-550971 Share on other sites More sharing options...
revraz Posted May 27, 2008 Share Posted May 27, 2008 If it was fine, you wouldn't have an error TO is a mysql reserved word, either use `backticks` around the fieldname or rename it. Link to comment https://forums.phpfreaks.com/topic/107489-problems-with-insert-into-mysql-statement/#findComment-550973 Share on other sites More sharing options...
DannyM Posted May 27, 2008 Author Share Posted May 27, 2008 Ag, I need to stop using keywords in MySQL statements. Every error I've had with MySQL so far in this project has had to do with keywords. I dunno how 'to' slipped by me. Thankyou! Link to comment https://forums.phpfreaks.com/topic/107489-problems-with-insert-into-mysql-statement/#findComment-550976 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.