|
|
|
ถ้าจะใช้ array ในการบันทึกข้อมูลลงฐานข้อมูล ตอนนี้มันบันทึกได้แค่ตัวเดียวครับ รบกวนช่่วยดูโค้ดทีครัับ |
|
|
|
|
|
|
|
ถ้าจะใช้ array ในการบันทึกข้อมูลลงฐานข้อมูล
ตอนนี้มันบันทึกได้แค่ตัวเดียวครับ รบกวนช่่วยดูโค้ดทีครัับ
<?php
session_start();
$db = mysqli_connect("localhost", "root", "", "food_new");
$db->query("set names utf8");
if (isset($_POST['items_btn'])) {
$id_mat = $_POST['id_mat'];
$food_id= $_POST['food_id'];
$amoutt = $_POST['amoutt'];
echo $id_mat;
echo $food_id; echo $amoutt;
// $check = "select * from food_material where food_id = '$food_id' AND id_mat = '$id_mat'";
// $result1 = mysqli_query($db,$check) or die(mysqli_error());
// $num=mysqli_num_rows($result1);
// if($num > 0)
// {
// echo "<script>";
// echo "alert(' มีข้อความนี้อยู่ในระบบ !');";
// echo "window.location='food_mat1.php';";
// echo "</script>";
// exit();
// }else{
$sql = "INSERT INTO food_material`(id_mat`, food_id, amout_1) VALUES ('$id_mat' , '$food_id' ,'$amoutt')";
mysqli_query($db, $sql);
//}
}
?>
<SCRIPT language="javascript" type="db059f872f74adb416db7bd0-text/javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
if(rowCount <= 1) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
</SCRIPT>
<!DOCTYPE html>
<html>
<head>
<title>Material</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/style_form.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body id="LoginForm">
<div class="container">
<div class="login-form">
<div class="main-div">
<div class="panel">
<div class="set_char_utf8">
<h1>วัตถุดิบ</h1>
</div>
<form method="post" action="food_mat1.php" >
<div class="row">
<div class="col-sm-4">
<select style="width: 300px; height: 50px; border-radius: 5px;" name="food_id" id="food_id">
<option value="" required/><-- รายการอาหาร --></option>
<?php
$sqlc= "SELECT * FROM food_items ";
$result=mysqli_query($db,$sqlc);
while($objResuutb = mysqli_fetch_array($result))
{
?>
<option value="<?php echo $objResuutb["food_id"];?>" ><?php echo $objResuutb["food_name"];?></option>
<?php
}
?>
</select></div></div><br>
<INPUT type="button" value="Add" onclick="if (!window.__cfRLUnblockHandlers) return false; addRow('dataTable')" data-cf-modified-db059f872f74adb416db7bd0-="" />
<INPUT type="button" value="Delete" onclick="if (!window.__cfRLUnblockHandlers) return false; deleteRow('dataTable')" data-cf-modified-db059f872f74adb416db7bd0-="" />
<TABLE id="dataTable" width="30px" border="0">
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD>
<select style="width: 160px; height: 30px; border-radius: 5px;" name="id_mat" id="id_mat">
<option value="" required/><-- วัตถุดิบ --></option>
<?php
$sqlc= "SELECT * FROM materiall ";
$result=mysqli_query($db,$sqlc);
while($objResuutb = mysqli_fetch_array($result))
{
?>
<option value="<?php echo $objResuutb["id_mat"];?>" ><?php echo $objResuutb["name_mat"];?></option>
<?php
}
?>
</select>
</TD><td>
<input style="width: 80px; height: 30px; border-radius: 5px" type="number" class="form-control" id="amoutt" name="amoutt" placeholder="จำนวนวัตถุดิบ" required/></td>
</TR>
</TABLE>
<br>
<button type="submit" class="btn btn-primary" name="items_btn" id="items_btn">ตกลง</button>
</form>
</div>
</div>
</div>
</div>
<script src="https://ajax.cloudflare.com/cdn-cgi/scripts/95c75768/cloudflare-static/rocket-loader.min.js" data-cf-settings="db059f872f74adb416db7bd0-|49" defer=""></script>
</body>
</html>
Tag : PHP, MySQL, JavaScript
|
|
|
|
|
|
Date :
2019-08-28 13:32:57 |
By :
2360950024153620 |
View :
1579 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
กำหนด name ของ input ให้เป็น array ด้วยครับ เช่น
Code (PHP)
<input type="text" name="txt1[]">
<input type="text" name="txt2[]">
<input type="text" name="txt3[]">
เวลารับค่าก็วนลูปเอา เช่น
Code (PHP)
$i = 0;
foreach($_POST['txt1'] as $val){ //เอา val ไปใช้ ที่เหลือ txt2 , txt3 กำหนด ตาม index array
$txt1 = $val;
$txt2 = $_POST['txt2'][$i];
$txt3 = $_POST['txt3'][$i];
//code
$i++;
}
|
|
|
|
|
Date :
2019-08-28 15:29:50 |
By :
Manussawin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|