0

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


}


}


?>
8
  • Try inserting (before the foreach loop) die(print_r($_POST['title'])); to see if $_POST['title'] is actually an associative array Commented Aug 30, 2016 at 13:59
  • Have you tried testing your code before testing the form post? Commented Aug 30, 2016 at 14:00
  • 1
    Also please please do not put your dbconnection include and $con in the foreach. Each iteration will re-connect to the DB. Place these 2 lines outside the loop Commented Aug 30, 2016 at 14:00
  • Array ( [0] => title ) 1 is the response Commented Aug 30, 2016 at 14:01
  • You are wide open to SQL Injections and should really use Prepared Statements. Never trust user inputs Commented Aug 30, 2016 at 14:03

2 Answers 2

2

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

    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

OP's $_POST['title'] array seems to not be associative, so $key is just a simple numeric index (not sure OP intends this)
0

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" />

1 Comment

I don't think your first line is true: see johnrockefeller.net/html-input-forms-sending-in-an-array-in-php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.