You are on page 1of 6

Tạo database:

<?php
$conn = mysql_connect("localhost","root", "")
or die (mysql_error());
$string = "create database my_other_db";
mysql_query($string) or
die(mysql_error());
if($string)
{
echo " ?ã t?o ???c database";
}
?>
Tạo table:
<?php
$link = mysql_connect("localhost", "root", "");
if ($link)
{
echo "kết nối được tới MySQL";
}
mysql_select_db("my_other_db", $link) or die(mysql_error());
$query = "create table my_table (id int primary key, name text)";
mysql_query($query) or die(mysql_error());
mysql_close($link);
?>

Sửa dử liệu bằng form: sua.php và doupdate.php


<html>
<head>
<SCRIPT language=JavaScript>
function checkInput()
{
if (document.frmPHP.txtID.value=="")
{
alert("Invalid ID, Please enter ID");
document.frmPHP.txtID.focus();
return false;
}
if (document.frmPHP.txtName.value=="")
{
alert("Please enter Name");
document.frmPHP.txtName.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="frmPHP" method="post"
action="doupdate.php"
onsubmit="return checkInput();">
<tr>
<td align="left" class="content-sm"><p><b> Please enter ID and
Name</b></p>
</td>
</tr>
<tr>
<td align="left" >ID:</td>
</tr>
<p>
<input type="text" name="txtID"
size="25" maxlength="3" class="textbox">
</p>
<tr>
<td align="left">&nbsp; </td>
</tr>
<tr>
<td align="left" ><p>Name:</p>
</td>
</tr>
<tr>
<td align="left" >
<input type="text" name="txtName"
size="25" maxlength="50" class="textbox">
</td>
</tr>
<tr>
<td align="left" valign="top"> <p>
<input name="submit" type="submit" class="button"
value="Submit">
<input name="reset" type="reset" class="button"
value="Reset">
</p>
</td>
</tr>
</form>
</body>
</html>

Doupdate.php
<?php
echo $txtID;
echo "<br>";
echo $txtName;
echo "<br>";
$link = mysql_connect("localhost", "root", "");
if ($link)
{
echo "kết nối được tới MySQL";
}
echo "<br>";
mysql_select_db("my_other_db", $link) or die(mysql_error());
$query = "Update my_table set name ='$txtName' where id
='$txtID'";
$result=mysql_query($query) or die(mysql_error());
$affectrow=0;
if($result)
$affectrow=mysql_affected_rows();
mysql_close($link);
?>
So mau tin cap nhat <?php echo $affectrow ?>

Thêm vào csdl gồm 2 form : nhap.php và doinsert.php

<html>
<head>
<SCRIPT language=JavaScript>
function checkInput()
{
if(document.frmPHP.txtID.value=="")
{
alert("Invalid ID, Please enter ID");
document.frmPHP.txtID.focus();
return false;
}
if (document.frmPHP.txtName.value=="")
{
alert("Please enter Name");
document.frmPHP.txtName.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="frmPHP" method="post"
action="doinsert.php"
onsubmit="return checkInput();">
<tr>
<td align="left" class="content-sm"><p><b> Please enter ID and
Name</b></p>
</td>
</tr>
<tr>
<td align="left" >ID:</td>
</tr>
<p>
<input type="text" name="txtID"
size="25" maxlength="3" class="textbox">
</p>
<tr>
<td align="left">&nbsp; </td>
</tr>
<tr>
<td align="left" ><p>Name:</p>
</td>
</tr>
<tr>
<td align="left" >
<input type="text" name="txtName"
size="25" maxlength="50" class="textbox">
</td>
</tr>
<tr>
<td align="left" valign="top"> <p>
<input name="submit" type="submit" class="button"
value="Submit">
<input name="reset" type="reset" class="button"
value="Reset">
</p>
</td>
</tr>
</form>
</body>
</html>

Doinsert.php

<?php
echo $txtID;
echo "<br>";
echo $txtName;
echo "<br>";
$link = mysql_connect("localhost", "root", "");
if ($link)
{
echo "kết nối được tới MySQL";
}
echo "<br>";
mysql_select_db("my_other_db", $link) or die(mysql_error());
$query = "insert into my_table (id,name) values
('$txtID','$txtName')";
mysql_query($query) or die(mysql_error());
mysql_close($link);
?>

Xóa một đối tượng gồm 2 form : xoabangform.php ketquaxoa.php


<html>
<head>
<SCRIPT language=JavaScript>
function checkInput()
{
if (document.frmPHP.txtID.value=="")
{
alert("Invalid ID, Please enter ID");
document.frmPHP.txtID.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="frmPHP" method="post"
action="ketquaxoa.php"
onsubmit="return checkInput();">
<tr>
<td align="left" class="content-sm"><p><b> Please enter ID and
Name</b></p>
</td>
</tr>
<tr>
<td align="left" >ID:</td>
</tr>
<p>
<input type="text" name="txtID"
size="25" maxlength="3" class="textbox">
</p>
<tr>
<td align="left">&nbsp; </td>
</tr>
<td align="left" valign="top"> <p>
<input name="submit" type="submit" class="button"
value="Submit">
<input name="reset" type="reset" class="button"
value="Reset">
</p>
</td>
</tr>
</form>
</body>
</html>

Ketquaxoa.php
<?php
echo "da xoa ".$txtID;
echo "<br>";
$link = mysql_connect("localhost", "root", "");
if ($link)
{
echo "ket noi duoc toi MySQL";
}
echo "<br>";
mysql_select_db("my_other_db", $link) or die(mysql_error());
$query = "Delete from my_table where id ='$txtID'";
$result=mysql_query($query) or die(mysql_error());
$affectrow=0;
if($result)
$affectrow=mysql_affected_rows();
mysql_close($link);
?>

Xem bảng xem.php


<?php
$link = mysql_connect("localhost", "root", "");
if ($link)
{
echo "kết nối được tới MySQL";
}
?>
<h2>View My Table!</h2>
<?php
mysql_select_db("my_other_db", $link) or die(mysql_error());
$result = mysql_query("select * from my_table") or die
(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<b>Masp:</b>";
echo $row["id"];
echo "<br>";
echo "<b>Tensp:</b>";
echo $row["name"];
echo "<br>";
echo "<br>";
}
?>
Them bang mot file php them.php
<?php
$link = mysql_connect("localhost", "root", "");
if ($link)
{
echo "kết nối được tới MySQL";
}
mysql_select_db("my_other_db", $link) or die(mysql_error());
$query = "insert into my_table (id, name ) values ('001','entry
one')";
mysql_query($query) or die(mysql_error());
mysql_close($link);
?>

You might also like