PHP POST 多行到 MySQL

发布于 2024-10-14 18:20:40 字数 397 浏览 2 评论 0原文

我有一个名为“fruits”的表:

id      fruit
1       Apple
1       Banana
2       Apple
2       Apple
2       Pear

我希望能够使用 PHP 一次添加多行。

我尝试使用两组两个文本框(用于 id 和水果),但仅使用最后一组文本框插入 1 行。

编辑:

如果我有这样的表,我该怎么办:

id      fruit       taste
1       Apple       Good
1       Banana      Okay
2       Apple       Good
2       Apple       Bad
2       Pear        Good

I have a table named fruits:

id      fruit
1       Apple
1       Banana
2       Apple
2       Apple
2       Pear

I would like to be able to add several rows at once using PHP.

I tried having two sets of two textboxes (for id and fruit) but that only insert 1 row, using the last set of textboxes.

EDIT:

Then what do I do if I have a table like this:

id      fruit       taste
1       Apple       Good
1       Banana      Okay
2       Apple       Good
2       Apple       Bad
2       Pear        Good

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傾城如夢未必闌珊 2024-10-21 18:20:40

您需要使用括号 [] 正确命名文本框以形成一个数组,然后循环遍历 POST 后的数组以插入数据。确保在使用 mysql_real_escape_string 或准备好的查询运行查询之前转义数据。

编辑:更新示例,因为OP将信息附加到问题中。

在您提供的扩展示例中,taste 看起来只有几个选择。在这种情况下,我将在文本框上使用

表格
显着变化:使用 PHP 中的简单循环,您可以指定要显示的字段数量,并轻松地将其他选项添加到口味选择框中。我在 [] 中放置了一个任意数字,以确保我们在处理表单时将水果与味道联系起来。

<?php
$n = 5; //Number of fields to show
$tastes = array('Great', 'Good', 'Okay', 'Bad', 'Horrible');

for($i=0;$i<$n;$i++) {
  echo '<p>';
  echo '<input type="text" name="fruit['.$i.']" /> ';
  echo '<select name="taste['.$i.']">';
  foreach($tastes as $t) {
    echo '<option value="'.htmlentities($t).'">'.$t.'</option>';
  }
  echo '</select>';
  echo '</p>';
}
echo '<input type="submit" name="submit" value="Submit" />';
?>

处理代码
显着变化:我正在检查以确保水果具有价值,否则它不会插入。我使用该任意值(即数组键 $k)来选择与水果匹配的口味。

<?php
if($_POST['submit']) {
  foreach($_POST['fruit'] as $k => $v) {
    if(!empty($v)) {
      $query = "INSERT INTO fruits (fruit, taste) VALUE ('".mysql_real_escape_string($v)."', '".mysql_real_escape_string($_POST['taste'][$k])."')";
      mysql_query($query);
    }
  }
}
?>

You need to name your text boxes properly using brackets [] to form an array, then loop through your POST'ed array to insert the data. Be certain you're escaping data before running your query by using mysql_real_escape_string or prepared queries.

EDIT: Updating examples because OP appended information to the question.

In the extended example you've provided, it looks like the taste only has a few choices. In that case, I would use a <select> element over a text box. Check out my examples for how, personally, I would do it.

Form
Notable changes: Using a simple loop in PHP, you can specify the number of fields you want to show and easily add other options to the taste select box. I'm placing an arbitrary number within the [] to be certain we link the fruit to the taste when we process the form.

<?php
$n = 5; //Number of fields to show
$tastes = array('Great', 'Good', 'Okay', 'Bad', 'Horrible');

for($i=0;$i<$n;$i++) {
  echo '<p>';
  echo '<input type="text" name="fruit['.$i.']" /> ';
  echo '<select name="taste['.$i.']">';
  foreach($tastes as $t) {
    echo '<option value="'.htmlentities($t).'">'.$t.'</option>';
  }
  echo '</select>';
  echo '</p>';
}
echo '<input type="submit" name="submit" value="Submit" />';
?>

Process Code
Notable changes: I'm checking to make sure the fruit has a value, otherwise it won't insert. I'm using that arbitrary value (which is the array key $k) to select the taste that matches the fruit.

<?php
if($_POST['submit']) {
  foreach($_POST['fruit'] as $k => $v) {
    if(!empty($v)) {
      $query = "INSERT INTO fruits (fruit, taste) VALUE ('".mysql_real_escape_string($v)."', '".mysql_real_escape_string($_POST['taste'][$k])."')";
      mysql_query($query);
    }
  }
}
?>
坏尐絯℡ 2024-10-21 18:20:40

使用内爆功能的单次插入。

你的 HTML

<input type="text" name="fruit[]" />
<input type="text" name="fruit[]" />
<input type="text" name="fruit[]" />

你的 PHP

foreach($_POST['fruit'] as $fruit) $values[] = "('".mysql_real_escape_string($fruit)."')";
if(count($values) > 0) mysql_query("INSERT INTO fruits (fruitName) VALUES " . implode(',', $values));

Single insert using implode function.

Your HTML

<input type="text" name="fruit[]" />
<input type="text" name="fruit[]" />
<input type="text" name="fruit[]" />

Your PHP

foreach($_POST['fruit'] as $fruit) $values[] = "('".mysql_real_escape_string($fruit)."')";
if(count($values) > 0) mysql_query("INSERT INTO fruits (fruitName) VALUES " . implode(',', $values));
浅唱ヾ落雨殇 2024-10-21 18:20:40

MySQL 支持在单个查询中进行多次插入:

INSERT INTO table (field1, field2) VALUES (values1a, values2a), (values1b, values2b), etc...

MySQL supports multiple inserts in a single query:

INSERT INTO table (field1, field2) VALUES (values1a, values2a), (values1b, values2b), etc...
机场等船 2024-10-21 18:20:40

您可以创建一个 html 元素数组,例如

在 html

<form action='save.php' method='post'/>
<input name='fruits[]' value='' />
<input name='fruits[]' value='' />
<input name='fruits[]' value='' />
</form>

然后在 save.php 中

$fruits = '';
foreach($_POST['fruits'] as $fruit) {
 $fruits .= "({$fruit}),"
}

 $fruits = rtrim($fruits,',');

$sql = "INSERT INTO fruits(fruit) VALUES {$fruits}";

You can make an html array of elements for example

in html

<form action='save.php' method='post'/>
<input name='fruits[]' value='' />
<input name='fruits[]' value='' />
<input name='fruits[]' value='' />
</form>

Then in save.php

$fruits = '';
foreach($_POST['fruits'] as $fruit) {
 $fruits .= "({$fruit}),"
}

 $fruits = rtrim($fruits,',');

$sql = "INSERT INTO fruits(fruit) VALUES {$fruits}";
吻安 2024-10-21 18:20:40

在插入语句中只需添加值,如下所示:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

因此,对于您的示例,

插入水果(水果)值('橙色'),('柠檬'),('酸橙');

应该有效。

http://dev.mysql.com/doc/refman/5.5/en /插入.html

In your insert statement just add the values, like so:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

so for your example,

INSERT INTO fruits (fruit) VALUES ('Orange'),('Lemon'),('Lime');

should work.

http://dev.mysql.com/doc/refman/5.5/en/insert.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文