如何使用PHP将多个数组插入数据库 [英] how to insert multiple array into database using PHP

查看:66
本文介绍了如何使用PHP将多个数组插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助。我们的php代码似乎是什么问题?我们似乎无法将数据插入数据库。我只是一个初学者,我的任务是将多个数据存储到我们数据库中的多个数组中。我们实际上要做的是输入一个数字(例如5),该页面应显示5个表格。每个表格将由姓名地址和电话号码组成。之后,我们将其提交到我们的数据库。我们已经控制了很多表格可以显示,但是我们无法存储插入的数据。谁能帮我们吗?谢谢。

I need help. What seems to be the problem with our php codes? We can't seem to insert our data into our database. I'm just a beginner and I'm tasked to store multiple data into multiple arrays into our database. What we're actually doing is to enter a number (ex: 5) and 5 forms should show within that page. each form would consist of name address and tel phone number. after that we submit it to our database. We have already controlled hot many forms to show but we weren't able to store the data inserted. Can anyone help us please? Thank you.

form.php

<form method="POST" action="form.php">
<input type="text" name="waw" />
<input type="submit" />

<?php 
$i=0;

while ($i<$_POST['waw'])
{
?>
</form>

<form method="POST" action="input.php">
<!-- Person #1 -->

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

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

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


<?php 
$i++;
}
?>
<input type="submit" />
</form>

input.php

input.php

<?php

$username="maizakath";
$password="12345";
$database="tryinsert";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");



$sql_start = 'INSERT INTO `mytable` VALUES '; 
$sql_array = array(); 
$queue_num = $_POST['waw']; 
foreach ($_POST['username'] as $row=>$name)
{
$username = $name;
$phonenum = $_POST['phonenum'][$row];
$add = $_POST['add'][$row];
$sql_array[] = '(' . $username . ', ' . $phonenum . ', ' . $add . ')'; 
if (count($sql_array) >= $queue_num)
{ 
mysql_query($sql_start . implode(', ', $sql_array)); 
$sql_array = array(); 
}
}
if (count($sql_array) > 0) 
{
mysql_query($sql_start . implode(', ', $sql_array))or die(mysql_error());
} 


?>


推荐答案

我已修改您的代码以使其正常工作:

I've modified your code to make it work:

form.php

<form method="POST" action="form.php">
<input type="text" name="waw" />
<input type="submit" />
</form>

<form method="POST" action="input.php">
<?php 
$i=0;

while ($i<$_GET['waw'])
{
?>

<!-- Person #1 -->

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

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

<input type="text" name="add[]" /><br />


<?php 
$i++;
}
?>
<input type="submit" />
</form>

input.php

input.php

<?php


$username="maizakath";
$password="12345";
$database="tryinsert";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");



$sql_start = 'INSERT INTO `mytable` VALUES '; 
$sql_array = array(); 
$queue_num = $_POST['waw']; 
foreach ($_POST['username'] as $row=>$name)
{
$username = $name;
$phonenum = $_POST['phonenum'][$row];
$add = $_POST['add'][$row];
$sql_array[] = '("' . $username . '", "'.$phonenum.'", "'.$add.'")'; 
if (count($sql_array) >= $queue_num) {
  $query_single=$sql_start . implode(', ', $sql_array);
  mysql_query($query_single); 
  $sql_array = array(); 
}
}

if (count($sql_array) > 0)  {
  $query = $sql_start . implode(', ', $sql_array);
  mysql_query($query)or die(mysql_error());
}

?>

工作正常。我刚刚在本地计算机上对其进行了测试。

It works fine. I've just tested it on my local machine.

编辑(评论)


  1. 在input.php中使用变量$ queue_num是没有意义的,因为此变量仅在form.php脚本中可用(以另一种形式放置的哇输入,提交给文件form.php,而不是input.php)。因此 if(count($ sql_array)> = $ queue_num)块的工作方式是错误的;

  1. Usage of variable $queue_num in input.php is senseless, because this variable is available only in form.php script('wow' input placed in another form, which is submitted to file form.php, not input.php). So if (count($sql_array) >= $queue_num) block works wrong;

检查数据库连接的配置设置(正如我在评论中所写的那样,您必须定义名称为'localhost'的常量或用引号将单词localhost括起来);

Check your config settings for the database connection(as I've wrote in comment, you have to define constant with name 'localhost' or enclose word localhost with quotes);

我修改了您的表单,因为它的结构有误;

I've modified your form, because it had wrong structure;

我不明白在form.php中创建第一个表单的目的。 / p>

I didn't understand the objective of creating first form in form.php.

您可以修改此代码,使其更适合您的情况。但是,第一个尝试使用它的人。

You can modify this code to make it more appropriate for your case. But firsat of all try to use this one.

注意。使用 var_dump()函数可在调试过程中查看$ _POST数组,以了解可用的变量。

Note. Use var_dump() function to see your $_POST array during debugging to understand, what variables are available.

这篇关于如何使用PHP将多个数组插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆