01.CREATE TABLE IF NOT EXISTS `customer` (02.`CustomerID` varchar(4) NOT NULL,03.`Name` varchar(50) NOT NULL,04.`Email` varchar(50) NOT NULL,05.`CountryCode` varchar(2) NOT NULL,06.`Budget` double NOT NULL,07.`Used` double NOT NULL,08.PRIMARY KEY (`CustomerID`)09.) ENGINE=MyISAM DEFAULT CHARSET=utf8;10. 11.--12.-- dump ตาราง `customer`13.--14. 15.INSERT INTO `customer` (`CustomerID`, `Name`, `Email`, `CountryCode`, `Budget`, `Used`) VALUES16.('C001', 'Win Weerachai', 'win.weerachai@thaicreate.com', 'TH', 1000000, 600000),17.('C002', 'John Smith', 'john.smith@thaicreate.com', 'UK', 2000000, 800000),18.('C003', 'Jame Born', 'jame.born@thaicreate.com', 'US', 3000000, 600000),19.('C004', 'Chalee Angel', 'chalee.angel@thaicreate.com', 'US', 4000000, 100000);SQL user (PHP)
1.CREATE TABLE IF NOT EXISTS `user` (2.`UserID` varchar(4) COLLATE utf8_unicode_ci NOT NULL,3.`UserName` varchar(50) COLLATE utf8_unicode_ci NOT NULL,4.`Email` varchar(50) COLLATE utf8_unicode_ci NOT NULL5.) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;INDEX(PHP)
001.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">002.<html xmlns="http://www.w3.org/1999/xhtml">003.<head>004.<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />005.<title>Untitled Document</title>006.</head>007.<body>008.<?php009.// connect to db by mysqli010.$serverName = "localhost";011.$userName = "root";012.$userPassword = "44@44";013.$dbName = "mydatabase";014.$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);015.// end connect to db by mysqli016. 017.// SELECT TO INSERT018.$customer = " SELECT * FROM customer ";019.$Q_customer = mysqli_query($conn,$customer);020. 021.// END SELECT TO INSERT022. 023. 024.?>025.<?php026. 027.$CheckBox = $_POST['Ch_INSERT'];028.if(isset($_POST["SAVE"]))029.{030.if(empty($CheckBox) || $CheckBox == 0 ) { 031.echo "Please Select Checkbox after click Save!!!"; 032.}else{033.foreach($_POST['Ch_INSERT'] as $i)034.{035.$INSERT = "INSERT INTO user (UserID,036.UserName,037.Email)038.VALUES039.('{$_POST['CustomerID'][$i]}',040.'{$_POST['Name'][$i]}',041.'{$_POST['Email'][$i]}')";042.$Q_INSERT = mysqli_query($conn,$INSERT); 043.}044.}045.if($Q_INSERT)046.{047.echo "<script> alert('SVAE DONE !!!') </script>";048.}049. 050.}051.?>052. 053. 054.<form action="#" name="Frm1" id="Frm1" method="post" enctype="multipart/form-data">055.<table width="100%" border="1" cellspacing="0" cellpadding="0">056.<tr>057.<td colspan="7" align="center">PHP MYSQLI INSERT BY CHECKBOX</td>058.</tr>059.<tr>060.<td>CustomerID</td>061.<td>Name</td>062.<td>Email</td>063.<td>CountryCode</td>064.<td>Budget</td>065.<td>Used</td>066.<td>Checkbox</td>067.</tr>068.<?php069.$i = 0;070.while ($row = mysqli_fetch_array($Q_customer,MYSQLI_ASSOC))071.{072. 073.?>074.<tr align="center">075.<td> <input type="text" name="CustomerID[]" id="CustomerID"076.value="<?php echo $row["CustomerID"]; ?>" />077.</td>078.<td> <input type="text" name="Name[]" id="Name"079.value="<?php echo $row["Name"]; ?>" />080.</td>081.<td> <input type="text" name="Email[]" id="Email"082.value="<?php echo $row["Email"]; ?>" />083.</td>084.<td> <input type="text" name="CountryCode[]" id="CountryCode"085.value="<?php echo $row["CountryCode"]; ?>" />086.</td>087.<td> <input type="text" name="Budget[]" id="Budget"088.value="<?php echo $row["Budget"]; ?>" />089.</td>090.<td> <input type="text" name="Used[]" id="Used"091.value="<?php echo $row["Used"]; ?>" />092.</td>093.<td> <input type="checkbox" name="Ch_INSERT[]" id="Ch_INSERT" value="<?php echo $i++; ?>" /></td> 094.</tr>095.<?php } ?>096.<tr>097.<td colspan="7" align="center"><input type="submit" id="SAVE" name="SAVE[]" value="SAVE" /></td>098. 099.</tr>100.</table>101.</form>102.</body>103.</html>หลักการ
1. mysqli
2. SELECT ก่อน INSERT
3. Multiple INSERT
4. isset
5. if else
6. foreach
7. FORM
8. Input มี []
9. $i ++ ให้ Checkbox
ไม่มีความคิดเห็น:
แสดงความคิดเห็น