-1

嘿伙计们,我一直在尝试编写代码,从 PHP 订单页面提交表单,以便它可以使用所需信息更新 MySQL 数据库

我在这里有订购 PHP 代码:

<form action="http://zim.cs.uow.edu.au/~ga420/order.php" method="post">
    <tr>
        <th>Shirts</th>
        <th>Quantity</th>
    </tr>
    <tr>
        <td>
            <br />
            <input type="checkbox" name="items" value="SH01" />
            <label for="rd1">Obey T-Shirt: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH02" />
            <label for="rd1">Obey Professor: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH03" />
            <label for="rd1">Hustle T-Shirt: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH04" />
            <label for="rd1">Hip-Hop Support: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH05" />
            <label for="rd1">90's Shirt: $9.99</label>
            </div>
            <br />
            <input type="checkbox" name="items" value="SH06" />
            <label for="rd1">DOPE Shirt: $9.99</label>
            </div>
            <br />
            <br />
        </td>
        <td>
            <br />
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <input type="text" name="qty" size="2" />
            <br/>
            <br />
        </td>
    </tr>
    <tr>
        <td>
            <br />
            <input type="checkbox" name="items[]" value="SO1" />
            <label for="rd1">Shoe - Red Lace: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO2" />
            <label for="rd1">Shoe - Red High Top: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO3" />
            <label for="rd1">Shoe - White: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO4" />
            <label for="rd1">Shoe - Black: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO5" />
            <label for="rd1">Shoe - Black High Top: $19.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SO6" />
            <label for="rd1">Red Basketball: $19.99</label>
            </div>
            <br />
            <br />
        </td>
        <td>
            <br />
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <br />
        </td>
    </tr>
    <tr>
        <td>
            <br />
            <input type="checkbox" name="items[]" value="SN1" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN2" />
            <label for="rd1">Snapback: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN3" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN4" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN5" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <input type="checkbox" name="items[]" value="SN6" />
            <label for="rd1">Snapback Bullets: $29.99</label>
            </div>
            <br />
            <br />
        </td>
        <td>
            <br />
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <input type="text" name="qty[]" size="2" />
            <br/>
            <br />
        </td>
    </tr>
    </tr>
    </table>
    <br />
    <input type="submit" name="submit">
</form>

<?php

if (isset($_POST['submit'])){

    $conn = mysql_connect('url','username','password');
    if (!$con){
        die("Could Not Connect: " . mysql_error());
    }

    mysql_select_db("db",$conn);

    $sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES (null,$_POST[items]','$_POST[qty]')";

    mysql_query($sql, $con);

mysql_close($con);
}

?>

显然我的用户名和密码我不会显示,但是当我点击提交按钮时它说它无法连接。

您可以在这个网站上自己尝试表格:http: //zim.cs.uow.edu.au/~ga420/order.php

当我清楚地输入正确的细节时,怎么会说我无法连接。

任何人都可以帮忙吗?这让我很紧张:'(

非常感谢您的帮助!!

4

2 回答 2

1

如果您在所有字段中都具有相同的名称“数量”,则需要序列化所有输入/数量。如果您没有得到有关此的答案,我稍后会更新我的答案。

您的代码也有小错误,请注意:

VALUES (null,'$_POST[items]','$_POST[qty]')";'- 你在 $_POST[items] 之前失踪了

$conn = mysql_connect('zim.cs.uow.edu.au','username','password');- $conn 与 2 "n"

所以,一个更正的版本是:

<?php

if (isset($_POST['submit'])){

$con = mysql_connect('url','username','password');
if (!$con){
    die("Could Not Connect: " . mysql_error());
}

mysql_select_db("db",$con);

$sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES (null,'$_POST[items]','$_POST[qty]')";

mysql_query($sql, $con);

mysql_close($con);
}

?>
于 2013-06-21T09:04:00.787 回答
0

Change to this. You did some mistake on $conn and $con. Then, '$_POST[items]','$_POST[qty]' in query.

if (isset($_POST['submit'])){

$conn = mysql_connect('url','username','password');
if (!$conn){
    die("Could Not Connect: " . mysql_error());
}

mysql_select_db("db",$conn);

$sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES      (null,'$_POST[items]','$_POST[qty]')";

mysql_query($sql, $conn);

mysql_close($conn);
}

Hope this help

于 2013-06-21T08:42:30.277 回答