 |
|
save array(for)
Code (PHP)
<?php
session_start();
include '../include/condb.php';
for($i=0;$i<count($_POST["txtMedName"]);$i++){
$sql = "INSERT INTO Medical (Name, UOM, Price, MedicalTypeID, SupplierID, CreateDateTime, CreateByUser)"
. "VALUES ('".$_POST["txtMedName"][$i]."',"
." '".$_POST["txtUOM"][$i]."',"
." '".$_POST["txtPrice"][$i]."',"
." '".$_POST["txtMedType"][$i]."',"
." '".$_POST["txtSupplier"][$i]."',"
." NOW(),"
." '$_SESSION[Username]')";
if ($conn->query($sql) === TRUE) {
header( "location: ../stock.php" );
exit(0);
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
$conn->close();
?>
save array(foreach)
Code (PHP)
<?php
session_start();
include'include/condb.php';
foreach($_POST['txtMedName'] as $key=>$val){
$sql = "INSERT INTO Medical (Name, UOM, Price, MedicalTypeID, SupplierID, CreateDateTime, CreateByUser)"
. "VALUES ('".$_POST["txtMedName"][$key]."',"
." '".$_POST["txtUOM"][$key]."',"
." '".$_POST["txtPrice"][$key]."',"
." '".$_POST["txtMedType"][$key]."',"
." '".$_POST["txtSupplier"][$key]."',"
." NOW(),"
." '$_SESSION[Username]')";
if ($conn->query($sql) === TRUE) {
echo 'save success';
//header( "location: ../stock.php" );
exit(0);
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
ลองแล้วทั้งคู่ แค่บันทึกได้แค่ค่าแรกค่าเดียว แต่ถ้า echo ออกมาเฉพาะค่า Array สามารถ echo ออกมาได้ทั้งหมด ครบทุกตัว
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2017-04-27 10:29:31 |
By :
NP-Carbon |
View :
2975 |
Reply :
7 |
|
 |
 |
 |
 |
|
|
|
 |