Jump to content

Date/Time Inserting Into Database Problem


morrisjohnny

Recommended Posts

I have an error saying Error: 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, Sent, Title, Opened, Message) VALUES ('To', 'From', '07:57:36 - 22 S' at line 1this line is

$message=mysql_real_escape_string($_POST['Message']);	$unread="2";	$sent=date("H:i:s - d M Y");		$sql="INSERT INTO $sql_table(To, From, Sent, Title, Opened, Message)VALUES('$to', '$from', '$sent', '$title', '$unread', '$message')";

Below is my code so you can see what i'm trying to do.

<?phpinclude"settings.php";if (isset($_POST['To']))	{	$con = mysql_connect($sql_host,$sql_username,$sql_password);		if (!$con)		{		  die('Could not connect: ' . mysql_error());		}	mysql_select_db($sql_database, $con);		$to=mysql_real_escape_string($_POST['To']);	$from=mysql_real_escape_string($_POST['From']);	$title=mysql_real_escape_string($_POST['Title']);	$message=mysql_real_escape_string($_POST['Message']);	$unread="2";	$sent=date("H:i:s - d M Y");		$sql="INSERT INTO $sql_table(To, From, Sent, Title, Opened, Message)VALUES('$to', '$from', '$sent', '$title', '$unread', '$message')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }else	{		echo"added";	}	}else	{	echo"<form name=\"input\" action=\"\" method=\"post\">	<input type=\"text\" value=\"To\" name=\"To\" /><br />	<input type=\"text\" value=\"From\" name=\"From\" /><br />	<input type=\"text\" name=\"Title\" value=\"Title\" /><br />	<input type=\"text\" name=\"Message\" value=\"Message\" /><br />	<input type=\"Submit\" Value=\"Submit\" /><input type=\"Reset\" Value=\"Reset\" /><br />	</form>";	}?>

Link to comment
Share on other sites

Using $sent=date("YYYY-MM-DD HH:MM:SS");I still get the errorError: 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, Sent, Title, Opened, Message) VALUES ('To', 'From', '200720072007200' at line 1

Link to comment
Share on other sites

Can u post your database structure.please check your databasedatatype of sent field should be datetimeand to and from are key words,change those fieldnameand check once

again same errro only it now says(wht same as aboth but with)VALUES ('To', 'From', '2007-09-22 10:1' at line 1tht's using $sent=date("Y-m-d H:i:s");
Link to comment
Share on other sites

ErrorIt now doesn't even reconised it's been posted?DataBase

-- phpMyAdmin SQL Dump-- version 2.10.2-- [url="http://www.phpmyadmin.net"]http://www.phpmyadmin.net[/url]-- -- Host: localhost-- Generation Time: Sep 22, 2007 at 12:24 PM-- Server version: 5.0.45-- PHP Version: 5.2.3SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";-- -- Database: `projects`-- -- ---------------------------------------------------------- -- Table structure for table `message_system`-- CREATE TABLE `message_system` (  `ID` int(11) NOT NULL auto_increment,  `To` varchar(30) collate latin1_general_ci NOT NULL,  `From` varchar(30) collate latin1_general_ci NOT NULL,  `Sent` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,  `Title` varchar(30) collate latin1_general_ci NOT NULL,  `Opened` varchar(1) collate latin1_general_ci NOT NULL,  `Message` longtext collate latin1_general_ci NOT NULL,  PRIMARY KEY  (`ID`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;-- -- Dumping data for table `message_system`-- 

New Code

<?phpinclude"settings.php";if (isset($_POST['sento']))	{	$con = mysql_connect($sql_host,$sql_username,$sql_password);		if (!$con)		{		  die('Could not connect: ' . mysql_error());		}	mysql_select_db($sql_database, $con);		$sendto=mysql_real_escape_string($_POST['sendto']);	$sentfrom=mysql_real_escape_strsing($_POST['sentfrom']);	$title=mysql_real_escape_string($_POST['Title']);	$message=mysql_real_escape_string($_POST['Message']);	$unread="2";	$sent=date("Y-m-d H:i:s");		$sql="INSERT INTO $sql_table(To, From, Sent, Title, Opened, Message)VALUES('$to', '$from', '$sent', '$title', '$unread', '$message')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }	echo"added";	}else	{	echo"<form name=\"input\" action=\"\" method=\"post\">	<input type=\"text\" value=\"To\" name=\"sendto\" /><br />	<input type=\"text\" value=\"From\" name=\"sentfrom\" /><br />	<input type=\"text\" name=\"Title\" value=\"Title\" /><br />	<input type=\"text\" name=\"Message\" value=\"Message\" /><br />	<input type=\"Submit\" Value=\"Submit\" /><input type=\"Reset\" Value=\"Reset\" /><br />	</form>";	}?>

Link to comment
Share on other sites

Change your query like and checkCREATE TABLE `message_system` ( `ID` int(11) NOT NULL auto_increment, `mTo` varchar(30) collate latin1_general_ci NOT NULL, `mFrom` varchar(30) collate latin1_general_ci NOT NULL, `Sent` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `Title` varchar(30) collate latin1_general_ci NOT NULL, `Opened` varchar(1) collate latin1_general_ci NOT NULL, `Message` longtext collate latin1_general_ci NOT NULL, PRIMARY KEY (`ID`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1 ;Execute this query "To", "From" are keywords so its not working

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...