I am passing user inputs from a page called add_product.php. There is a drop down menu that CONCATs two table fields to form one name ($category). After passing $category to the product_created.php page, I can break this back down into the two respective fields using explode() on but how do I create variable out of the results?
Here is the snippet from add_product.php
<?php
//Grab the Product Category name and Bucket name from productcategory and bucket tables in DB
$qry=mysql_query("SELECT CONCAT(bucket.b_name, ' : ', productcategory.name) AS category
FROM productcategory, bucket, product
WHERE product.category_id = productcategory.id
AND productcategory.bucket_id = bucket.id", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
?>
<p>Category:
<select name="category" id="category">
<?php
//take the responses from the query above and show then in a drop down menu
while($row=mysql_fetch_array($qry))
{
echo $category = "<option value='".$row['category']."'>".$row['category']."</option>";
}
?>
</select>
I then pass that to product_created.php and this is what I have so far and where I am stuck:
//This gets all the other information from the form
$category=$_POST['category'];
// Explode category variable to get bucket.b_name and productcategory.name
list($b_name, $pc_name) = explode(":", $category);
echo $b_name.'<br/>'; // bucket.b_name
echo $pc_name.'<br/>'; // productcategory.name
//the above code works and prints the separate category names onscreen
$categories = array($b_name,$pc_name);
print "The product category name is $pc_name";
//test two: this code works and prints just the productcategory name
//Define the query to grab the product category id where pc_name equals the product category name
$sql = "SELECT id FROM productcategory WHERE name = '$pc_name'";
//submit the query and capture the result
$result = mysql_query($sql) or die(mysql_error());
echo $result;
//find out how many rows were retrieved
$numRows = mysql_num_rows($result);
//current result is 0 and should be 1 if successful
//this is my attempt to convert the productcategory id into a variable that can be passed back into the DB
$sql_q = "SELECT id FROM productcategory WHERE name = '$pc_name'" or die(mysql_error());
$sql = mysql_fetch_array($sql_q);
//above sets the category_id array
//example usage: $sql['mysql_col_name']
echo $sql['category_id'];
//the above prints the category_id
foreach ($sql as $category_id) {
//echo $category_id.'<br/>';
}
//Define query to add product to DB
$qry=mysql_query("INSERT INTO product(name,category_id,slug,old_price,price,description,video_url,status,date_posted)VALUES('$name','$category_id','$slug','$o_price','$price','$desc','$video','$status','$date')", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
else
{
echo "<br/>";
echo "Product Added Successfully";
echo "<br/>";
}
I know some of this is redundant but I am trying to teach myself how to solve this and this was the best way for me to learn.
I feel like I am really close, but that I have gone round and round at this point and have just confused myself. I understand that there may be other issues with my code as it is written, but I really need to focus on resolving this first. Any help, advise, teaching, would be most appreciated.