如何使用Web表单使用数组向mysql数据库输入数据?

时间:2011-05-12 18:49:36

标签: php mysql database arrays

这是目标表:

**trophies**
game_name
tr_name
tr_description
tr_color
tr_ach_value

以下是表格:

    <form action="update.php" method="post">
Game Name:  <input name="game_name" type="text" size="25" maxlength="255" /><br></br>
Release Date:  <input name="release_date" type="text" size="25" /><p></p>
</p>
<p>&nbsp;</p>
<p>Trophies:</p>
Trophy Name:    <input name="tr_name" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_color">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr_ach_value" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr_name" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_color">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr_ach_value" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr_name" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_color">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr_ach_value" type="text" size="4" maxlength="4" /><p></p>


Trophy Name:    <input name="tr_name" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_color">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr_ach_value" type="text" size="4" maxlength="4" /><p></p>

<input name="submit" type="button" value="submit" />
</form>

我正在尝试获得多个奖杯记录,每个记录都插入到表奖杯中。我会用这个数组吗?如果是这样,你能告诉我一个如何的例子吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

将输入字段命名为

Trophy Name:    <input name="trophy[0][name]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="trophy[0][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="trophy[0][color]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="trophy[0][ach_value]" type="text" size="4" maxlength="4" /><p></p>

每个新数据集只需增加一个数字。 希望有所帮助。

编辑:

PHP代码如下:

<?php
    foreach($_POST['trophy'] as $trophy) {
        $sql = "INSERT INTO trophies (game_name, tr_name, tr_description, tr_color, tr_ach_value) VALUES ('".$_POST['game_name']."', '".$trophy['name']."', '".$trophy['desc']."', '".$trophy['color']."', '".$trophy['ach_value']."')";
        mysql_query($sql);
    }
?>

答案 1 :(得分:1)

使用name []作为字段名称

  <imput name="tr_name[]" ...

在php中所有使用名称中的[] final的字段都是服务器端的数组,所以在你的php中你可以在$ _POST ['tr_name']变量等中迭代你的倍数tr_name。