PHP VAØ DATABASE
9-4
COMPUTER LEARNING CENTER
WWW.HUUKHANG.COM
</tr>
<tr>
<td align="left" >Name:</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"> <br>
<input type="submit"
value="Submit"
class="button">
<input type="reset" value="Reset" class="button">
</td>
</tr>
</form>
Löu yù raèng, baïn khai baùo soá kyù töï lôùn nhaát cho pheùp nhaäp baèng vôùi kích thöôùc ñaõ khai baùo
trong cô sôû döõ lieäu öùng vôùi thuoäc tính maxlength.
Khi ngöôøi söû duïng nhaäp hai giaù trò vaø nhaán nuùt submit, trang keá tieáp ñöïôc trieäu goïi. Trang naøy
laáy giaù trò nhaäp baèng caùch söû duïng bieán form hay $HTTP_POST_VARS. Ñoái vôùi tröôøng hôïp naøy
chuùng ta söû duïng bieán form nhö trang doinsert.php.
<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
<BODY>
<h3>Them mau tin</h3>
<?php
$affectrow=0;
require("dbcon.php");
$sql="insert into tblships(ShipID,ShipName) ";
$sql .=" values('".$txtID."','".$txtName."')";
$result
=
mysql_query($sql,$link);
if($result)
$affectrow=mysql_affected_rows();
mysql_close($link);
?>
So mau tin them vao<?= $affectrow?>
</BODY>
</HTML>
3.
CAÄP NHAÄT MAÅU TIN
Ñoái vôùi tröôøng hôïp caäp nhaät maåu tin, baïn cuõng söû duïng haøm mysql_query vôùi phaùt bieåu Update
thay ví Insert nhö treân, ví duï chuùng ta khai baùo trang update.php ñeå caäp nhaät maåu tin trong baûng
tblShips vôùi teân laø UpdateTesting khi maõ coù giaù trò laø A01.
<HTML>
<HEAD>
<TITLE>::Welcome to PHP and mySQL</TITLE>
</HEAD>
<BODY>
<h3>Cap nhat mau tin</h3>
<?php
require("dbcon.php");
$sql="Update tblships set ShipName='UpdateTesting' ";
$sql.="
where
ShipID='A01'";
$result
=
mysql_query($sql,$link);
$affectrow=0;
if($result)