mysqli php แบบ object

INSERT

 

<?php

if(isset($_POST['submit']))
   process();
   else
   showform();

 

   function showform()
   {

print <<<_HTML_
<center><h1> INSERT  </h1></center>

<form name = "insertForm" method="post" action="$_SERVER[PHP_SELF]">
ID :  <input name="txtid" type="text" />
Name :  <input name="txtname" type="text" /><br/>
<input type="submit" name="submit" value="Insert" />
</form>
_HTML_;

   }


function process()
{


$host="localhost";
$dbname="studentdb";
$user="root";
$pass="root";


$db = new mysqli($host, $user, $pass, $dbname);


/* check connection */
if (mysqli_connect_errno()) {
    die("Unable to connect!");
}

$id=empty($_POST['txtid'])? die ("ERROR : Enter a ID"): $db->real_escape_string($_POST['txtid']);
$name=empty($_POST['txtname'])? die ("ERROR : Enter a Name"): $db->real_escape_string($_POST['txtname']);

$query = "INSERT INTO student VALUES('$id','$name')";
$result = $db->query($query)or die("Error in Query".$db->error);

echo "Insert เรียบร้อย";

$db->close();

 


}
?>

 

 

UPDATE

 

<?php

if($_POST['submit']=='Update')
   process();
   else
   showform();


   function showform()
   {

print <<<_HTML_
<center><h1>UPDATE </h1></center>

<form name = "UpdateForm" method="post" action="$_SERVER[PHP_SELF]">
Update ID :  <input name="txtid" type="text" />
Change Name :  <input name="txtname" type="text" /><br/>
<input type="submit" name="submit" value="Update" />
</form>
_HTML_;

   }


function process()
{


 
$host="localhost";
$dbname="studentdb";
$user="root";
$pass="root";


$db = new mysqli($host, $user, $pass, $dbname);


/* check connection */
if (mysqli_connect_errno()) {
    die("Unable to connect!");
}

$id=empty($_POST['txtid'])? die ("ERROR : Enter a ID"): $db->real_escape_string($_POST['txtid']);
$name=empty($_POST['txtname'])? die ("ERROR : Enter a Change Name"): $db->real_escape_string($_POST['txtname']);

$query = "UPDATE student SET name = '$name' where id ='$id'";
$result = $db->query($query)or die("Error in Query".$db->error);

echo "Update เรียบร้อย";

$db->close();

 

}
?>

 

DELETE

<?php

if(isset($_POST['submit']))
   process();
   else
   showform();


   function showform()
   {

print <<<_HTML_
<center><h1> DELETE </h1></center>

<form name = "DeleteForm" method="post" action="$_SERVER[PHP_SELF]">
Delete ID :  <input name="txtid" type="text" />

<input type="submit" name="submit" value="Delete" />
</form>
_HTML_;

   }


function process()
{

 

 

$host="localhost";
$dbname="studentdb";
$user="root";
$pass="root";


$db = new mysqli($host, $user, $pass, $dbname);


/* check connection */
if (mysqli_connect_errno()) {
    die("Unable to connect!");
}

$id=empty($_POST['txtid'])? die ("ERROR : Enter a ID"): $db->real_escape_string($_POST['txtid']);

$query = "Delete from student  where id ='$id'";
$result = $db->query($query)or die("Error in Query".$db->error);

echo "Delete เรียบร้อย";

$db->close();


}
?>

 

 

SELECT

 

<?php
   showform();


  
  function showform()
{


$host="localhost";
$dbname="studentdb";
$user="root";
$pass="root";


$db = new mysqli($host, $user, $pass, $dbname);


/* check connection */
if (mysqli_connect_errno()) {
    die("Unable to connect!");
}

$query = "select * from student";
if($result = $db->query($query))
{

 

 if($result->num_rows>0)
 {
  echo "<table><tr><th>ID</th><th>NAME</th></tr>";
  $i = 0;
  while ($row = $result->fetch_array())
   {
    
     if($i%2 ==0)
      echo "<tr bgcolor='#99CCFF'><td>".$row['id']."</td><td>".$row['name']."</td></tr>";
     else
       echo "<tr><td>".$row['id']."</td><td>".$row['name']."</td></tr>";
     $i++;
   }

  
  echo "</table>";
 }

 $result->close();
}
else
 echo "Error in query : $query. ".$db->error;

 

$db->close();

 

}
?>

 

SEARCH

 

<?php

 if(isset($_POST['submit']))
   process();
   else
   showform();


   function showform()
   {

print <<<_HTML_
<center><h1> SEARCH</h1></center>

<form name = "SearchForm" method="post" action="$_SERVER[PHP_SELF]">
ID :  <input name="txtid" type="text" />

<input type="submit" name="submit" value="Search" />
</form>
_HTML_;

   }


function process()

{

$host="localhost";
$dbname="studentdb";
$user="root";
$pass="root";


$db = new mysqli($host, $user, $pass, $dbname);


/* check connection */
if (mysqli_connect_errno()) {
    die("Unable to connect!");
}

$id=empty($_POST['txtid'])? die ("ERROR : Enter a ID"): $db->real_escape_string($_POST['txtid']);

$query = "select * from student where id like '%$id%'";
if($result = $db->query($query))
{

 

 if($result->num_rows>0)
 {
  echo "<table><tr><th>ID</th><th>NAME</th></tr>";
  $i = 0;
  while ($row = $result->fetch_array())
   {
    
     if($i%2 ==0)
      echo "<tr bgcolor='#99CCFF'><td>".$row['id']."</td><td>".$row['name']."</td></tr>";
     else
       echo "<tr><td>".$row['id']."</td><td>".$row['name']."</td></tr>";
     $i++;
   }

  
  echo "</table>";
 }
 else
  echo "Not Found Data";

 $result->close();
}
else
 echo "Error in query : $query. ".$db->error;

 

$db->close();

 

}
?>