Jump to content

[SOLVED] sql error when i use php to run it


ludjer

Recommended Posts

when i try run one sql query i get a error

here is the sql query, it works in phpmyadmin when i run it

INSERT INTO `mainp_com` ( `ID` , `newsid` , `userid` , `comment` , `time` , `date1` )
VALUES (
NULL , '9', '2', 'woot this is a 1337 test', '03:16:06 AM', 'October 7 2007'
);

 

this is the out put i get, i have my own custom error printing

A fatal MySQL error occured.

Query: INSERT INTO `mainp_com` (`ID` , `newsid` , `userid` , `comment` , `time` , `date1`) VALUES (NULL, `9`, `2`, `i am the most l337 of them all`, `03:36:40 AM`, `October 7 2007`);

Error: (1054) Unknown column '9' in 'field list'

if this error happens again and you dont know what is wrong please email

 

now for the php code

 

<?php
include "functions.php";
if (isset($_GET["nid"])) $nid = $_GET["nid"];

else die("error");
if (isset($_POST['submit'])){
$comment = $_POST["comment"];
if($comment == null)
{
	die("error please input somthing into the comment box");
}
$userid = $user->data["user_id"];
$time = date("h:i:s A");
$date = date("F j Y");
$comment = htmlspecialchars($comment);
$sql = 'INSERT INTO `mainp_com` (`ID` , `newsid` , `userid` , `comment` , `time` , `date1`) VALUES (NULL, `'.$nid.'`, `'.$userid.'`, `'.$comment.'`, `'.$time.'`, `'.$date.'`);';
$qur = sql_safe($sql);//custome function

mysql_query($qur)or die(sqlerr($qur,mysql_error(),mysql_errno()));
}else{
	die("error");
	}
?>

 

thanks in advance

Link to comment
Share on other sites

there cant be a typo cause this sql query works when i run it using phpmyadmin

INSERT INTO `mainp_com` ( `ID` , `newsid` , `userid` , `comment` , `time` , `date1` )

VALUES (

NULL , '9', '54', 'this works when i use phpmyadmin', '2:35 am', ' October 7 2007'

);

and i copy it directly into my php code and edit the values

Link to comment
Share on other sites

if you have a look it says Unknown column '9' in 'field list', this means that the error comes after the null it comes in with newsid

 

here is the structure of my sql database

CREATE TABLE `mainp_com` (
  `ID` int(11) NOT NULL auto_increment,
  `newsid` int(11) NOT NULL,
  `userid` int(11) NOT NULL,
  `comment` mediumblob NOT NULL,
  `time` varchar(10) collate latin1_general_ci NOT NULL,
  `date1` varchar(20) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=9 ;

Link to comment
Share on other sites

Before sqlsafe: INSERT INTO `mainp_com` (`newsid` , `userid` , `comment` , `time` , `date1`) VALUES (`9`, `2`, `i am the most l337 of them all`, `04:05:41 AM`, `October 7 2007`);
After sqlsafe: INSERT INTO `mainp_com` (`newsid` , `userid` , `comment` , `time` , `date1`) VALUES (`9`, `2`, `i am the most l337 of them all`, `04:05:41 AM`, `October 7 2007`);

A fatal MySQL error occured.
Query: INSERT INTO `mainp_com` (`newsid` , `userid` , `comment` , `time` , `date1`) VALUES (`9`, `2`, `i am the most l337 of them all`, `04:05:41 AM`, `October 7 2007`);
Error: (1054) Unknown column '9' in 'field list'
if this error happens again and you dont know what is wrong please email

And why would your set your ID column to null?? its a waste of space but you can do it if you want, also that is how phpmyadmin gives it to you.

Link to comment
Share on other sites

Try it with no quotes around the field names and ' rather than ` around each value.

 

INSERT INTO mainp_com (newsid , userid , comment , time , date1) VALUES ('9', '2', 'i am the most l337 of them all', '04:05:41 AM', 'October 7 2007')

Link to comment
Share on other sites

i found my problem if i run the $comments through the sqlsafe before putting it into the sql query anbd not run the query through the sqlsafe it works,

 

sql safe gave this

INSERT INTO mainp_com (newsid , userid , comment , time , date1) VALUES (\'9\', \'2\', \'i am the most l337 of them all\', \'04:05:41 AM\', \'October 7 2007\')

 

and i needed this

 

INSERT INTO mainp_com (newsid , userid , comment , time , date1) VALUES ('9', '2', 'i am the most l337 of them all', '04:05:41 AM', 'October 7 2007')

 

thanks all

 

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.