Query error

Namecheap AuctionsNamecheap Auctions
NamecheapNamecheap
Namecheap AuctionsNamecheap Auctions
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
'$ mpgf' should be '$mpgf
 
0
Close
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
Close
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
Close
isnt it the samething or did u change something from it?
 
0
Close
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
Close
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
Close
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
Close
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
Close
Edit: *remider to self: Alway refresh before posting*
Someone beat me to it.. :)
 
0
Close
yup it was the '
thanks :)
 
0
Close
That's why being lazy is good. If you didn't put anything around those, it would have worked. ;)
 
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

A browser tab with a favicon next to the page title
Search members
You can now search for members by name, company, location and more. Open the search at the top of any page and choose Members. You will...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back