数组多值文本框

时间:2016-08-30 13:53:02

标签: php html

我在让阵列工作时遇到一些问题,我不确定如何正确构建foreach loop,以及如何正确地将其添加到我的查询中以便插入。

这是我对数组甚至PHP的第一次尝试,我需要帮助了解如何继续前进并且不要害怕数组。这个正常工作的重新设置应该采用6个文本值并将它们存储到表中D B 。我认为我的主要问题是使用这一行foreach($_POST['title'] as $idx => $title)来使事情有效但我可能错了。再次感谢。我看了一些示例,但仍然无法让我的代码完全工作或理解。

谢谢

HTML CODE

<form method="post" name="add_record" id="add_record" action="EnterNewAlbum.php">
  <input type="text" name="title[]" value="title" size="32" required="required" />
  <input type="text" name="artist[]" value="artist" size="32" required="required" />
  <input type="text" name="country[]" value="country" size="32" required="required" />
  <input type="text" name="company[]" value="company" size="32" required="required" />
  <input type="text" name="price[]" value="200" size="32" required="required" />
  <input type="text" name="year[]" value="100" size="32" required="required" />
<br /><br />
<input type="submit" action="EnterNewAlbum.php"  name="add_record" id="add_record" value="Add" />
</form>

PHP代码

<?php

if(isset($_POST['add_record'])) {
include 'dbconnection.php'; 
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
echo "button press test";

foreach($_POST['title'] as $idx => $title) {

$add_entry = mysqli_query($con , "INSERT INTO albumsID (`title`,`artist`,`country`,`company`,`price`,`year`)  VALUES ('".$title."', '" . $_POST['artist'][$idx] . "',  '" . $_POST['country'][$idx] . "' ,  '" . $_POST['company'][$idx] . "' ,  '" . $_POST['price'][$idx] . "'  ,  '" . $_POST['year'][$idx] . "'     ");


}


}


?>

2 个答案:

答案 0 :(得分:2)

在此,您希望它能为您提供帮助:

<?php 
    if(isset($_POST['title'])) {
        $title = $_POST['title'];

        foreach ($title as $key => $value) {

            //FOR YOUR CHECKING PURPOSE
            echo $value.'-'.
            $_POST['title'][$key].'-'.
            $_POST['artist'][$key].'-'.
            $_POST['country'][$key].'-'.
            $_POST['company'][$key].'-'.
            $_POST['price'][$key].'-'.
            $_POST['year'][$key];

            //ADD QUERY   
            $title_data = $_POST['title'][$key]; 
            $artist_data = $_POST['artist'][$key];
            $country_data = $_POST['country'][$key];
            $company_data = $_POST['company'][$key];
            $price_data = $_POST['price'][$key];
            $year_data = $_POST['year'][$key];

            $your_query = mysqli_query($con , "INSERT INTO albumsID (`title,artist,country,company,price,year`)  
            VALUES ('".$title_data."', '".$artist_data."', '".$country_data."', '".$company_data."', '".$price_data."', '" .$year_data. "'");
        }

    }
?>

答案 1 :(得分:0)

每个输入的类型为&#34; text&#34;您只能提交一个值,因此尝试将其存储在数组中并不会产生任何感觉。如果您使用相同的数组名称命名多个文本输入,则可以提交数组;例如:

<input type="text" name="values[]" value="first" />
<input type="text" name="values[]" value="second" />

但这似乎并不适合你的情况。

正确的代码可以是:

HTML:

<form method="post" name="add_record" id="add_record" 

action="EnterNewAlbum.php">
  <input type="text" name="title" value="title" size="32" required="required" />
  <input type="text" name="artist" value="artist" size="32" required="required" />
  <input type="text" name="country" value="country" size="32" required="required" />
  <input type="text" name="company" value="company" size="32" required="required" />
  <input type="text" name="price" value="200" size="32" required="required" />
  <input type="text" name="year" value="100" size="32" required="required" />
<br /><br />
<input type="submit" action="EnterNewAlbum.php"  name="add_record" id="add_record" value="Add" />
</form>

PHP:

<?php

if(isset($_POST['add_record'])) {
echo "button press test";


include 'dbconnection.php'; 
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
$add_entry = mysqli_query($con , "INSERT INTO albumsID (`title,artist,country,company,price,year`)  VALUES ('".$_POST['title']."', '" . $_POST['artist'] . "',  '" . $_POST['country'] . "' ,  '" . $_POST['company'] . "' ,  '" . $_POST['price'] . "'  ,  '" . $_POST['year'] . "'     ");
}
?>

值数组只能通过选择提交,其属性为multiple =&#34; multiple&#34;或者使用带有如下模板的复选框:

<input type="checkbox" name="checkboxes_results[]" value="value1" />
<input type="checkbox" name="checkboxes_results[]" value="value2" />
相关问题