PHP+MySqli EP.38 Ajax multipart insert
PHP+MySqli EP.38 Code Ajax multipart insert
ระบบ ตระกร้าสินค้า แบบไม่สมัครสมาชิกสั่งซื้อได้
ระบบ ตระกร้าสินค้าแบบสมัครสมาชิก
ระบบ ตระกร้าสินค้า ADVANCE แจ้งเตือนชำระเงินผ่านไลน์
ระบบ เช่าชุดออนไลน์
ระบบ บริหารจัดการห้องพัก
Code หน้า aj_f.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <link href=" https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <title>Hello, world!</title> </head> <body> <h1></h1> <script language="javascript"> function CreateNewRow() { var intLine = parseInt(document.frmMain.hdnMaxLine.value); intLine++; var theTable = document.all.tbExp var newRow = theTable.insertRow(theTable.rows.length) newRow.id = newRow.uniqueID var item1 = 1 var newCell //*** Column 1 ***// newCell = newRow.insertCell(0) newCell.id = newCell.uniqueID newCell.setAttribute("className", "css-name"); // newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"Column1_"+intLine+"\" VALUE=\"\"></center>" newCell.innerHTML = "<center><INPUT TYPE=\"TEXT\" CLASS=\"form-control\" SIZE=\"5\" NAME=\"Column1["+intLine+"]\" VALUE=\"\"></center>" document.frmMain.hdnMaxLine.value = intLine; } function RemoveRow() { intLine = parseInt(document.frmMain.hdnMaxLine.value); if(parseInt(intLine) > 0) { theTable = (document.all) ? document.all.tbExp : document.getElementById("tbExp") theTableBody = theTable.tBodies[0]; theTableBody.deleteRow(intLine); intLine--; document.frmMain.hdnMaxLine.value = intLine; } } </script> <body> <div class="container"> <div class="row"> <div class="col-md-3"> </div> <div class="col-md-6"> <br> <br> <center><img src="logo_fordev22_2.png" width="60%" alt=""></center> <br> <form name="frmMain" method="post" action="aj_s.php"> <table class="table table-striped" width="445" border="1" id="tbExp"> <tr> <td><div align="center"><h3>Ajax multipart insert By fordev22</h3></div></td> </tr> </table> <center> <input type="hidden" name="hdnMaxLine" value="0"> <div class="btn-group" role="group" aria-label="Basic mixed styles example"> <input name="btnAdd" class="btn btn-primary btn-md" type="button" id="btnAdd" value="Add" onClick="CreateNewRow();"> <input name="btnDel" type="button" class="btn btn-danger btn-md" id="btnDel" value="Del" onClick="RemoveRow();"> <button type="submit" class="btn btn-secondary btn-md">Save</button> </div> </center> </form> </div> <div class="col-md-3"> </div> </div> </div> <!-- Optional JavaScript; choose one of the two! --> <!-- Option 1: Bootstrap Bundle with Popper --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script> <!-- Option 2: Separate Popper and Bootstrap JS --> <!-- <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script> --> </body> </html> |
Code หน้า aj_s.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php echo "<pre>"; print_r($_POST); echo "</pre>"; // foreach($_POST['Column1'] as $key => $value){ // // print_r($key); // echo $_POST['Column1'][$key].'<br>'; // } exit(); include('condb.php'); foreach($_POST['Column1'] as $key => $value){ $sql4 = sprintf("INSERT INTO t_aj (column1,column2,column3,column4,column5) VALUES ('%s','%s','%s','%s','%s')", $_POST['Column1'][$key], $_POST['Column2'][$key], $_POST['Column3'][$key], $_POST['Column4'][$key], $_POST['Column5'][$key] ); $query4 = mysqli_query($con, $sql4) or die ("Error in query: $sql4 " . mysqli_error()); } ?> |
con_db.php
1 2 3 4 5 6 |
<?php $con= mysqli_connect("localhost","root","","ชื่อDatabase") or die("Error: " . mysqli_error($con)); mysqli_query($con, "SET NAMES 'utf8' "); error_reporting( error_reporting() & ~E_NOTICE ); date_default_timezone_set('Asia/Bangkok'); ?> |
sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
-- phpMyAdmin SQL Dump -- version -- -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 -- Generation Time: Jun 30, 2022 at 11:10 AM -- Server version: 10.4.14-MariaDB -- PHP Version: 7.4.11 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `food_db` -- -- -------------------------------------------------------- -- -- Table structure for table `t_aj` -- CREATE TABLE `t_aj` ( `pk` int(11) NOT NULL, `Column1` text DEFAULT NULL, `Column2` text DEFAULT NULL, `Column3` text DEFAULT NULL, `Column4` text DEFAULT NULL, `Column5` text DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Dumping data for table `t_aj` -- INSERT INTO `t_aj` (`pk`, `Column1`, `Column2`, `Column3`, `Column4`, `Column5`) VALUES (3, 'fordev22', '', '', '', ''), (4, 'fordev22', '', '', '', ''), (5, 'fordev22', '', '', '', ''); -- -- Indexes for dumped tables -- -- -- Indexes for table `t_aj` -- ALTER TABLE `t_aj` ADD PRIMARY KEY (`pk`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `t_aj` -- ALTER TABLE `t_aj` MODIFY `pk` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |