thiet ke web - thiet ke website - thiet ke web - Design A website
B 3: Tao v ket no database bng php
ang ngy: 20-10-2009Ln xem: 20168 1.Tao mt co so du u mysq: Ban mo trnh duyt v g d}a ch sau: http://ocahost/phpmyadmn G vo hp Create new database tn database o dy guestbook v nhan Create. Tao bng guestbook bng cch cck chut vo database va tao trong tab structure nhp tn tabe guestbook, Number of feds 5. Sau d nhan Go: Tao cc feds sau: name varchar 50, ocaton varchar 200, ema varchar 100, ur varchar 100, comments text. Ket no co so d u bng PHP: Bo v PHP v MySOL hn ta trn my cua ban van cn ha the g tch bt nhau. Do d, muon dng PHP de m vc duoc v CSDL ban cn ph tao ra so dy n ket ga ha chen hu ny. <? mysq_connect("ocahost", "root","") or de ("Coud not connect to database"); mysq_seect_db("guestbook") or de ("Coud not seect database"); ?> Dng du tn thuc hn vc ket no v Database Server dang nm trn my ocahost, c username root, password rng. Neu ket no thnh cng, n s thuc hn buc ke tep ket no v database nm trong d guestbook bng nh mysq_seect_db(). Cc ban nn uu cc nh trn ban su dung thung xuyn cho mo ket no CSDL cua ban, do d t khuyn ban nn uu n vo mt tp tn dbconnect.php sau ny cn th ch vc dng nh ncude(dbconnect.php); Vet trang submt du u: Hy bat tay vo cng vc d th. ban tao mt trang sgn.php o thu muc goc cua web oca page 1 / 3 thiet ke web - thiet ke website - thiet ke web - Design A website c n dung nhu sau: <h2>Sgn my Guest Book!!!</h2> <form method=post acton="create_entry.php"> <b>Name:</b> <nput type=text sze=40 name=name> <br> <b>Locaton:</b> <nput type=text sze=40 name=ocaton> <br> <b>Ema:</b> <nput type=text sze=40 name=ema> <br> <b>Home Page URL:</b> <nput type=text sze=40 name=ur> <br> <b>Comments:</b> <textarea name=comments cos=40 rows=4 wrap=vrtua></textarea> <br> <nput type=submt name=submt vaue="Sgn!"> <nput type=reset name=reset vaue="Start Over"> </form> Tao mt trang create_entry.php o thu muc goc c n dung nhu sau nhu sau: <?php ncude("dbconnect.php"); // ket no co so du eu $name = $_POST|"name"|; $ocaton = $_POST|"ocaton"|; $ema = $_POST|"ema"|; $ur = $_POST|"ur"|; $comments = $_POST|"comments"|; f ($submt == "Sgn!") { $query = "nsert nto guestbook (name,ocaton,ema,ur,comments) vaues ('$name', '$ocaton', '$ema', '$ur', '$comments')"; mysq_query($query) orde (mysq_error()); ?> <h2>Thanks!!</h2> <h2><a href="vew.php">Vew My Guest Book!!!</a></h2> <?php } ese { ncude("sgn.php"); } page 2 / 3 thiet ke web - thiet ke website - thiet ke web - Design A website ?> uoc ro, ban thu g d}a ch sau vo trnh duyt http://ocahost/sgn.php, nhp thng tn v nhan submt. admn page 3 / 3