SQL Query returns empty result

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!

Hello,

I would like to see your database Structure, if possible

I have three tables that are involved in this query.

  1. product table - houses the individual product information that link back to a product category
  2. productcategory table - sub-categories of the bucket table
  3. bucket table - high level product categories

Product Table Structure

[table]
[tr]
[td]Field[/td][td]Type[/td][td]Null[/td][td]Index[/td]
[/tr]
[tr]
[td]id[/td][td]INT[/td][td]No[/td][td]PK[/td]
[/tr]
[tr]
[td]name[/td][td]VARCHAR[/td][td]No[/td][td][/td]
[/tr]
[tr]
[td]category_id[/td][td]INT[/td][td]No[/td][td]FK[/td]
[/tr]
[tr]
[td]slug[/td][td]VARCHAR[/td][td]No[/td][td][/td]
[/tr]
[tr]
[td]old_price[/td][td]INT[/td][td]Yes[/td][td][/td]
[/tr]
[tr]
[td]price[/td][td]INT[/td][td]No[/td][td][/td]
[/tr]
[tr]
[td]description[/td][td]text[/td][td]No[/td][td][/td]
[/tr]
[tr]
[td]status[/td][td]INT[/td][td]No[/td][td][/td]
[/tr]
[tr]
[td]date_posted[/td][td]date[/td][td]No[/td][td][/td]
[/tr]
[tr]
[td]video_url[/td][td]VARCHAR[/td][td]No[/td][td][/td]
[/tr]
[/table]


productcategory table structure

[table]
[tr]
[td]Field[/td][td]Type[/td][td]Null[/td][td]Index[/td]
[/tr]
[tr]
[td]id[/td][td]INT[/td][td]No[/td][td]PK[/td]
[/tr]
[tr]
[td]name[/td][td]VARCHAR[/td][td]No[/td][td][/td]
[/tr]
[tr]
[td]slug[/td][td]VARCHAR[/td][td]No[/td][td][/td]
[/tr]
[tr]
[td]bucket_id[/td][td]INT[/td][td]No[/td][td]FK[/td]
[/tr]
[/table]

bucket table structure

[table]
[tr]
[td]Field[/td][td]Type[/td][td]Null[/td][td]Index[/td]
[/tr]
[tr]
[td]id[/td][td]INT[/td][td]No[/td][td]PK[/td]
[/tr]
[tr]
[td]name[/td][td]VARCHAR[/td][td]No[/td][td][/td]
[/tr]
[tr]
[td]slug[/td][td]VARCHAR[/td][td]No[/td][td][/td]
[/tr]
[/table]

I hope this helps. If you need me to supply any additional information I am happy to do so for the help.

well I echoed $sql[‘id’] on my localhost, just make sure your that your $pc_name match one of your record in your database, echo $pc_name and compare.

you will get error on the second query because you need to declare all the following varibles
[php]’$slug’,’$o_price’,’$price’,’$desc’,’$video’,’$status’, $con[/php]

You need to make sure the user input is exact in the spelling. mysql doesn’t know the difference between Sales and sales, so if the table data is in caps and the user input isn’t, the query will fail. you can get around that by using LIKE

$sql_q = ‘SELECT id FROM productcategory WHERE name LIKE “%$pc_name%”’;

Agree with Richei, also you can use strtolower() all your user input to make comparison easier

Thank you for your help with this. It is halfway there. I have defined the variables and am now able to INSERT INTO using the qry defined earlier. But, I am still not able to echo $sql[‘id’]. So it is passing a empty result into the database. I did echo $pc_name (T-shirts) and checked to make sure that it matched a record in the productcategory table. It does… spelling, capitalization, etc… is the same. I also tried using LIKE “%$pc_name%” as another poster suggested below.

Do you have any other ideas or tests I can perform to determine why this would not be working. I am going to look at strtolower() next.

Again, thank you (everyone) for the help thus far.

Thank you for responding. I think I understand what you are saying. It must be an exact match when using = . Correct me if my thinking is wrong here though, but for this specific example, the user is selecting a item from a dropdown, that is populated from the DB. So in this instance, the input is from the DB and being passed back to the table to SELECT the corresponding category id. Am I correct in this thinking that it should be a match?

I ran out of Ideas, i mean the codes look right and it works on my localhost.

can you save all the queries to recreate your database tables, together with all the necesary php files put it in a zip and email to me.

OR

I can help you thru teamviewer whichever is easier for you

@wilson382:
I am not familiar with teamviewer so I was planning to email the files over that you asked for. However, when I click on the email icon under your name, I don’t see anywhere in the email system where I can add an attachment. Let me knowbest way to get these files to you?
Thanks!

I got the files, i will be taking a look in half a hour. i will post back with results

Thank you so much Wilson382! I would not have found the CONCAT issue in my form had you not taken the time to review the file with me. I appreciate the lessons!

Sponsor our Newsletter | Privacy Policy | Terms of Service