gpt4 book ai didi

php - 准备好的语句 : error near '? WHERE ` quantity`=? '

转载 作者:行者123 更新时间:2023-11-30 22:57:43 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用准备好的语句来验证表单,并且我希望能够在多个表单中使用相同的代码。我想坚持使用 DRY 方法。

每个表单将包含不同的表单名称(表示数据库表的隐藏字段)并具有不同的属性(选择列表中的值表示 MYSQL 数据库中的列),这些属性需要在添加到之前进行验证购物车。

由于某些疯狂的原因,价格增加是在 JS 中完成的(我知道这是一种糟糕的增加价格的方式,但这是我唯一的选择,所以不幸的是,这不是一个选择)。需要验证表单以确保用户没有篡改表单以节省一些钱。因此,我检查的方式是从表单中获取值,并根据数据库中的值检查它们,以确保它们相同,如果任何值与数据库中的值不同,则表单无效。我不断收到关于我的 SQL 语句的错误。特别是它未能准备声明

这是带有准备好的语句的 PHP 代码:

<?php

///////MYSQL Login Data/////
$user = "root";
$password = "";
$database = "printoptions";
///////MYSQL loging Data ///////

//Grab the quantity selected from the form
$quantity = explode('|+', $_POST['quantity']);

//Set quantity to be only the numerical value not the add amount part
$quantity = $quantity[0];

//what is the name of the form? This comes from a hidden field
$formname = $_POST['formname'];

//instantiate mysqli object used for db queries
$mysqli = new mysqli("localhost", $user, $password, $database);

//If there is an error connecting to the database spit it out and tell me what the error is
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

//Start our prepared statment
//The query is:
//SELECT `$attribute` FROM `$formname` WHERE `quantity` = $quantity
if (!($stmt = $mysqli->prepare('SELECT ? FROM ? WHERE `quantity`=?')))
{
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

//Loop Through each Post value
foreach ($_POST as $name => $value)
{

//make sure we don't grab formname and quantity just get all the attributes and leave those alone
if ($name != 'formname' AND $name != 'quantity')
{

//Prepared statement do our bind and execute

//the attribute is the field values first part before the |+
//so explode the $value
$attribute = explode('|+', $value);

//Assign just the first part
$attribute = $attribute[0];

//do the binding string, string, float(double)
if (!$stmt->bind_param("sss", $attribute, $formname, $quantity))
{
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

//execute the query based on the binded paramaters
if (!$stmt->execute())
{
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

//Bind the results of the query temporarily to $result
$stmt->bind_result($result);

//If the result of the query is NOT equal to the value of the field make it fail validation
if ($result != $value)
{

//What to do if it fails validation
echo "Validation Failed You little Cheater!";
} else
{

//What to do if it passes validation
echo "Validation Success!";
}
//end if checking form values aginst DB values

}
//end if excluding formname and quantity
}
//end foreach loop

然后表单 HTML 本身只是为了阐明 |+ 之前的值的第一部分是列名称,而第二部分是要添加到价格中的多少。所以:value="columnName|+priceIncrease"

<form action="post.php" method="post">

<input type="hidden" name="formname" value="brochure">

<div>
<select name="quantity" id="ff_elem13">
<option value="1000|+550.00">1000</option>
<option value="2500|+900.00">2500</option>
</select>
</div>

<div>
<select name="fold" id="ff_elem14">
<option value="foldBi|+15.00">Bi Fold</option>
<option value="foldTri|+20.00">Tri Fold</option>
</select>
</div>


<div>
<select name="color" id="ff_elem15">
<option value="color40|+15.00"> Color 4:0</option>
<option value="color41|+20.00">Color 4:1</option>
</select>
</div>


<div>
<select name="rush" id="ff_elem16">
<option value="rush13|+15.00">Rush 1-3 Days</option>
<option value="rush24|+20.00">Rush 2-4 Days</option>
</select>
</div>

<input type="SUBMIT" value="Submit">
</form>

我收到的错误是

Prepare failed: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? WHERE quantity=?' at line 1

我该如何解决这个问题,以便我可以使用这个准备好的语句?我环顾四周,但从未见过有人使用 ?用于选择实际的表名称,并且主要是人们使用它来插入或更新值。

希望这足以解释我的情况,但如果我遗漏了什么,请告诉我,以便我可以纠正自己。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com