Nivek Defend Nivek Defend - 1 month ago 4x
HTML Question

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 .



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



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





Here you go hope it will help you:

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

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

            echo $value.'-'.

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