Page 1 of 1
Insert not working... help its driving me crazy
Posted: Mon Jun 09, 2008 2:11 pm
by <br>
Code: Select all
<?php
$con = mysql_connect('hostname.db', 'username', 'pass') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
if($_POST['submit']){
$name=$_POST['name'];
$price=$_POST['price'];
$thumb=$_POST['thumb'];
$img=$_POST['image'];
$desc=$_POST['desc'];
$cat1=$_POST['cat1'];
$cat2=$_POST['cat2'];
mysql_query('insert into table (name, price, img, thumb, desc, cat1, cat2) values ("$name", "$price", "$img", "$thumb", "$desc", "$cat1", "$cat2")');
header('location:http://www.mysite.com/admin/');
}
mysql_close($con);
?>
I have no idea why this will not insert a row... the variables can be echoed so they are being posted & set... I have an auto_increment id column, but that will automatically be set right? The only difference I can find between this and other scripts I've written that work is that I am not using the IP address of the host, but data can be selected by using "myhostname.db" so I don't see why that would not work for inserting...
Thanks
Re: Insert not working... help its driving me crazy
Posted: Mon Jun 09, 2008 4:07 pm
by timsewell
Add 'or die(mysql_error())' to the end of your query to see where it's failing. Perhaps it's because you haven't assigned the query to the resource '$con'.
Re: Insert not working... help its driving me crazy
Posted: Mon Jun 09, 2008 4:29 pm
by dbemowsk
Try this:
Code: Select all
$sql = "insert into table (name, price, img, thumb, desc, cat1, cat2) values ('$name', '$price', '$img', '$thumb', '$desc', '$cat1', '$cat2')";
echo $sql; //comment out or remove this line after your problem has been fixed.
mysql_query($sql);
I changed it so the query is using single quotes for the values instead of double quotes. Assigning the query to a variable will allow you to see the query with all the values inserted. From there you can see if there is some data that is messing up your query. It may also be a good idea to use mysql_real_escape_string on your varchar and text fields to prevent erroneous errors in your query down the line.
Re: Insert not working... help its driving me crazy
Posted: Mon Jun 09, 2008 5:08 pm
by <br>
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 'desc, cat1, cat2) VALUES ('fafa', 'afaf', 'afaf', 'fafa', 'fafa', 'Earrings - Go' at line 1
I don't see a syntax error... here's what my "insert" line looks like (copied from an echo):
INSERT INTO products (name, price, img, thumb, desc, cat1, cat2) VALUES ('fafa', 'afaf', 'afaf', 'fafa', 'fafa', 'Earrings - Gold', 'Earrings - Gold')
All fields (except auto_increment) are text, but collation was set to 'latin1_swedish_ci' for some reason... issue? Always use 'ascii_bin'?
Re: Insert not working... help its driving me crazy
Posted: Mon Jun 09, 2008 5:17 pm
by dbemowsk
Change your field name desc to something else like descr. Desc is a mysql command an is confusing the sql engine. Or put your field names in backticks.
Re: Insert not working... help its driving me crazy
Posted: Tue Jun 10, 2008 9:25 am
by <br>
Thanks, worked perfectly.