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());