|
|
|
รบกวนช่วยแนะนำแนวทาง insert array php mysql หน่อยครับ |
|
|
|
|
|
|
|
Form Data
<div>
<input type="text" class="form-control" name="customerName" value="<?=$_REQUEST["customerName"];?>">
<input type="text" class="form-control" name="customerTelephone" value="<?=$_REQUEST["customerTelephone"];?>">
<input type="text" class="form-control" name="customerDatetime" value="<?=$_REQUEST["customerDatetime"];?>">
<input type="text" class="form-control" name="customerText" value="<?=$_REQUEST["customerText"];?>">
<input type="text" class="form-control" name="memberID" value="<?=$_REQUEST["memberID"];?>">
<?php
for ($i=0; $i < count($_REQUEST["massageHours"]); $i++) {
?>
<div class="row">
<div class="col-sm-6">
ID
<input type="text" class="form-control" name="massageID" value="<?=$_REQUEST["massageID"][$i];?>">
</div>
<div class="col-sm-6">
Hours
<input type="text" class="form-control" name="massageHours" value="<?=$_REQUEST["massageHours"][$i];?>">
</div>
</div>
<?php
}
?>
</div>
Save Data
<?php @session_start();
include('connection.php');
$customerName = $_REQUEST["customerName"];
$customerTelephone = $_REQUEST["customerTelephone"];
$customerDatetime = $_REQUEST["customerDatetime"];
$customerText = $_REQUEST["customerText"];
$memberID = $_REQUEST["memberID"];
$massageID = $_REQUEST["massageID"];
$massageHours = $_REQUEST["massageHours"];
for ($i=0; $i < $massageID; $i++) {
$sql = "INSERT INTO tbl_invoice(customerName, customerTelephone, customerDatetime,
customerText, memberID, massageID, massageHours)
VALUES('$customerName', '$customerTelephone', '$customerDatetime',
'$customerText', '$memberID', '$massageID[$i]', '$massageHours[$i]')";
}
$result = mysqli_query($con, $sql) or die ("Error in query: $sql " . mysqli_error());
mysqli_close($con);
if($result > 0)
{
echo "<script type='text/javascript'>";
echo "alert('Save Succesfully');";
echo "window.location = 'index.php'; ";
echo "</script>";
}
else
{
echo "<script type='text/javascript'>";
echo "alert('Error back to try again');";
echo "</script>";
}
?>
cusName cusTel cusDate cusText memID massageHour พวกนี้รับค่ามาจาก Form ตามรูปครับ
ส่วน massageID ผมดึงมาจาก database ตอนนี้ผมติดปัญหา save ข้อมูลแล้วข้อมูล array ไม่เข้า database เลย
รบกวนช่วยแนะแนวทางแก้ไขให้ด้วยครับ ขอบคุณครับ
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2017-01-31 14:03:21 |
By :
ebuzzz |
View :
1169 |
Reply :
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จะเก็บข้อมูลเป็น Array หรือว่าส่งค่าเป็น Array บันทึกทีละแถวครับ?
|
|
|
|
|
Date :
2017-01-31 16:17:17 |
By :
ALTELMA |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เก็บเข้า json encode ไปเลยครับ
|
|
|
|
|
Date :
2017-01-31 16:20:24 |
By :
คนไม่ดีทำผิดเสมอ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Form Data
<div>
<input type="text" class="form-control" name="customerName" value="<?=$_REQUEST["customerName"];?>">
<input type="text" class="form-control" name="customerTelephone" value="<?=$_REQUEST["customerTelephone"];?>">
<input type="text" class="form-control" name="customerDatetime" value="<?=$_REQUEST["customerDatetime"];?>">
<input type="text" class="form-control" name="customerText" value="<?=$_REQUEST["customerText"];?>">
<input type="text" class="form-control" name="memberID" value="<?=$_REQUEST["memberID"];?>">
<?php
for ($i=0; $i < count($_REQUEST["massageHours"]); $i++) {
?>
<div class="row">
<div class="col-sm-6">
ID
<input type="text" class="form-control" name="massageID[]" value="<?=$_REQUEST["massageID"][$i];?>">
</div>
<div class="col-sm-6">
Hours
<input type="text" class="form-control" name="massageHours[]" value="<?=$_REQUEST["massageHours"][$i];?>">
</div>
</div>
<?php
}
?>
</div>
Save Data
<?php @session_start();
include('connection.php');
$customerName = $_REQUEST["customerName"];
$customerTelephone = $_REQUEST["customerTelephone"];
$customerDatetime = $_REQUEST["customerDatetime"];
$customerText = $_REQUEST["customerText"];
$memberID = $_REQUEST["memberID"];
$massageID = $_REQUEST["massageID"];
$massageHours = $_REQUEST["massageHours"];
for ($i=0; $i < count($massageID); $i++) {
$sql = "INSERT INTO tbl_invoice(customerName, customerTelephone, customerDatetime,
customerText, memberID, massageID, massageHours)
VALUES('$customerName', '$customerTelephone', '$customerDatetime',
'$customerText', '$memberID', '$massageID[$i]', '$massageHours[$i]')";
}
$result = mysqli_query($con, $sql) or die ("Error in query: $sql " . mysqli_error());
mysqli_close($con);
if($result > 0)
{
echo "<script type='text/javascript'>";
echo "alert('Save Succesfully');";
echo "window.location = 'index.php'; ";
echo "</script>";
}
else
{
echo "<script type='text/javascript'>";
echo "alert('Error back to try again');";
echo "</script>";
}
?>
|
|
|
|
|
Date :
2017-01-31 18:50:33 |
By :
tomrambo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เป็นเพราะว่าบรรทัด query ด้านล่างมันอยู่นอก for loop ครับ ต้องเอาไปไว้ด้านใน for loop ครับ
$result = mysqli_query($con, $sql) or die ("Error in query: $sql " . mysqli_error());
ประมาณนี้ครับ
<?php @session_start();
include('connection.php');
mysqli_autocommit($con, FALSE);
$customerName = $_REQUEST["customerName"];
$customerTelephone = $_REQUEST["customerTelephone"];
$customerDatetime = $_REQUEST["customerDatetime"];
$customerText = $_REQUEST["customerText"];
$memberID = $_REQUEST["memberID"];
$massageID = $_REQUEST["massageID"];
$massageHours = $_REQUEST["massageHours"];
$result = 0;
for ($i=0; $i < count($massageID); $i++) {
$sql = "INSERT INTO tbl_invoice(customerName, customerTelephone, customerDatetime,
customerText, memberID, massageID, massageHours)
VALUES('$customerName', '$customerTelephone', '$customerDatetime',
'$customerText', '$memberID', '$massageID[$i]', '$massageHours[$i]')";
mysqli_query($con, $sql) or die ("Error in query: $sql " . mysqli_error());
$result++;
}
if($result == count($massageID))
{
mysqli_commit($con);
echo "<script type='text/javascript'>";
echo "alert('Save Succesfully');";
echo "window.location = 'index.php'; ";
echo "</script>";
}
else
{
mysqli_rollback($con);
echo "<script type='text/javascript'>";
echo "alert('Error back to try again');";
echo "</script>";
}
mysqli_close($con);
?>
|
ประวัติการแก้ไข 2017-01-31 20:04:45 2017-01-31 20:06:35
|
|
|
|
Date :
2017-01-31 20:00:18 |
By :
tomrambo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|