Insert not working... help its driving me crazy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
<br>
Forum Commoner
Posts: 35
Joined: Thu May 01, 2008 2:42 pm

Insert not working... help its driving me crazy

Post 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
timsewell
Forum Newbie
Posts: 21
Joined: Tue Feb 26, 2008 5:43 am
Location: Hove, England

Re: Insert not working... help its driving me crazy

Post 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'.
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Insert not working... help its driving me crazy

Post 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.
<br>
Forum Commoner
Posts: 35
Joined: Thu May 01, 2008 2:42 pm

Re: Insert not working... help its driving me crazy

Post 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'?
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Insert not working... help its driving me crazy

Post 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.
<br>
Forum Commoner
Posts: 35
Joined: Thu May 01, 2008 2:42 pm

Re: Insert not working... help its driving me crazy

Post by <br> »

Thanks, worked perfectly.
Post Reply