วันพฤหัสบดีที่ 27 ธันวาคม พ.ศ. 2561

UPDATE TABLE FROM DATABASES WHERE ID ON PHP

ຕໍ່ຈາກ ບົດຄວາມທີ່ 4 ມາເວົ້າກ່ຽວກັບ ການຂຽນ UPDATE TABLE ຊຶ່ງຈະມີຂັ້ນຕອນທີ່ແສນຈະງ່າຍ ງ່າຍແບບໃດມາລົງມືເຮັດນຳກັນເລີຍ ນີ້ຄື

 File test_oop.php

 <?php
// connect oop mysqli
 $localhost = "localhost"; //localhost server ຈຳລອງຜ່ານ web server Xampp,Appserv
 $root = "root"; // root ຂອງການຕິດຕັ້ງ ໃນ web server
 $pass = "44@44"; // ລະຫັດຜ່ານທີ່ຕິດຕັ້ງໄວ້ ໃນ web server
 $db = "mydatabase"; // database ທີ່ຕ້ອງການຈະຕິດຕໍ່
 $con= new mysqli($localhost,$root,$pass,$db); // connection
 mysqli_query($con,"set names utf8"); // set ພາສາ ເພື່ອອ່ານພາສາລາວ ໄທ ອື່ນໆ
// check connection or die
// ເມື່ອ Error ຈະຂຶ້ນສະແດງ ຂໍ້ຄວາມຕ່າງໆ ແຕ່ຖ້າຫາກ Connection ສຳເລັດ ສະບໍ່ມີຂໍ້ຄວາມຫຍັງຂຶ້ນ
   if($con->connect_errno){
    echo $con->connect_error;
    exit;
   }
// end to connection statement
?>
<html>
<head>
<meta charset="utf-8">  
<title>Customer</title>
</head>
<body>
<center>

<br>
<form name="update" method="post" action="update.php">
  <?php
$strSQLS = " SELECT * FROM customer WHERE CustomerID = 'C002'";
$query =  $con->query($strSQLS);
$row = $query->fetch_assoc();
  ?>
  <h3>Form update</h3>
  <table border="1" width="900" cellpadding='0' cellspacing='0'>
<tr>
  <td width="450" align="center">ID</td>
  <td width="450" align="center"><input type="text" name="CustomerID" id="CustomerID" value="<?php  echo $row["CustomerID"]; ?>"><input type="hidden" name="HCustomerID" id="HCustomerID" value="<?php  echo $row["CustomerID"]; ?>"></td>
</tr>

<tr>
<td align="center">Name</td>
<td align="center"><input type="text" name="Name" id="Name" value="<?php  echo $row["Name"];?>"></td>
</tr>
<tr>
<td align="center">Email</td>
<td align="center"><input type="text" name="Email" id="Email" value="<?php  echo $row["Email"]; ?>"></td>
</tr>
<tr>
<td align="center">CountryCode</td>
<td align="center"><input type="text" name="CountryCode" id="CountryCode" value="<?php echo $row["CountryCode"]; ?>"></td>
</tr>
<tr>
<td align="center">Used</td>
<td align="center"><input type="text" name="Used" id="Used" value="<?php echo $row["Used"]; ?>"></td>
</tr>
<tr>
<td align="center">Bath</td>
<td align="center"><input type="text" name="bath" id="bath" value="<?php echo $row["bath"];?>"></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
<?php $con->close(); ?>

========================================================
Flie update.php

<?php
// connect oop mysqli
 $localhost = "localhost"; //localhost server ຈຳລອງຜ່ານ web server Xampp,Appserv
 $root = "root"; // root ຂອງການຕິດຕັ້ງ ໃນ web server
 $pass = "44@44"; // ລະຫັດຜ່ານທີ່ຕິດຕັ້ງໄວ້ ໃນ web server
 $db = "mydatabase"; // database ທີ່ຕ້ອງການຈະຕິດຕໍ່
 $con= new mysqli($localhost,$root,$pass,$db); // connection
 mysqli_query($con,"set names utf8"); // set ພາສາ ເພື່ອອ່ານພາສາລາວ ໄທ ອື່ນໆ
// check connection or die
// ເມື່ອ Error ຈະຂຶ້ນສະແດງ ຂໍ້ຄວາມຕ່າງໆ ແຕ່ຖ້າຫາກ Connection ສຳເລັດ ສະບໍ່ມີຂໍ້ຄວາມຫຍັງຂຶ້ນ
   if($con->connect_errno){
    echo $con->connect_error;
    exit;
   }
// end to connection statement

?>
<html>
<head>
<meta charset="utf-8">  
<title>Customer</title>
</head>
<body>
<?php
if(isset($_POST["update"])){

    $update = " UPDATE customer SET
    CustomerID = '".$_POST["CustomerID"]."',
    Name = '".$_POST["Name"]."',
    Email = '".$_POST["Email"]."',
    CountryCode = '".$_POST["CountryCode"]."',
    Used = '".$_POST["Used"]."',
    bath = '".$_POST["bath"]."'
    WHERE CustomerID = '".$_POST["HCustomerID"]."' ";

    $query = $con->query($update);
    if($query){
        echo "<script> alert('Update done'); </script>";
        echo "<meta http-equiv='refresh'content='0; url=test_oop.php'>";
    }
}
?>
</body>
</html>
<?php $con->close(); ?>
=====================================================
Out Put



 After Update




วันพุธที่ 26 ธันวาคม พ.ศ. 2561

SELECT TABLE FROM DATABASES WHERE ID ON PHP BY OOP

ການຂຽນ ສະແດງຂໍ້ມູນ ໂດມີເງື່ອນໄຂ ແບບງ່າຍໆ ຈາກບົດຄວາມທີ່ 3 ທີ່ໄດ້ຂຽນໄປໃນການຕິດຕໍ່ກັບ ຖານຂໍ້ມູນ ມາໃນບົດຄວາມນີ້ ສິໄດ້ສອນກ່ຽວກັບການ ສະແດງຂໍ້ມູນອອກມາ ຈາກ Table ໂດຍ ໃຊ້ຊຸດຄຳສັ່ງ Mysql SELECT * FROM ... Where ແລະ ທຳການ ສະແດງຂໍ້ມູນຜ່ານ ການ Loop While Fetch_array ຊຶ່ງຈະມີຂັ້ນຕອນທີ່ແສນຈະງ່າຍ ສາມາດເຮັດຕາມໄດ້ ດັ່ງ Code ຕໍ່ໄປນີ້

<?php
// connect oop mysqli
$localhost = "localhost"; //localhost server ຈຳລອງຜ່ານ web server Xampp,Appserv
$root = "root"; // root ຂອງການຕິດຕັ້ງ ໃນ web server
$pass = "xxx"; // ລະຫັດຜ່ານທີ່ຕິດຕັ້ງໄວ້ ໃນ web server
$db = "db_test"; // database ທີ່ຕ້ອງການຈະຕິດຕໍ່
$con= new mysqli($localhost,$root,$pass,$db); // connection
mysqli_query($con,"set names utf8"); // set ພາສາ ເພື່ອອ່ານພາສາລາວ ໄທ ອື່ນໆ


// check connection or die
// ເມື່ອ Error ຈະຂຶ້ນສະແດງ ຂໍ້ຄວາມຕ່າງໆ ແຕ່ຖ້າຫາກ Connection ສຳເລັດ ສະບໍ່ມີຂໍ້ຄວາມຫຍັງຂຶ້ນ
if($con->connect_errno){
echo $con->connect_error;
exit;
}
// end to connection statement

$CustomerID = "C005";
$strSQL = " SELECT * FROM customer  WHERE CustomerID = '$CustomerID' ";
$objQuery = $con->query($strSQL);

?>
<html>
<head>
<meta charset="utf-8">
<title>Customer</title>
</head>
<body>
<center>
<br/>
<h3> TEST SELECT TABLE  customer  With OOP </h3>
<br/>
<table border="1" width="900" cellpadding='0' cellspacing='0'>
<tr>
<th width="61">ID</th>
<th width="207">Name</th>
    <th width="100">Email</th>
    <th width="100">CountryCode</th>
<th width="129">Used</th>
<th width="103">Bath</th>
</tr>
<?php
while($value = $objQuery->fetch_array()) {
?>
<tr>
<td align='center'><?php echo  $value["CustomerID"];?></td>
<td align='center'><?php echo  $value["Name"];?></td>
<td align='center'><?php echo  $value["Email"];?></td>
<td align='center'><?php echo  $value["CountryCode"];?></td>
<td align='center'><?php echo  $value["Used"];?></td>
<td align='center'><?php echo  $value["bath"];?></td>
</tr>
<?php } ?>
<?php $con->close(); ?>
</table>
</body>
</html>






Out put

วันจันทร์ที่ 24 ธันวาคม พ.ศ. 2561

SELECT TABLE FROM DATABASES ON PHP BY OOP

ຈາກບົດຄວາມທີ່ 2 ທີ່ໄດ້ຂຽນໄປໃນການຕິດຕໍ່ກັບ ຖານຂໍ້ມູນ ມາໃນບົດຄວາມນີ້ ສິໄດ້ສອນກ່ຽວກັບການ ສະແດງຂໍ້ມູນອອກມາ ຈາກ Table ໂດຍ ໃຊ້ຊຸດຄຳສັ່ງ Mysql SELECT * FROM ... ແລະ ທຳການ ສະແດງຂໍ້ມູນຜ່ານ ການ Loop While Fetch_array ຊຶ່ງຈະມີຂັ້ນຕອນທີ່ແສນຈະງ່າຍ ສາມາດເຮັດຕາມໄດ້ ດັ່ງ Code ຕໍ່ໄປນີ້

ການຂຽນ ຕິດຕໍ່ກັບ ຖານຂໍ້ມູນ OOP Connection to database on PHP by OOP


ການຂຽນ Connection to database on PHP by OOP ແບບງ່າຍໆ ໃຜກ໊ເຮັດໄດ້



<?php
// connect oop mysqli
$localhost = "localhost"; //localhost server ຈຳລອງຜ່ານ web server Xampp,Appserv
$root = "root"; // root ຂອງການຕິດຕັ້ງ ໃນ web server
$pass = "xxx"; // ລະຫັດຜ່ານທີ່ຕິດຕັ້ງໄວ້ ໃນ web server
$db = "db_test"; // database ທີ່ຕ້ອງການຈະຕິດຕໍ່


$con= new mysqli($localhost,$root,$pass,$db); // connection

mysqli_query($con,"set names utf8"); // set ພາສາ ເພື່ອອ່ານພາສາລາວ ໄທ ອື່ນໆ


// check connection or die
// ເມື່ອ Error ຈະຂຶ້ນສະແດງ ຂໍ້ຄວາມຕ່າງໆ ແຕ່ຖ້າຫາກ Connection ສຳເລັດ ສະບໍ່ມີຂໍ້ຄວາມຫຍັງຂຶ້ນ
if($con->connect_errno){
echo $con->connect_error;
exit;
}
// end to connection statement
?>

out put on Google Chrome

-------------------------------
ກາຮຕິດຕໍ່ ສົມບູນແບບ













ເມື່ອຂຽນຜິດພາດ
ເມື່ອ Errors ໃຫ້ໄປແກ້ໄຂຂໍ້ຜິດພາດ ແລ້ວ Run ຂຶ້ນມາໃໝ່














ຖືວ່າຈົບໄປແລ້ວເນາະ ການສອນ ຂຽນ Connection to databases php on oop
ພົບກັນໃໝ່ ໃນບົດຄວາມໜ້າ 
ກົດຕິດຕາມ ແລະ ສອບຖາມສົງໃສອັນໃດໄດ້ເດິ້
ໂປຣແກຣມເມີ ທຳມະດາຄົນໜຶ່ງ ທີ່ມັກຕຶກປາ ແລະ ຮ້ອງເພລງ By Mr. HaraRock