I have 5 Tables created in MySQL, 4 are different products'Clothes, Toys, Electronics and Books' with few columns (ID, productName and price) and one table is Categories with (categoryID and categoryName). I'm trying to make page where after login in admin can add a product and assign it to category, and later be able to add more Categories. I have a Drop-down List of categories where user chose a category he wishes to add a product to and then using provided from fills in necessary information like the product name and price. Unfortunately I keep getting an error which I cannot overcome Help ;( Here is my Code:
//this creates the drop down list of categories...
//Database connection
$server = '*********';
$username = '********';
$password = '******';
//The name of the schema created in MySQL Workbench
$schema = '******';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password,
[ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$results = $pdo1->query('SELECT * FROM categories ');
echo 'Chose Category:';
echo "<form action='add_products.php' method='POST'>";
echo "<select name='catForm'>";
foreach ($results as $row) {
echo "<label>Chose Category:</label><option value='" . $row['catName'] . "'>" . $row['catName'] . "</option>";
}
echo "</select>";
echo "<input type='submit' value='Add' name='submitSelectCat' />
</form>";
if (isset($_POST['submitSelectCat']))
{
$myvalue=$_POST['catForm'];
echo $myvalue;
}
//-----------------------------------
//This is what happens when button was pressed
//Database connection
$server = '*********';
$username = '**********';
$password = '**********';
//The name of the schema created in MySQL Workbench
$schema = '********';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password,
[ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->query('INSERT INTO ' . $myvalue . '(productName, price)
VALUES(
"' . $_POST['productName'] . '",
"' . $_POST['price'] . '"
)');
echo $myvalue;
}
//The form was not submitted, display the form
else {
?>
<form action="add_products.php" method="POST">
<label>Product Name:</label>
<input type="text" name="productName" />
<label>Price:</label>
<input type="text" name="price" value="£" />
<input type="submit" value="Add" name="submitAddProduct" />
</form>
<?php
}
?>
( ! ) Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(productName, price) VALUES( "thissssssssss", "£1111" )' at line 1 in /srv/http/public_html/add_products.php on line 78
( ! ) PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(productName, price) VALUES( "thissssssssss", "£1111" )' at line 1 in /srv/http/public_html/add_products.php on line 78
LINE 77- "' . $_POST['price'] . '"
LINE - 78 )');
Notice: Undefined variable: myvalue in /srv/http/public_html/add_products.php on line 74
LINE 74 - $pdo->query('INSERT INTO ' . $myvalue . '(productName, price)