NameSilo

Query error

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Impact
19
Hey
i am getting a querry error from this line:
PHP:
		$query = "INSERT INTO `mpg` ('id','Manufacturer','Year','Car Name','Miles Per Gallon City',
				  'Miles Per Gallon Freeway','Year Gas Price') 
				  VALUES ('','$manufacturer','$year','$car_name','$mpgc','$mpgf','$year_gas_price')";
		mysql_query($query) or die ("Error, insert query failed");
it gives the "Error,insert query failed" error...and i have 2 questions
1. If there is any visible problem in that piece of code
2. How can i make it show the actual error instead of the die command that i gave it..if i remove that die command..it shows nothing..
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
'$ mpgf' should be '$mpgf
 
0
•••
hm it is like that..i duno y it added that space when i copy pasted it here :-s

$query = "INSERT INTO mpg ('id','Manufacturer','Year','Car Name','Miles Per Gallon City',
'Miles Per Gallon Freeway','Year Gas Price')
VALUES ('','$manufacturer','$year','$car_name','$mpgc','$mpgf','$year_gas_price')";
mysql_query($query) or die ("Error, insert query failed");
 
0
•••
mysql_query("INSERT INTO mpg ('id','Manufacturer','Year','Car Name','Miles Per Gallon City',
'Miles Per Gallon Freeway','Year Gas Price')
VALUES ('','$manufacturer','$year','$car_name','$mpgc','$ mpgf','$year_gas_price'")or die ("Error, insert query failed");
 
0
•••
isnt it the samething or did u change something from it?
 
0
•••
It's the same thing.. >__>

Run the query in phpMyAdmin and post the error it gives you. I don't see anything that would just be wrong on its own, unless you can't have spaces in the column titles. The only other thing I see is ' around column names.. shouldn't it be ` or nothing?

If you used spaces between items, it wouldn't put that space in by itself into $mpgf
 
0
•••
Always added error reporting to every query, this will gve you a more descriptive error as Dan said phpmyadmin will.

Run this:
PHP:
 $query = "INSERT INTO `mpg` ('id','Manufacturer','Year','Car Name','Miles Per Gallon City',
                  'Miles Per Gallon Freeway','Year Gas Price') 
                  VALUES ('','$manufacturer','$year','$car_name','$mpgc','$  mpgf','$year_gas_price')";
        mysql_query($query) or die ("Error, insert query failed" . mysql_error());

Note the mysql_error() has been added.

The problem is (i suspect) that you are not using backticks for field names, instead you are using '.

Correct query should be:
Code:
INSERT INTO `mpg` (`id`,`Manufacturer`,`Year`,`Car Name`,`Miles Per Gallon City`,
                  `Miles Per Gallon Freeway`,`Year Gas Price`) 
                  VALUES ('','$manufacturer','$year','$car_name','$mpgc','$  mpgf','$year_gas_price')
 
Last edited:
0
•••
unknowngiver said:
2. How can i make it show the actual error instead of the die command that i gave it..if i remove that die command..it shows nothing..

Change:-

PHP:
or die ("Error, insert query failed");

To the following:-


PHP:
or die (mysql_errno().' : '.mysql_error());

Best to change it before the site goes into production tho as people can work out how your database is made from the errors.



EDIT Looks like Matthew. just beat me :td:
 
Last edited:
0
•••
filth@flexiwebhost said:
Change:-

PHP:
or die ("Error, insert query failed");

To the following:-


PHP:
or die (mysql_errno().' : '.mysql_error());

Beat ya :hehe:

Best to change it before the site goes into production tho as people can work out how your database is made from the errors.

Now i've never agreed with that, if someone is in a position where they can executer a query they don't need to know table/field names...You can get those by running a query.

(having said that i normally extend my error logger into the mysql class so they do get hidden)
 
0
•••
Edit: *remider to self: Alway refresh before posting*
Someone beat me to it.. :)
 
0
•••
yup it was the '
thanks :)
 
0
•••
That's why being lazy is good. If you didn't put anything around those, it would have worked. ;)
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back