DB Concept
DB Connection
Procedural Connection
<?php
$con = mysqli_connect('localhost', 'root', '', 'clothes');
if($con){
//echo 'Connect';
}
else{echo 'Not Connect';}
?>
PDO
Insert
<?php
$msg = "";
// check if the user has clicked the button "UPLOAD"
if (isset($_POST['uploadfile'])) {
$t1 = $_POST['t1'];
$t2 = $_POST['t2'];
$t3 = $_POST['t3'];
$t4 = $_POST['t4'];
$t5 = $_POST['t5'];
$t6 = $_POST['t6'];
$t7 = $_POST['t7'];
$filename = $_FILES["choosefile"]["name"];
$tempname = $_FILES["choosefile"]["tmp_name"];
$folder = "Image/".$filename;
// connect with the database
// query to insert the submitted data
$sql = "INSERT INTO product (ID, Name, Cat, Detail, Pricein, Priceout, Amount, Picture) VALUES ('$t1', '$t2', '$t3', '$t4', '$t5', '$t6', '$t7', '$filename')";
// function to execute above query
mysqli_query($con, $sql);
// Add the image to the "image" folder"
if (move_uploaded_file($tempname, $folder)) {
//$msg = "Image uploaded successfully";
//echo "<img src=".$folder." height=200 width=300 />";
}else{
$msg = "Failed to upload image";
}
}
?>
Select
<table>
<tr>
<th width="8%">ລະຫັດ</th>
<th width="20%">ຊື່</th>
<th width="8%">ປະເພດ</th>
<th width="20%">ລາຍລະອຽດ</th>
<th width="8%">ລາຄາຊື້</th>
<th width="8%">ລາຄາຂາຍ</th>
<th width="8%">ຈໍານວນ</th>
<th width="8%">ຮູບພາບ</th>
<th colspan="2">ແກ້ໄຂ</th>
</tr>
<?php
$query = " select * from product";
$result = mysqli_query($con, $query);
while ($data = mysqli_fetch_assoc($result)) {
?>
<tr>
<td style="text-align:center;"><?php echo $data['ID'];?></td>
<td style="text-align:center;"><?php echo $data['Name'];?></td>
<td style="text-align:center;"><?php echo $data['Cat'];?></td>
<td style="text-align:center;"><?php echo $data['Detail'];?></td>
<td style="text-align:center;"><?php echo $data['Pricein'];?></td>
<td style="text-align:center;"><?php echo $data['Priceout'];?></td>
<td style="text-align:center;"><?php echo $data['Amount'];?></td>
<td style="text-align:center;"><?php echo $data['Picture'];?></td>
<td width="5%" style="text-align:center;"><a href="Update/UpProduct.php?Edit=<?php echo $data['ID']; ?>">
ແກ້ໄຂ</a></td>
<td style="font-weight:300; text-align:center;" width="5%"><a href="Delete/DelProduct.php?i=<?php echo $data['ID']; ?>">
ລົບ</a></td>
</tr>
<?php
}
//<img src="Image/<?php echo $data['Picture'];
?>
</table>
Update
<?php
$msg = "";
// check if the user has clicked the button "UPLOAD"
if (isset($_POST['update'])) {
$t1 = $_POST['t1'];
$t2 = $_POST['t2'];
$t3 = $_POST['t3'];
$t4 = $_POST['t4'];
$t5 = $_POST['t5'];
$t6 = $_POST['t6'];
$t7 = $_POST['t7'];
$t8 = $_POST['t8'];
$filename = $_FILES["choosefile"]["name"];
$tempname = $_FILES["choosefile"]["tmp_name"];
$folder = "image/".$filename;
if($tempname != "")
{
move_uploaded_file($tempname, $folder);
$update="update product set Name='$t2', Cat='$t3', Detail='$t4', Pricein= '$t5', Priceout= '$t6', Amount= '$t7', Picture= '$filename'
where ID='$t1'";
}else
{
$update="update product set Name='$t2', Cat='$t3', Detail='$t4', Pricein= '$t5', Priceout= '$t6', Amount= '$t7', Picture= '$t8'
where ID='$t1'";
}
$query1=mysqli_query($con, $update);
if($query1){
header("refresh:0; ../Product.php");
}
else{echo'Not Update';}
//echo $pp1, $pp2;
}
?>
Delete
<?php
$msg = "";
// check if the user has clicked the button "UPLOAD"
if (isset($_POST['update'])) {
$t1 = $_POST['t1'];
$t2 = $_POST['t2'];
$t3 = $_POST['t3'];
$t4 = $_POST['t4'];
$t5 = $_POST['t5'];
$t6 = $_POST['t6'];
$t7 = $_POST['t7'];
$t8 = $_POST['t8'];
$filename = $_FILES["choosefile"]["name"];
$tempname = $_FILES["choosefile"]["tmp_name"];
$folder = "image/".$filename;
if($tempname != "")
{
move_uploaded_file($tempname, $folder);
$update="update product set Name='$t2', Cat='$t3', Detail='$t4', Pricein= '$t5', Priceout= '$t6', Amount= '$t7', Picture= '$filename'
where ID='$t1'";
}else
{
$update="update product set Name='$t2', Cat='$t3', Detail='$t4', Pricein= '$t5', Priceout= '$t6', Amount= '$t7', Picture= '$t8'
where ID='$t1'";
}
$query1=mysqli_query($con, $update);
if($query1){
header("refresh:0; ../Product.php");
}
else{echo'Not Update';}
//echo $pp1, $pp2;
}
?>