我创建了两个php文件,其中包含自助餐厅订单的代码以及用于在我的数据库中插入已发布值的相应代码行。我在这里介绍他们。
代码为addorder_form.php
**<?php
db_connect();
$cats=array("Kafedes", "Rofhmata", "Pota", "Snack/Glyka");
$arrlength=count($cats);
for($i=0;$i<$arrlength;$i++) {
$sql = mysql_query('SELECT title FROM products WHERE cname="'.$cats[$i].'"') or die(mysql_error());
echo '<div id="main_content">';
echo "<h4 style=color:#800000> ".$cats[$i]."</h4>";
echo "<br />";
while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
echo "<div id='center' style='align:center'>";
echo "<input style='text-align:right;' type='checkbox' action='addorder.php' name='products[]' value='".$row["title"]."'>".$row["title"];
echo '</div>';
echo ' <div id="center_side" style="float:right"><form "method="post" action="addorder.php"><input type="text" size="4" padding-left="0.2em" name="quantity"/>';
echo '</div>';
echo '</div>';
echo '<br />';
}
}
echo '<form name="addorder" method="" action="addorder.php" onclick="addorder.php">';
echo '<input type="submit" value="Add order" style="float: right;"><br/>';
echo '</form>';
?>**
代码为addorder.php
**<?php
include_once("buzzcafe_fns.php");
do_html_header("");
$quantity = '';
$title = '';
if (isset($_POST['quantity']) && isset($_POST['products'])) {
if(isset($_POST["Submit"])) {
$quantity = $_POST['quantity'];
$title = $_POST['products'];
if($_POST["Submit"] == "Submit")
{
for ($i=0; $i<sizeof($title); $i++) {
db_connect();
$insertOrder = mysql_query("INSERT INTO orders VALUES('".$title[i]."','".$quantity."')")or die(mysql_error());
}
echo "Record inserted";
}
}
}
?>**
当我运行它们时,我没有任何语法错误,但它不起作用。在buzzcafe_fns.php文件中设置了db_connect(),我已经包含了它,并且检查它是否正常工作。如何使我的“INSERT INTO”工作?
答案 0 :(得分:1)
You can use another insert query
First You will check if $title variable array or not.
Next Using For loop
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("yourdatabasename here" ,$con);
$count=count($title);
for($i=0;$i<$count;$i++){
mysql_query("INSERT INTO orders SET title='".$title[$i]."', quantity='".$quantity."'");
}
?>