Iterating Through Multiple PHP Variables, Assigning Values To Each For SQL Statement -
so been searching around , not sure regarding question. basically, wanting have going on in code (i have ingredient variables hardcoded), instead create single loop iterate through , assign available ingredients corresponding variable , update sql record accordingly. what's best way go this?
i want make more efficient, don't end empty columns in sql db, instead leave null in database if ingredient strings empty in url api, consists of ingredients.php?ingredient[]=apple&ingredient[]=pepper&ingredient[]=.....
$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 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)); }
since have separate table ingredients, can create simple junction table. here example of design
id | menuid | ingredientid ---------------------------- 1 | 1 | 2 2 | 1 | 3
where menuid
fk menu table , ingredientid
fk ingredients db. above example means have menu id, contains ingredient 2 , 3. id
column optional. feel free remove if don't need it.
then, set select box ingredients has next html result
<option value="2">pepper</option> <option value="3">beef</option>
you can selecting both id
, name
ingredients table, , use id
value field.
now, @ php, send items values filled in. (so not send 15 if 4 got chosen. send 4)
$ingredients = $_get['ingredient']; // insert menu item (not sure if it's part of ajax ...) $newmenuquery = "insert menu_items (rest_id,item_name,item_genre,item_price,item_descript,wait_time) values (?,?,?,?,?,?)"; // query insert junction table $newingredients = "insert 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(); // inserted id (insert_id gives last id given $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 or error // close connection $stmtingredients->close(); } else { // send error menu inserting has failed } // close connection
please check bindparam string notation @ both queries. might have missed or entered incorrect character.
however, if have used pdo, things more easier. should fine above.
Comments
Post a Comment