如何使用数组进行表单输入和更新各自的数据库记录

时间:2009-12-14 07:36:02

标签: php database arrays forms input

我是PHP新手...我想知道如何从mySQL填充表单。我在输入部分难以接受,我试图让用户为每条记录选择单选按钮/复选框的选项。提前感谢任何人的帮助!

TC

以下是我的代码片段:

<?php

$mQuantity = 1;

$con = mysql_connect("localhost","t","c");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("tc", $con);

/*
    if request.form("itemname")<>" " then
        cSQLAcct =  "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' and Item like '%" & Request.Form("ITEMNAME") & "%'  ORDER BY FOODTYPE, ITEM"
    else
        cSQLAcct =  "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' ORDER BY FOODTYPE, ITEM"
    end if
*/

// retrieve form data 
$input = $_POST['itemname'];
echo $_POST['ITEM'];
$mItem = $_POST['ITEM'];
$mPrice = $_POST['PRICE'];
$mQuantity = $_POST['QUANTITY'];
$mOrderTotal = 0;
$mSide0 = '1';
$mOil = '1';
$mStarch = '1';
$mSalt = '1';

// use it 
echo "You searched for: <i>$input</i>"; 
echo $mSessionID;

mysql_query("INSERT INTO OrderQueue (SessionID,item,quantity,price,no_oil,no_starch,no_salt,side0) VALUES ('$mSessionID','$mItem','$mQuantity','$mPrice','$mOil','$mStarch','$mSalt','$mSide0')");

/*
$sql="SELECT * FROM Restaurant_Menus
WHERE
Item like '%$input%'";
*/


$sql="SELECT * FROM OrderQueue
WHERE
SessionID = '$mSessionID'";

$result = mysql_query($sql); 

//('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

//echo $input;
//echo $sql;

/*
if ($input!=" "){
    $result = mysql_query($sql); 
    }

//          cSQLAcct =  "SELECT * FROM Restaurant_Menus WHERE Restaurant_ID='" & mRestaurant_ID & "' and Item like '%" & Request.Form("ITEMNAME") & "%'  ORDER BY 
else
{   $result = mysql_query("SELECT * FROM Restaurant_Menus");
}
*/






$c=1;
$class[1] = 'odd';
$class[2] = '';
$array_no_oil = array();
$array_no_salt = array();
$array_no_starch = array();
$array_rice = array();




echo "<table border='1' width=500px>
<tr>
<th></th>
<th align=left>Item</th>
<th align=right>Price</th>
<th align=right>Quantity</th>
</tr>";

//<tr onMouseOver="this.bgColor = '#F3EB49'" onMouseOut ="this.bgColor = '#DDDDDD'" bgcolor="#DDDDDD">

while($row = mysql_fetch_array($result))
    {
    //echo '<tr class="'.$class[$c].'" onMouseOver='#F3EB49' onMouseOut ='#DDDDDD' bgcolor="#DDDDDD" >';
    //echo "<tr class='odd'>";
    //echo '<tr onMouseOver="this.bgColor = '#F3EB49'" onMouseOut ="this.bgColor = '#DDDDDD'" bgcolor="#DDDDDD">'
?>

    <TR onMouseover="this.bgColor='#FFC000'"onMouseout="this.bgColor='#DDDDDD'">

  <?php
/*
        <form action="Orders.asp" method="post" target="_top" name="LogonForm">
        <td><font size = 2>

        <!--<%= mQuantity %>-->        
        <INPUT TYPE="TEXT" NAME="QUAN" VALUE="1" SIZE=2>
        </font></td>
        <td width=50px><font size = 2>
        <INPUT TYPE=HIDDEN NAME=ITEM VALUE="<% =mItem %>">
        <INPUT TYPE=HIDDEN NAME=PRICE VALUE=<% =mPrice %>>
        <INPUT TYPE=HIDDEN NAME=RID VALUE= <% =mRestaurant_ID %>>       
        <INPUT TYPE=HIDDEN NAME=ADDITEM VALUE = "1">
        <input id="Choices" class="findit" type="submit" value ="Order" />
        </form>
*/
?>


<?php   



// Obtain list of images from directory 
//$img = getRandomFromArray($imgList);
}

?>



    <?php

    if ($row['Picture']!=" "){
        echo "<td><a><img src='images/".$row['Picture'].".JPG' height=50px></a></td>";
    }
    else{
        echo "<td></td>";
    }

    echo "<td width=200><b>" . $row['Item'] . "</b><br>


    <input class='dropwidth' type='radio' name='$array_rice' value='1' selected>White Rice<br>
    <input type='radio' name='$array_rice' value='2'>Pork Fried Rice<br>
    <input type='radio' name='$array_rice' value='3'>Brown Rice<br>
    <input type='checkbox' name='$array_no_oil' value='1' />No Oil
    <input type='checkbox' name='starch' value='no oil' />No Starch
    <input type='checkbox' name='salt' value='no salt' />No Salt
    </td>";

    echo "<td width=50 align=right>" . number_format($row['Price'],2) . "</td>";
    $mQuantity = "'" . number_format($row['Quantity'],0) . "'";
    $mPrice = "'" . number_format($row['Price'],2) . "'";
    $mLineItemTotal = $row['Quantity'] * $row['Price'];
    $mOrderTotal = (number_format($mOrderTotal,2) + number_format($mLineItemTotal,2));
    echo $mOrderTotal;
    $mLineItemTotal2 = "'". number_format($mLineItemTotal,2) . "'";
    //echo "<td>" . $mQuantity. "</td>";
?>        
      <form action="orders.php" method="post" target="_top" name="LogonForm">
        <td width="50" align=right><font size = 2>

          <!--<%= mQuantity %>-->

          <!--<INPUT TYPE="TEXT" NAME="QUANTITY" VALUE=<?php $mQuantity; ?>>-->
          <INPUT TYPE="TEXT" NAME="QUANTITY" VALUE=<?php echo $mQuantity.";" ?>/>
        </font></td>



        <?php echo "<td width=50 align=right>" . $mLineItemTotal . "</td>";?>

        <!--<td width=50px><font size = 2>-->
        <!--<INPUT TYPE="TEXT" NAME="LINEITEMTOTAL" VALUE=<?php echo $mLineItemTotal.";" ?> WIDTH=10/>-->
          <INPUT TYPE=HIDDEN NAME=ITEM VALUE=<?php $mItem ?> />
          <INPUT TYPE=HIDDEN NAME=PRICE VALUE=<?php $mPrice ?>/>
          <INPUT TYPE=HIDDEN NAME=RID VALUE=<?php $mRestaurant_ID ?>/>      
          <INPUT TYPE=HIDDEN NAME=ADDITEM VALUE = "1">
          <!--<input id="Choices" class="findit" type="submit" value ="Order" />-->
        </form>     



  <?php 

    echo "</tr>";
    if($c==2) $c=0;
    $c++;
    }
echo "</table>";



    echo "<div>".$mOrderTotal."</div>";


mysql_close($con);
?> 

1 个答案:

答案 0 :(得分:1)

您应该将HTML表单元素命名为:MyElement []将它们用作数组时。例如:

<form method="post" name="myForm" action="myForm.php">
    <select multiple name="myFormSelectMultiple[]">
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
    </select>
</form>

在这个选择的多个示例中,当选择前两个项目然后发布时,此表单将产生类似于以下内容的输出:

array('myFormSelectMultiple' => array(1,2)); 

希望这有帮助!