Вы находитесь на странице: 1из 34

Kt ni vi MySQL v truy xut c s d liu

Ni dung
Nhng vn chnh c cp:
Khai bo kt ni c s d liu Cp nht d liu.

Kch hot d liu


xa d liu

I. Khai bo kt ni c s d liu.
kt ni c s d liu mySQL trn my ch ta dng hm mysql_connect().

Khai bo nh sau: $link = mysql_connect($host, $user, $password); Cc tham s c gi cho hm (My ch, tn ng k, mt khu) c da trn c s tng ti khon v u tin c thit lp trong c s d liu MySQL. Thng thng my ch c xc nh l localhost.

Nu kt ni c thc hin, bin $link c tr thnh im tham


chiu. Nu b qua chng s t ng s dng kt ni ang c m.

1. Kt ni c s d liu.
V d: <?php $link = mysql_connect(servername, username, password); if (!$link){ Echo Could not connect to MySQL Database; Exit; } ?> C th s dng pht biu nh sau: <?php $link = mysql_connect ("localhost", "root", "") or die ("Could not connect to MySQL Database"); ?> Lnh die() cho php kt thc kt ni v gii phng ton b cc bin khi to kt ni c s d liu trc .

2. M c s d liu
Sau khi kt ni thnh cng, c th khai bo m CSDL theo hai cch: mysql_select_db(Database name); mysql_select_db("Database name", $link); V d: <?php $db = mysql_connect(localhost,root,); If (!$db){ Echo Could not connect to MySQL Database; Exit; } mysql_select_db(InterShop,$link); ?>

2. M c s d liu
Hoc: <?php $link = mysql_connect(localhost,root,) or die (Could not connect to MySQL Database.mysql_error());

mysql_select_db(InterShop,$link);
?>
Kt ni c s d liu trn c lu trong file dbcon.inc T sau tr i ta c th s dng kt ni bng cu lnh. Require(dbcon.inc);

3. ng kt ni CSDL
Khi khng s dng kt ni vi CSDL, ta c th dng hm ng kt ni vi CSDL nh sau: mysql_close (Database connection); <?php $link = mysql_connect(localhost,root,) or die (Could not connect to MySQL Database); mysql_select_db(InterShop,$link); .... mysql_close(InterShop); ?>

4. Truy vn c s d liu
truy vn CSDL, dng hm mysql_query() vi tham s bt buc l chui sql, v cc tham s ty chn. Int mysql_query(string sql); Int mysql_query(string sql, [int db_connect]); Int mysql_query(string sql, $link, [int db_connect]); V d: <?php $link = mysql_connect(localhost,root,) or die (Could not connect to MySQL Database); mysql_select_db(InterShop); $sql = select * from tblusers; $result = mysql_query($sql, $link); mysql_close($link); ?>

5. S lng mu tin
bit s lng mu tin truy vn tr v dng hm. Mysql_num_rows($result); V d: <?php Require(dbcon.inc); $sql = select * from tblusers where username =;

$sql .= $_POST[txtuser]. ;
$result = mysql_query($sql,$link); $num = mysql_num_rows($result); Echo Number of records = $num;

Mysql_close($link);
?>

6. Truy vn d liu
S dng hm mysql_fetch_array() c tng bn ghi. Hm ny s c tng bn ghi v chuyn xung bn ghi tip theo. V d: <?php Require(dbcon.inc); $sql = select * from tbllanguages; $result = mysql_query($sql, $link); ?> <table><tr><td><b>Code</b></td><td><b>Name</b></td></tr> <?php If(mysql_num_rows($result) !=0) While ($row = mysql_fetch_array($result)) { Echo <tr><td>$row[0]</td>; echo <td>$row[1]</td></tr>; } Mysql_close($link); ?></table>

6. Truy vn d liu
S dng hm mysql_fetch_object() c d liu vo i tng, sau s dng thuc tnh $row->FieldName ly d liu. V d: <?php Require(dbcon.inc); $sql = select * from tbllanguages; $result = mysql_query($sql, $link); ?> <table><tr><td><b>Code</b></td><td><b>Name</b></td></tr><?php If(mysql_num_rows($result) !=0)

While ($row = mysql_fetch_object($result)) { Echo <tr><td>$row->LangID</td>; echo <td>$row->LangName</td></tr>; }


Mysql_close($link); ?></table>

6. Truy vn d liu
Ngoi cc hm trn cn mt s hm thao tc vi c s d liu nh: Mysql_create_db(string database name[,databaseconnection]);

Mysql_drop_db(string database name[,databaseconnection]);

II. Thm thng tin vo CSDL


S dng hm mysql_query(chui Insert). V d: Khi thm thng tin vo trong bng tbllanguages gm hai trng LangID, LangName <?php require("dbcon.inc"); $sql="insert into tbllanguages values('RUS','Russia')"; $result = mysql_query($sql,$link); ?> Thng thng khi nhp thng tin vo CSDL, cn xy dng mt trang PHP dng xc nhn li cc thng tin ca ngi nhp trc .

II. Thm thng tin vo CSDL


Trng hp ng k mi ngi dng vo bng tblusers. C th xy dng cc trang x l theo cc bc sau: - Ngi dng nhp thng tin v n vo nt Register me! - Trang xc nhn s ly cc thuc tnh t trang ng k bng bin form hay thng qua mng $_POST. - Kt ni CSDL kim tra user name tn ti hay cha?
+ Nu user tn ti, thng bo cho ngi dng bit, cho php tr v trang trc.
+ Nu cha c, cho php ngi s dng tr v hay tip tc ng k.

- Nu ngi dng chn nt save, cho php chp nhn thng tin ng k.

Trang ng k ngi dng newuser.php


Trc tin cn chn on Javascript kim tra d liu nhp. <script language="javascript"> function CheckInput(){ if (document.FInput.txtuser.value == ""){ alert('Nhp vo username'); document.FInput.txtuser.focus(); return false; } if (document.FInput.txtpass.value == ""){ alert('Nhp vo password'); document.FInput.txtpass.focus(); return false; } return true; }</script>

Trang ng k ngi dng newuser.php


Khai bo th form v hai th input dng text yu cu ngi s dng nhp ID v Name nh sau: <form name="FInput" action="NewConf.php?goto =" onsubmit="return CheckInput();" method="post" > User name: <input type="text" name="txtuser" width="20" maxlength="50" /> <br> Password: <input type="password" name="txtpass" width="10" maxlength="10" /> <br> <input type="submit" name="submit" value="Register me!" /> <input type="reset" name="reset" value="cance" /> </form>

Trang ng k ngi dng newuser.php


Ch : khai bo s k t ln nht cho php nhp vo bng vi kch thc khai bo trong c s d liu ng vi thuc tnh maxlength. Khi ngi s dng nhp hai gi tr v nhn nt submit, trang k tip Newconf.php c gi. Trang ny ly gi tr nhp bng cch s

dng bin form hay $_POST.

ng k ngi dng
Trang Newconf.php kim tra xc nhn thng tin on m m c s d liu kim tra.

<?php
require("dbcon.inc"); $sql = "select * from tblusers where username = '".$_POST[txtuser]."'";

$result = mysql_query($sql, $link);


$exist = 0; if (mysql_num_rows($result)!=0) {

$exist = 1;
mysql_close($link); } ?>

ng k ngi dng
Nu user cha tn ti cho php gi d liu chn vo CSDL: <?php if ($exist == 1){ ?> User tn ti Chn User khc ng k <?php } if ($exist == 0){ ?> <form name="Fconf" method="post" action="doreg.php"> <input type="submit" name="new" value="save" /> <input type="hidden" name="txtuser" value="<?php echo $_POST[txtuser]; ?>" /> <input type="hidden" name="txtpass" value="<?php echo $_POST[txtpass]; ?>" /> <input type="hidden" name="form_" value="newuser" /> </form> <?php } ?>

ng k ngi dng
Sau khi d liu c kim tra tha mn, cp nht thng tin vo CSDL. on m c th hin trang doreg.php

<?php require("dbcon.inc"); if ($_POST[form_] == "newuser") { $sql = "insert into tblusers(username, password) values ('".$_POST[txtuser]."','".$_POST[txtpass]."')"; } else{ //echo Update; } mysql_query($sql, $link); mysql_close($link); ?>

ng k ngi dng
Mt s vn cn trnh khi lu d liu. Nu ngi dng tip tc n phm F5 hay Refresh li mang hnh, trang doreg.php s thc hin li mt ln na. Trong trng hp ny, cn phi xc nh sau khi lu d liu, phi c c (flag) nhn bit thng tin c lu. Nu s dng bin session nhn bit, c th khi to bin session

trong trang NewConf.php. Vi gi tr isSave = N, khi lu kt qu vo


CSDL ta gn cho isSave = Y. C th dng mt trang sau trang doreg. Php thng bo vic ng nhn thnh cng.

III. Kch hot d liu


Cc vn v kch hot d liu: Khi ngi dng ng nhp h thng, mun xem thng tin ngi dng

v cp nht, thay i thng tin nu cn thit.


kch hot thng tin xc nhn thng qua UserID, UserID c th ly t session ca ngi dng sau khi ng nhp hoc t gi tr ca ngi qun tr cung cp. Trong trng hp edit d liu cu ngi dng, khng cho php ngi dng thay i username. Cc th chn la nh select, radio, Checkbox lu gi tr cn phi chn

n ng gi tr c lu trong c s d liu.
gii quyt vn ny c th vit bng PHP hoc javascript.

on m trang login.html
on m script kim tra d liu nhp vo: <SCRIPT language=JavaScript> function checkInput() { if (document.frmLogin.txtUser.value==""){ alert("Invalid Email, Please enter your own Email"); document.frmLogin.txtUser.focus(); return false; } if (document.frmLogin.txtPWD.value=="") { alert("Please enter your own Password"); document.frmLogin.txtPWD.focus(); return false; } ..... return true; } </script>

on m trang login.html
Thit k mt Form submit d liu vi phng thc POST. <form name="frmLogin" method="post" action="login_authentication.php?goto=" onSubmit="return checkInput();"> E-mail Address: <input type="text" name="txtUser" size="25" maxlength="100"> <br> Password: <input type="password" name="txtPWD" size="25" maxlength="8" > <br> <input type="submit" style="width=80px" name="Signin" value="Sign me in!" class="button"> <input type="reset" name="Reset" value="Reset" class="button"> </form>

Trang login_authentication.php
ng k "suid","susername","sfullname vo session: <?php session_start(); require("dbcon.inc"); $sql="select * from tblusers where username='".$_POST["txtUser"]."'"; $result = mysql_query($sql,$link); $i = mysql_num_rows($result); if($i){ $row=mysql_fetch_row($result); echo "No of Records: ".$i; $suid=$row[0]; $susername=$row[1]; $sfullname=$row[3]; session_register("suid","susername","sfullname"); } mysql_close($link); ?>

Trang edituser.php
Sau khi ng k session. Trang edituser.php s ly userID t session v hin th thng tin truy vn. on m truy vn d liu: <?php

session_start();
require("dbcon.inc"); $sql="select * from tblUsers where UserID='".$_SESSION[suid]."'"; $result = mysql_query($sql,$link); ?>

Trang edituser.php
on m hin th thng tin. <form name="frmNew" method="post" action="editconf.php" onSubmit="return checkInput();"> <?php if (mysql_num_rows($result)!=0) { $row = mysql_fetch_row($result);

echo Username:. $row[1].<br>;


?> Password:<input type="password" name="txtPWD" size="25" maxlength="8" value="<?php echo $row[2]?>"> <br> ....

Trang edituser.php
on m hin th thng tin th select. <select type="text" name="cbProvince" class="textbox"> <option value="" selected>-Select one-</option> <option value="HCM" <?php if($row[6]=="HCM") echo " selected";?> >Ho Chi Minh</option> <option value="HAN" <?php if($row[6]=="HAN") echo " selected";?> >Ha Noi</option> ... </select> <input type="submit" style="width=120px" name="new" value="Register me!" class="button"> <input type="reset" name="Reset" value="Reset" class="button"> <?php } ?> </form>

Trang edituser.php
Sau khi thay i thng tin trn trang edituser.php. Trang editconf.php s xc nhn li cc thng tin ngi dng. Nu tip tc chn Update,

trang doreg.php s c triu gi.

Trang doreg.php thc hin mt trong hai pht biu SQL. Nu gi t

trang newconf.php l ng k mi (insert into). Nu gi t trang


editconf.php, trang ny s update d liu.

Trang doreg.php
<?php require("dbcon.inc"); if ($_POST[form_] == "newuser") { $sql = "insert into tblusers(username, password) values ('".$_POST[txtuser]."','".$_POST[txtpass]."')"; }else{ // update $sql = "update tblusers set password = '".$_POST[txtPWD]; $sql.= "',FullName = '". $_POST[txtFullName]; $sql.= "',Address = '".$_POST[txtAddress]; $sql.= "',Tel = '". $_POST[txtTel]; $sql.= "',City = '. $_POST[txtCity]; $sql.= "',Province = '". $_POST[cbProvince]; $sql.= "' where userID = '".$_SESSION[suid]."'"; } mysql_query($sql, $link); mysql_close($link); }?>

X l d liu hin th
Vi th select: Khi d liu danh mc c lu trong CSDL, nh v d trn danh mc tnh thnh khng c nh, c cp nht v lu trong bng d liu. Vn phi gii quyt bng cch c m v tn tnh thnh t Table, sau thay th chui trong th select. Vi th input dng radio: Nh gii tnh c lu trong CSDL, khi hin th thng tin, cn chn ng gii tnh. Vi th input dng checkbox: Khi ngi dng nhp d liu dng checkbox, d liu c lu trong CSDL. Cn phi th hin thng tin ra trang WEB ch no c chn, ch no khng c chn. Vn c gii quyt bng cch chn on m PHP vo trong th.

on m hin th th select
<?php $sql = "select * from tblProvinces order by ProvinceName"; $result = mysql_query($sql,$link); $option = ""; if (mysql_num_rows($result)!=0){ while ($Prorow = mysql_fetch_row($result)){ $option .= "<option value =".$Prorow[0]; if ($Prorow[0]==$row[6]){$option .= "selected";} $option .= ">".$Prorow[1]."</option>"; } } ?> <select type="text" name="cbProvince" class="textbox"> <option value="" selected>-Select one-</option> <?php echo $option ?> </select>

X l vi th input dng radio


Th input dng radio th hin gii tnh, ly thng tin lu trong CSDL <?php if (mysql_num_rows($result)!=0){ $row =mysql_fetch_row($result); ?> <input type = radio name = GT value = M <?php if ($row[8] == M) echo checked;?>> Male <input type = radio name = GT value = F <?php if ($row[8] == F) echo checked;?>> Female <?php } ?>

X l vi th input dng checkbox


Th checkbox s hin ngnh ngh c chn ly thng tin t CSDL <?php if (mysql_num_rows($result)!=0){ $row =mysql_fetch_array ($result); $NN = $row[NganhNghe];?> <input type = checkbox name = chkGV value = GV <?php if (strstr($row[$NN],GV) echo checked;?>> GV <input type = checkbox name = chkNC value = NC

<?php if (strstr($row[$NN],NC) echo checked;?>> NC


<?php } ?>

Вам также может понравиться