Hello,

How to fix this error?

Array
(
    [application] => Array
        (
            [nama] => John
            [sex] => Pria
            [food] => Array
                (
                    [0] => ayam
                    [1] => bebek
                    [2] => nasi
                )

        )

)


Notice: Undefined offset: 3 in C:\xampp\htdocs\latihan2\print4.php on line 44

latihan4.php

<html> <br><br> <?php

echo "Current php version : ".phpversion();

$servername = "localhost";
$username = "root";
$password = "";

try {
    $conn = new PDO("mysql:host=$servername;dbname=twotables", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo '<br>'."Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: ". $e->getMessage();
    }
?> <br> <form action="print4.php" method="post">
Nama: <input type="text" name="nama"><br><br>
JIK:<br><input type="radio" name="sex" value="Pria">Pria<br> <input type="radio" name="sex" value="Wanita">Wanita<br><br>
Favorite:<br> <input type="checkbox" name="food" value="ayam">Ayam<br> <input type="checkbox" name="food" value="bebek">Bebek<br> <input type="checkbox" name="food" value="nasi">Nasi<br> <input type="checkbox" name="food" value="soto">Soto<br> <input type="checkbox" name="food" value="mie">Mie<br> <input type="checkbox" name="food" value="bakwan">Bakwan<br><br> <input type="submit" value="Submit"> </form> </html>

print4.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "twotables";

mysql_connect($servername, $username, $password) or die("Cannot connect");
mysql_select_db($db) or die("Cannot select db");
?> <?php 

session_start();


foreach($_POST as $key => $value){
    $_SESSION['application'] = $_POST;
}


?> <?php
echo "<pre>";
print_r($_SESSION); 
echo "</pre>";


$nama = $_SESSION['application']['nama'];
$sex = $_SESSION['application']['sex'];

mysql_query("INSERT INTO tbl_user (name, jk) VALUES ('".$nama."','".$sex."')") or die(mysql_error());
$user_id = mysql_insert_id();


$i = 0;
while($_SESSION['application']['food'])
{

//$food = $_SESSION['application']['food'][$i];
mysql_query("INSERT INTO tbl_user_detail (food, user_id) VALUES ('".$_SESSION['application']['food'][$i]."', '".$user_id."')") or die(mysql_error());

$i++;
}

echo "Total : ".$i;

// if(!empty($_SESSION['application']['ayam'])){
// $ayam = $_SESSION['application']['ayam'];    
// }else{ $ayam = "";}

// if(!empty($_SESSION['application']['bebek'])){
// $bebek = $_SESSION['application']['bebek'];  
// }else{ $bebek = "";}

// if(!empty($_SESSION['application']['nasi'])){
// $nasi = $_SESSION['application']['nasi'];    
// }else{ $nasi = "";}

// if(!empty($_SESSION['application']['soto'])){
// $soto = $_SESSION['application']['soto'];    
// }else{ $soto = "";}

// if(!empty($_SESSION['application']['mie'])){
// $mie = $_SESSION['application']['mie'];  
// }else{ $mie = "";}

// if(!empty($_SESSION['application']['bakwan'])){
// $bakwan = $_SESSION['application']['bakwan'];    
// }else{ $bakwan = "";}



echo "Nama :".$nama.'<br>';
echo "Sex :".$sex.'<br>';
echo "Food :".$food.'<br>';







// mysql_query("INSERT INTO tbl_user_detail (food, user_id) VALUES ('".$bebek."', '".$user_id."')");
// mysql_query("INSERT INTO tbl_user_detail (food, user_id) VALUES ('".$nasi."', '".$user_id."')");
// mysql_query("INSERT INTO tbl_user_detail (food, user_id) VALUES ('".$soto."', '".$user_id."')");
// mysql_query("INSERT INTO tbl_user_detail (food, user_id) VALUES ('".$mie."', '".$user_id."')");
// mysql_query("INSERT INTO tbl_user_detail (food, user_id) VALUES ('".$bakwan."', '".$user_id."')");


?>

Line 44: mysql_query("INSERT INTO tbl_user_detail (food, user_id) VALUES ('".$_SESSION['application']['food'][$i]."', '".$user_id."')") or die(mysql_error());

Hi, instead of:

while($_SESSION['application']['food'])

which acts as an endless loop, because it's equal to while(TRUE), write:

foreach($_SESSION['application']['food'] as $food)
{
    mysql_query("INSERT INTO tbl_user_detail (food, user_id) VALUES ('".$food."', '".$user_id."')") or die(mysql_error());
}

Or better:

$sql = 'INSERT INTO tbl_user_detail (food, user_id) VALUES';

foreach($_SESSION['application']['food'] as $food)
{
    $values[] = "($food, $user_id)";
}

$sql .= implode(',', $values);

mysql_query($sql) or die(mysql_error());

This will execute a single query with multiple rows:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

As always: I suggest you to really stop using MySQL API, switch to MySQLi or PDO.

Your array structure is having problem where the error Notice: Undefined offset: 3 in C:\xampp\htdocs\latihan2\print4.php on line 44 is actually informing that you try to assign a value into Array[application] and probably you not defined the veriable 'application'.

I assume your array creation is in text pointer. Then the array should be

Array
(
    'application' => Array
        (
            'nama' => "John",
            'sex' => "Pria",
            'food' => Array
                (
                    0 => "ayam",
                    1 => "bebek",
                    2 => "nasi"
                )
        )
)

Don't missed out the comma between variables and the double/single quotes for the string value inserted.

Or you can manually assign each

Array['application']['nama'] = "John";
Array['application']['sex'] = "Pria";
Array['application']['food'][0] = "ayam";

etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.