|
|
|
ไม่สามารถ insert ข้อมูลได้ หากมีมากกว่า 1 แถวแต่หากแถวเดียวสามารถ insert ได้ |
|
|
|
|
|
|
|
ไม่สามารถ insert ข้อมูลได้หาก search ออกมามีมากกว่า 1 ราย การค่ะ
/// ค้นหาและวนแสดงข้อมูลที่ ค้นหามา
Code (PHP)
<?php
$con = mysqli_connect("localhost", "root", "pchp@ssw0rd", "eqm") or die("Error: " . mysqli_error($con));
mysqli_query($con, "SET NAMES 'utf8' ");
date_default_timezone_set('Asia/Bangkok');
if (isset($_POST["datasr"]) && isset($_POST["typ"])) {
$datasr = $_POST["datasr"];
$typ = $_POST["typ"];
switch ($typ) {
case "1":
$query = "SELECT * FROM eqm WHERE PRDNO='$datasr'";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$no = $row['PRDNO'];
$name = $row['PRDNAME'];
$price = $row['PRDPRICE'];
$priceR = $row['GOVPRICE'];
$code = $row['GOVCODE'];
echo "
<tr>
<form method='post' id='add_details'>
<td style='width:15%'><input type='text' name='no' class='form-control' value='$no'/></td>
<td><input type='text' name='name' class='form-control' value='$name'/></td>
<td style='width:15%'><input type='text' name='price' class='form-control' value='$price'/></td>
<td style='width:15%'><input type='text' name='priceR' class='form-control' value='$priceR'/>
<input type='hidden' name='code' class='form-control' value='$code'/>
</td>
<td style='width:10%'><input type='text' name='numb' id='numb' class='form-control input' placeholder='จำนวน'></td>
<td style='width:5%'>
<button class='btn btn3' type='submit' id='add'><i class='typcn typcn-arrow-back btn-icon-append' style='font-size:28px;color:#0dd900;'></i></button>
</td>
</form>
";
}
break;
case "2":
$query = "SELECT * FROM eqm WHERE PRDNAME LIKE '%$datasr%'";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
$no = $row['PRDNO'];
$name = $row['PRDNAME'];
$price = $row['PRDPRICE'];
$priceR = $row['GOVPRICE'];
$code = $row['GOVCODE'];
echo "
<tr>
<form method='post' id='add_details'>
<td style='width:15%'><input type='text' name='no' class='form-control' value='$no'/></td>
<td><input type='text' name='name' class='form-control' value='$name'/></td>
<td style='width:15%'><input type='text' name='price' class='form-control' value='$price'/></td>
<td style='width:15%'><input type='text' name='priceR' class='form-control' value='$priceR'/>
<input type='hidden' name='code' class='form-control' value='$code'/>
</td>
<td style='width:10%'><input type='text' name='num' id='num' class='form-control input' placeholder='จำนวน' value=''></td>
<td style='width:5%'>
<button class='btn btn3' type='submit' id='add'><i class='typcn typcn-arrow-back btn-icon-append' style='font-size:28px;color:#0dd900;'></i></button>
</td>
</form>
";
}
break;
}
}
?>
///สคริปดึงข้อมูลมาโชว์ ในอีก table
Code (JavaScript)
<script>
$(document).ready(function() {
$('#add_details').on('submit', function(event) {
event.preventDefault();
$.ajax({
url: "insert.php",
method: "POST",
data: $(this).serialize(),
dataType: "json",
beforeSend: function() {
$('#add').attr('disabled', false);
},
success: function(data) {
$('#add').attr('disabled', false);
if (data.no) {
var html = '<tr>';
html += '<td><a href="del.php?no=' + data.no +'" class="btn btn3" type="submit" id="del"><i class="typcn typcn-times btn-icon-append" style="font-size:28px;color:red;padding-left:20px;"></i></td>';
html += '<td>' + data.no + '</td>';
html += '<td>' + data.name + '</td>';
html += '<td>' + data.code + '</td>';
html += '<td>' + data.numb + '</td>';
html += '<td>' + data.price + '</td>';
html += '<td>' + data.priceR + '</td>';
html += '<td>' + data.drip + '</td>';
html += '<td>' + data.total + '</td></tr>';
$('#table_data').prepend(html);
$('#add_details')[0].reset();
location.reload();
}
}
})
});
});
</script>
// insert เข้า ฐานข้อมูล
Code (PHP)
$no=$_POST["no"];
$name=$_POST["name"];
$code=$_POST["code"];
$numb=$_POST["numb"];
$price=$_POST["price"];
$priceR=$_POST["priceR"];
$drip=($price-$priceR)*$num;
$total=$price*$num;
$query = "SELECT * FROM tbl_sample WHERE no='$no'";
$result = mysqli_query($con, $query);
$num = mysqli_num_rows($result);
if($num===0) {
$query = "INSERT INTO tbl_sample (no,name,code,num,price,priceR,drip,total) VALUES ('$no','$name','$code','$numb','$price','$priceR','$drip','$total')";
$result = mysqli_query($con, $query);
$output = array(
'no' => $no,
'name' => $name,
'code' => $code,
'numb' => $numb,
'price' => $price,
'priceR' => $priceR,
'drip' => $drip,
'total' => $total
);
echo json_encode($output);
}
ขอบคุณค่ะ
Tag : PHP, HTML, JavaScript, Ajax, jQuery
|
|
|
|
|
|
Date :
2022-03-31 15:42:27 |
By :
mookmixxwipwap |
View :
488 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2022-03-31 16:05:02 |
By :
mookmixxwipwap |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1. ฟอร์มรับ input เอา id ออก เพราะหากมีหลายรายการ id จะซ้ำ ซึงผิดไวยากรณ์ HTML ไม่งั้นก็ assign dynamic id
2. ในฟอร์มกำหนด name เป็นแบบอาร์เรย์ เช่น name="no[]"
3. ฝั่ง server ถ่ายค่าตัวแปรแบบอร์เรย์ด้วยการ loop
4. ใส่รูปให้ sql statement บรรทัดนี้
Code (PHP)
$query = "INSERT INTO tbl_sample (no,name,code,num,price,priceR,drip,total) VALUES ('$no','$name','$code','$numb','$price','$priceR','$drip','$total')";
|
|
|
|
|
Date :
2022-03-31 16:15:28 |
By :
009 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทำได้แล้วนะค่ะ ขอบคุณทุกคนที่เข้ามาตอบค่ะ
|
|
|
|
|
Date :
2022-04-01 10:46:48 |
By :
mookmixxwipwap |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|