user6774638
user6774638

Reputation:

Array Multiple Values Text-Box

I'm having some issues with getting my array to work , I'm not sure how to structure my foreach loop correctly and also how to correctly add it to my query so it will insert .

This is my first attempt at arrays and even PHP , I need help understanding how to move forward with this and not be scared of arrays .The resuklt of this working correctly should take 6 text values and store them into the table in the DB . I think my main issues are with this line foreach($_POST['title'] as $idx => $title) to get things working but I may be wrong ..Thanks Again . I have looked at some of example but still cannot get my code to work or understand completely .

Thanks

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 CODE

<?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] . "'     ");


}


}


?>

Upvotes: 0

Views: 2519

Answers (2)

Davide Visentin
Davide Visentin

Reputation: 741

With each input of type "text" you can submit only one value, so it doesn't make senso to try to store it in an array. You can submit an array if you name multiple text inputs with the same array name; for example:

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

But this doesn't seem to fit well in your situation.

A correct code can be:

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'] . "'     ");
}
?>

Arrays of values are submitted only by select with attribute multiple="multiple" or by checkboxes with a template like the following:

<input type="checkbox" name="checkboxes_results[]" value="value1" />
<input type="checkbox" name="checkboxes_results[]" value="value2" />

Upvotes: 0

Noman
Noman

Reputation: 1487

Here you go hope it will help you:

<?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. "'");
        }

    }
?>

Upvotes: 2

Related Questions