Академический Документы
Профессиональный Документы
Культура Документы
Ni dung
Nhng vn chnh c cp:
Khai bo kt ni c s d liu Cp nht 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.
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)
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]);
- Nu ngi dng chn nt save, cho php chp nhn thng tin ng k.
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]."'";
$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
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);
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
<?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>