无法在MariaDB中执行查询

时间:2016-05-08 08:07:39

标签: php database mariadb

大家好我正在尝试更新数据库中的产品表 用户必须输入产品的ID才能进行更新 这是表格

            <form id="myform"method="post"action="update.php">
        <table class="userinf">
            <tr>
                <td><label class="labelii">PID</label></td>
                <td><input id ="edtxt" class ="itxt"type="text" name="PID"required></td>
                <td></td>
            </tr>
            <tr>
                <td><label class="labelii">Name</label></td>
                <td><input id ="iitxt1" class ="itxt"type="text" name="Name" r></td>
                <td><button type="submit" class="Ebutton" onclick="submitform()">Update</button></td>
            </tr>
            <tr>
                <td><label class="labelii">Picture</label></td>
                <td><input id ="iitxt2" class ="itxt" type="text" name="Picture" ></td>
                <td><button type="submit" class="Ebutton" onclick="submitform()">Update</button></td>
            </tr>
            <tr>
                <td><label class="labelii">Price</label></td>
                <td><input id ="iitxt3" class ="itxt" type = "text" name="Price" pattern="[-+]?([0-9]*\.[0-9]+|[0-9]+)"></td>
                <td><button type="submit" class="Ebutton" onclick="submitform()">Update</button></td>
            </tr>
            <tr>
                <td><label class="labelii">Type</label></td>
                <td><input id ="iitxt4" class ="itxt" type="text" name="Type"></td>
                <td><button type="submit" class="Ebutton" onclick="submitform()">Update</button></td>
            </tr>
            <tr>
                <td><label class="labelii">Stock</label></td>
                <td><input id ="iitxt5" class ="itxt"type="number" name="Stock"></td>
                <td><button type="submit" class="Ebutton" onclick="submitform()">Update</button></td>
            </tr>
            <tr>
                <td></td>
                <td><button type="submit" class="Ebutton" onclick="submitform()">Update All</button></td>
                <td></td>
            </tr>
        </table>
    </form>

这是进行更新的php代码,我从DB中查询它的查询

            <?php
        if(isset($_POST['PID'])){
            $PID=$_POST['PID'];
        }
        if(isset($_POST["Name"]) && isset($_POST["Picture"]) && isset($_POST["Price"]) && isset($_POST["Type"]) && isset($_POST["Stock"])){
            $Name=$_POST['Name'];
            $Picture=$_POST['Picture'];
            $Price=floatval($_POST['Price']);
            $Type=$_POST['Type'];
            $Stock=intval($_POST['Stock']);
            $query="UPDATE `product` SET `Name` = '$Name' , `Picture` = '$Picture', `Price` = '$Price' ,`Type` = '$Type' ,`Stock` = '$Stock' WHERE `product`.`PID` = '$PID'";
        }
        if (isset($_POST['Name'])){ 
            $Name=$_POST['Name'];
            $query="UPDATE `product` SET `Name` = '$Name' WHERE `product`.`PID` = '$PID'";
        }
        else if(isset($_POST['Picture'])){
            $Picture=$_POST['Picture'];
            $query="UPDATE `product` SET `Picture` = '$Picture' WHERE `product`.`PID` = '$PID'";
        }
        else if( isset($_POST['Price'])){
            $Price=floatval($_POST['Price']);
            $query="UPDATE `product` SET `Price` = '$Price' WHERE `product`.`PID` = '$PID'";
        }
        else if(isset($_POST['Type'])){
            $Type=$_POST['Type'];
            $query="UPDATE `product` SET `Type` = '$Type' WHERE `product`.`PID` = '$PID'";
        }
        else if(isset($_POST['Stock'])){
            $Stock=intval($_POST['Stock']);
            $query="UPDATE `product` SET `Stock` = '$Stock' WHERE `product`.`PID` = '$PID'";
        }

这是我的代码的最后一个版本,它只更新了名称

2 个答案:

答案 0 :(得分:0)

只是从DB服务器返回DB API错误,与您的代码语言无关。它表示您传递了无效的SQL语句。

还有一个额外的昏迷&#34;,&#34;在第一个$ query赋值语句中的WHERE语句之前。

更新:zainab,我看到你的编辑,现在,原来的问题解决了吗?

答案 1 :(得分:0)

如果$_POST["Name"]O'Brian,则会出现语法错误。逃避来自<form>

的任何内容