I have a form that passes user inputs to create_product.php where I define the variables to use in a SQL SELECT query. I have one variable that I use list() to explode the results into PHP variables and echo those values.
[php]
$category=$_POST[‘category’];
// Explode category variable to get bucket.b_name and productcategory.name
list($b_name, $pc_name) = explode(":", $category);
echo $b_name.’
’; // bucket.b_name
echo $pc_name.’
’; // productcategory.name
$categories = array($b_name,$pc_name);
print "The product category name is $pc_name
";
[/php]
Everything is functioning up until this point. I then take $pc_name and use it in a SELECT query to retrieve the ID from the productcategory table where the name = $pc_name.
[php]
$sql_q = “SELECT id FROM productcategory WHERE name=’$pc_name’”;
$res = mysql_query($sql_q) or die(mysql_error());
$sql = mysql_fetch_assoc($res);
//above sets the category id array
//example usage: $sql[‘mysql_col_name’]
echo $sql[‘id’];
[/php]
However, it does not echo $sql[‘id’] like I would expect it to. Instead the page is blank where the result should. If I switch out the actual value for $pc_name (ex: Accessories), it will retrieve and echo the corresponding ID.
I then want to use INSERT INTO to submit all the user inputs to the products table, like so:
[php]
$qry=mysql_query(“INSERT INTO product(name,category_id,slug,old_price,price,description,video_url,status,date_posted)VALUES(’$name’,{$sql[‘id’]},’$slug’,’$o_price’,’$price’,’$desc’,’$video’,’$status’,NOW())”, $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
else
{
echo “
”;
echo “Product Added Successfully”;
echo “
”;
}
[/php]
I am getting the following error (I believe due to the empty result I am trying to plug into category_id):
Query Failed: 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 ‘‘testing’,’’,‘1’,‘this is a test?’,’’,‘1’,NOW())’ at line 1
My question is: what am I doing incorrectly that is causing an empty result in my SELECT statement?
If anyone has any insight, it would be most appreciated. I am desperate to find a resolution to this and seem to be going around and around with myself trying to figure it out. I need some outside eyes to help.
Thanks!