如何使用PHP将多个数组插入数据库

问题描述:

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

<?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());
} 


?>

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

form.php p>

 &lt; form method =“POST”action =“form.php”&gt; 
&lt  ; input type =“text”name =“waw”/&gt; 
&lt; input type =“submit”/&gt; 
 
&lt;?php 
 $ i = 0; 
 
while($ i&lt; $  _POST ['waw'])
 {
?&gt; 
&lt; / form&gt; 
 
&lt; form method =“POST”action =“input.php”&gt; 
&lt;! - 人#1  - &gt; 
 
&lt; input type =“text”name =“username []”/&gt; 
 
&lt; input type =“text”name =“phonenum []”/&gt; 
 
&lt;  ; input type =“text”name =“add []”/&gt; 
 
 
&lt;?php 
 $ i ++; 
} 
?&gt; 
&lt; input type =“submit”/&gt  ; 
&LT; /形式&GT; 
 代码>  PRE> 
 
 

input.php p>

 &LT; PHP 
 
 $的 username =“maizakath”; 
 $ password =“12345”; 
 $ database =“tryinsert”; 
mysql_connect(localhost,$ username,$ password); 
 @ mysql_select_db($ database)或die(“&lt;  b&gt;无法指定数据库&lt; / b&gt;“); 
 
 
 
 $ sql_start ='INSERT INTO`mytable` VALUES';  
 $ sql_array = array();  
 $ queue_num = $ _POST ['waw'];  
foreach($ _POST ['username'] as $ row =&gt; $ name)
 {
 $ username = $ name; 
 $ phonenum = $ _POST ['phonenum'] [$ row]; 
 $  add = $ _POST ['add'] [$ row]; 
 $ sql_array [] ='('。$ username。','。$ phonenum。','。$ add。')';  
if(count($ sql_array)&gt; = $ queue_num)
 {
mysql_query($ sql_start.implode(',',$ sql_array));  
 $ sql_array = array();  
} 
} 
if(count($ sql_array)&gt; 0)
 {
mysql_query($ sql_start.implode(',',$ sql_array))或die(mysql_error()); 
} 
  
 
?&gt; 
  code>  pre> 
  div>

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

<?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.

EDIT(Comments):

  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;

  2. 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);

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

  4. 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.

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

I Had tried its work fine but when inserting into database it will insert null value also if user did'nt filled up the whole field.

I have stored two arrays in one table at the same time. Hope this will help you.

<?php
    $i = 0;
    foreach($element_name as $element_names){
        $element_value = $_POST['element_value'];
        $element_names_insert = mysql_query("insert into wp_remote_fields
        set
            remote_fields = '".$element_names."',
            remote_values = '".$element_value[$i]."'
            ");     
        $i++;           
    }
?>