Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迭代多个PHP变量,为每个For SQL语句赋值_Php_Sql - Fatal编程技术网

迭代多个PHP变量,为每个For SQL语句赋值

迭代多个PHP变量,为每个For SQL语句赋值,php,sql,Php,Sql,所以我一直在四处寻找,不知道我的问题要找什么。基本上,我想做我在代码中已经做过的事情(我已经对成分变量进行了硬编码),而是创建一个循环来迭代并将所有可用成分分配给相应的变量,并相应地更新我的SQL记录。最好的办法是什么 我想让它更高效,所以我的SQL DB中不会出现空列,而是如果URL API中的配料字符串为空,则在数据库中保留为null,URL API由配料组成。php?配料[]=苹果和配料[]=胡椒和配料[]= $ingredients = $_GET['ingredient'];

所以我一直在四处寻找,不知道我的问题要找什么。基本上,我想做我在代码中已经做过的事情(我已经对成分变量进行了硬编码),而是创建一个循环来迭代并将所有可用成分分配给相应的变量,并相应地更新我的SQL记录。最好的办法是什么

我想让它更高效,所以我的SQL DB中不会出现空列,而是如果URL API中的配料字符串为空,则在数据库中保留为null,URL API由配料组成。php?配料[]=苹果和配料[]=胡椒和配料[]=

$ingredients = $_GET['ingredient'];
    $ingredient1 = $ingredients[0];
    $ingredient2 = $ingredients[1];
    $ingredient3 = $ingredients[2];
    $ingredient4 = $ingredients[3];
    $ingredient5 = $ingredients[4];
    $ingredient6 = $ingredients[5];
    $ingredient7 = $ingredients[6];
    $ingredient8 = $ingredients[7];
    $ingredient9 = $ingredients[8];
    $ingredient10 = $ingredients[9];
    $ingredient11 = $ingredients[10];
    $ingredient12 = $ingredients[11];
    $ingredient13 = $ingredients[12];
    $ingredient14 = $ingredients[13];
    $ingredient15 = $ingredients[14];
$wait_time = $_GET['wait_time'];

$query = "INSERT INTO menu_items (rest_id,item_name,item_genre,item_price,item_descript,ingredient1,ingredient2,ingredient3,ingredient4,ingredient5,ingredient6,ingredient7,ingredient8,ingredient9,ingredient10,ingredient11,ingredient12,ingredient13,ingredient14,ingredient15,wait_time) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

$stmt =  $mysqli->prepare($query);
    if ($stmt) {
        $stmt->bind_param('issdssssssssssssssssi',$rest_id,$item_name,$item_genre,$item_price,$item_descript,$ingredient1,$ingredient2,$ingredient3,$ingredient4,$ingredient5,$ingredient6,$ingredient7,$ingredient8,$ingredient9,$ingredient10,$ingredient11,$ingredient12,$ingredient13,$ingredient14,$ingredient15,$wait_time);
        $stmt->execute();
            echo json_encode(array('itemID' => $mysqli->insert_id, 'error' => $mysqli->error));
        $stmt->close();
    } else {
        echo json_encode(array('itemID' => null, 'error' => $mysqli->error));
    }

根据文档,您不能动态使用bind_param,这可能是我认为MySQLi过时的原因:D 唯一的解决方案是使用call\u user\u func\u array()

我不支持bind_param,我不推荐它,而不是自己检查它,我不推荐MySQLi(但这比mysql_*函数好)

但事实上,您永远不应该影响从数组到许多变量的值。
此外,您应该始终检查用户输入,您不能信任任何输入变量,甚至管理员(他们会出错)。

因为您有一个单独的包含成分的表,所以您可以创建一个简单的连接表。下面是一个设计示例

id |  menuId | ingredientId
----------------------------
 1 |    1    |     2
 2 |    1    |     3
其中,
menuId
是菜单表的FK,而
ingredientId
是配料数据库的FK。上面的示例意味着您有一个Id为的菜单,其中还包含成分2和3。
id
列是可选的。如果您不需要,请随意移除它

然后,为具有下一个HTML结果的成分设置选择框

<option value="2">pepper</option>
<option value="3">beef</option>
请在两个查询中检查bindparam字符串表示法。可能缺少或输入了不正确的字符


但是,如果您使用了,事情会更容易。但是您应该对上面的内容很满意。

我应该使用关系数据库,尤其是使用,这将使您的解决方案比您试图实现的更容易。(在我看来不那么难看)我看不出这有什么帮助,但我对关系数据库有点陌生。。。我有一个单独的表叫做配料,但它只是用来选择行和填充应用程序的配料搜索菜单的一种方法。我现在需要查看PDO。。。有几个人推荐了这个,看来我可以做得更多。你的回答实际上解决了一些我甚至认为我不需要解决的问题,哈哈+1在这种情况下PDO声明会是什么样子?我以前从未使用过PDO,还是我要求太多?
<option value="2">pepper</option>
<option value="3">beef</option>
$ingredients = $_GET['ingredient'];
// insert menu item (not sure if it's a part of the ajax ...)
$newMenuQuery = "INSERT INTO menu_items (rest_id,item_name,item_genre,item_price,item_descript,wait_time) VALUES (?,?,?,?,?,?)";
// query insert for junction table
$newIngredients = "INSERT INTO menuHasIngredients VALUES (?, ?)";

// prepare statement menu
$stmtMenu = $db->prepare($newMenuQuery);

// insert menu
$stmtMenu->bind_param('issdsi',$rest_id,$item_name,$item_genre,$item_price,$item_descript,$wait_time);
$stmtMenu->execute();
// get inserted id (insert_id gives last id given back
$newId = $db->insert_id;
// close connection
$stmtMenu->close();

// check if success
if($newId) {
    // prepare statement ingredients
    $stmtIngredients = $db->prepare($newIngredients);
    // use junction table
    $count = count($ingredients);
    for($i = 0; $i < $count; $i++) {
        $stmtIngredients->bind_param('ii', $newId, $ingredients[$i]);
        $stmtIngredients->execute();
    }
    // handle return good or error

    // close connection
    $stmtIngredients->close();
}
else {
   // send error back that menu inserting has failed
}
// close connection