Professional Documents
Culture Documents
<?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);
?>
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 ?>
<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"> </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);
?>
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);
?>