pretty simple code in php and mysql:
code inserting data in mysql table :
<?php
include("config.php");
$sql="INSERT INTO messages_sent (to) VALUES (35)";
$result=mysql_query($sql);
if(!$result)
{
echo("<br>Sorry the message could not be inserted into table messages_sent</br><br>");
die(mysql_error());
}else{
echo("<br>Message Inserted</br>");
}
?>
table creation code follows:
$sql="CREATE TABLE IF NOT EXISTS `messages_sent`(
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`to` INT(11) NOT NULL,
`from` INT(11) NOT NULL,
`msg` TEXT NOT NULL,
`viewed` TINYINT(2)NOT NULL,
`time` DATETIME NOT NULL
)";
$result=mysql_query($sql);
if(!$result)
{
echo("Sorry the create query for table messages_sent could not be executed ");
die(mysql_error());
}
if i insert data in msg column, it is no prob but if the column is to or from then syntax error problem occurs.
Solution?
in the schema of your table, you are setting
NOT NULLwith noneDEFAULTvalue to other fields. so basically you cannot executeINSERT INTO messages_sent (to) VALUES (35)because you are leaving other fields
NULLand ALSOFROMandTOare Reserved Word in mySQL. Or instead use back ticks`inside your query.ex.