dousu8456 2017-01-05 19:42
浏览 41

MySQL + PHP使用DropDown List将产品添加到产品表中以从Category表中选择产品类别

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)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘