Hello all,
I’m working on my first PHP project which uses MySQL, but I’ve ran into a slight problem with one of my PDO queries. I’m trying to insert data into a table but the table name isn’t always the same, I thought I could bind a parameter to the prepared statement such as:
[php]
$table = “test”;
$product = $db->prepare(“INSERT INTO ? (?,?) VALUES (?,?)”);
$product->bindParam(1,$table);
[/php]
But after checking on google I realised that this does not work. So I’m trying to use a variable in the query instead without using bindParam, but I get a SQL syntax error. Here is my code:
[php]
function add_new_item($item_name,$item_price,$item_category) {
require("../inc/database.php");
switch($item_category) {
case "graphics":
$table = "graphics";
break;
case "hard_drives":
$table = "hard_drives";
break;
case "motherboards":
$table = "motherboards";
break;
case "operating_system":
$table = "operating_system";
break;
case "processors":
$table = "processors";
break;
case "ram":
$table = "ram";
break;
}
$namecolumn = $table . "_name";
$pricecolumn = $table . "_price";
try {
$product = $db->prepare("INSERT INTO $table (?,?) VALUES (?,?)");
$product->bindParam(1,$namecolumn);
$product->bindParam(2,$pricecolumn);
$product->bindParam(3,$item_name);
$product->bindParam(4,$item_price);
$value = $product->execute();
} catch(PDOException $e) {
echo $e->getMessage();
exit;
}
return $value;
}
[/php]
What am I doing wrong?
Thanks,
Adam