Cov txheej txheem:

Yuav Ua Li Cas Sau Ntawv Xov Xwm Lub Vev Xaib Siv PHP thiab MYSQL: 5 Kauj Ruam
Yuav Ua Li Cas Sau Ntawv Xov Xwm Lub Vev Xaib Siv PHP thiab MYSQL: 5 Kauj Ruam

Video: Yuav Ua Li Cas Sau Ntawv Xov Xwm Lub Vev Xaib Siv PHP thiab MYSQL: 5 Kauj Ruam

Video: Yuav Ua Li Cas Sau Ntawv Xov Xwm Lub Vev Xaib Siv PHP thiab MYSQL: 5 Kauj Ruam
Video: 8 yam tsis txhob ua thaum sib deev tag tsis li yuav phom sij txog lub neej txoj sia. 2024, Kaum ib hlis
Anonim
Yuav Ua Li Cas Sau Ntawv Xov Xwm Lub Vev Xaib Siv PHP thiab MYSQL
Yuav Ua Li Cas Sau Ntawv Xov Xwm Lub Vev Xaib Siv PHP thiab MYSQL

Cov lus qhia no yuav qhia koj yuav ua li cas los tsim cov ntawv xov xwm lub vev xaib siv php, mysql, html, thiab css. Yog tias koj yog tus tshiab rau kev txhim kho lub vev xaib, tsis txhob txhawj xeeb, yuav muaj cov lus piav qhia ntxaws thiab piv txwv yog li koj yuav nkag siab cov ntsiab lus zoo dua.

Cov Ntaub Ntawv Xav Tau:

  • Text editor (piv txwv li Sublime Text, Atom, thiab lwm yam). Kuv yuav siv cov ntawv zoo.
  • WAMP sawv rau Windows Apache MySQL thiab PHP
  • MAMP sawv rau Mac Apache MySQL thiab PHP

MAMP rau Mac cov neeg siv: https://www.mamp.info/en/ (suav nrog MySQL thiab PHP)

WAMP rau cov neeg siv Windows: https://www.wampserver.com/en/ (suav nrog MySQL thiab PHP)

Cov ntawv zoo:

Kauj ruam 1: Txheej txheem cej luam

Txoj hauv kev kom taug qab cov lus yog khaws lawv hauv cov ntaub ntawv khaws tseg. Hauv qhov kev qhia no, peb yuav siv MySQL. (Kuv tab tom siv WAMP txij li kuv muaj lub Windows raws li kev ua haujlwm)

  1. Kho PHP thiab MySQL los ntawm kev ua kom lawv tau rub tawm thiab koj tuaj yeem taw rau lawv cov ntaub ntawv qhov chaw ntawm koj lub computer.
  2. Qhib cov lus txib tam sim thiab mus rau koj phau ntawv teev npe mysql thiab ntaus "mysqladmin -u root -p password." Cov lus txib no yuav nug koj tus password thiab kom koj nkag mus tau dua.
  3. Tam sim no pib koj li WAMP lossis MAMP server. Yuav muaj lub cim ntsuab tshwm thaum tus neeg rau zaub mov tiav thauj khoom. Nyem rau lub cim -> MySQL -> MySQL console kom paub tseeb tias koj tuaj yeem nkag mus nrog koj tus lej tshiab uas tau tsim.
  4. Tam sim no txoj haujlwm yog los tsim cov chaw khaws ntaub ntawv uas cov ntaub ntawv hais txog cov lus yuav khaws cia. Cia peb xav txog dab tsi feem ntau xav tau los ntawm cov lus. Cov khoom ib txwm suav nrog: Npe, lub sijhawm tshaj tawm xov xwm, sijhawm tshaj tawm cov lus, thiab cov lus nws tus kheej.
  5. Tsim database hu ua "message".
  6. Tsim cov npe hu ua "lus pom" thiab ntxiv kab: id (Hom hom uas pib nce zuj zus), npe (Varchar type), tawm tswv yim (Varchar yam), sijhawm (Varchar type), hnub tim (Varchar type),
  7. Txuas mus rau "cov lus" database hauv php cov ntaub ntawv npe "db.php".
  8. Tsim cov lus teb rau tus sau lub npe thiab thaj chaw ntawv rau cov lus.
  9. Txheeb xyuas cov ntaub ntawv ua ntej tso rau hauv lub database. Yog tias txhua yam zoo, ntxig cov lus, lub npe sau, hnub tim thiab sijhawm uas tus tshaj tawm tau tshaj tawm.
  10. Zaub cov lus los ntawm kev khaws tag nrho cov ntaub ntawv khaws tseg rau hauv html div thiab style cov nplooj ntawv siv CSS.
  11. Horray, koj tau kawm tiav los tsim cov ntawv xa xov.

Kauj Ruam 2: Kauj Ruam 2 - Teeb ຖານ Database

Kauj Ruam 2 - Teeb ຖານ Database
Kauj Ruam 2 - Teeb ຖານ Database
Kauj Ruam 2 - Teeb ຖານ Database
Kauj Ruam 2 - Teeb ຖານ Database
Kauj Ruam 2 - Teeb ຖານ Database
Kauj Ruam 2 - Teeb ຖານ Database

Daim duab 1 - Thaum tsim cov chaw khaws ntaub ntawv, koj tuaj yeem nkag mus rau qhov hais kom ua 'qhia cov chaw khaws ntaub ntawv' kom ntseeg tau tias cov ntaub ntawv tsim tau ua tiav.

QHIA DATABASES;

Daim duab 2 - Txhawm rau khaws cov ntaub ntawv hauv lub database ib lub rooj xav tau los tsim. Tsim lub rooj nrog kab tseem ceeb yog qhov tseem ceeb kom ntseeg tau tias lub vev xaib muaj txhua cov ntaub ntawv tsim nyog. Qhov lus tawm tswv yim yuav muaj txhua yam ntaub ntawv tsim nyog

Txhawm rau kom tuaj yeem txuas mus rau cov ntaub ntawv xov xwm, tshaj tawm "GRANT ALL PRIVILEGES ON message.*" Hais kom ua. Koj tuaj yeem siv koj tus kheej lub npe siv thiab tus lej nkag, tsuas yog thov tsis txhob hnov qab nws.

CREATE DATABASE message; GRANT ALL PRIVILEGES ON message.* TO 'username'@'localhost' IDENTIFIED BY 'password';

Daim duab 3 - Sau 'piav qhia cov lus' kom paub tseeb tias lub rooj tau tsim tiav. Koj tuaj yeem pom kab thiab lawv cov ntaub ntawv los ntawm kev tshaj tawm cov lus txib no.

DESCRIBE COMMENTS;

Daim duab 4 - Txij li kab lus rau cov lus pom tsis yog thaum xub thawj, cia peb ntxiv nws nrog ALTER TABLE hais kom ua. Cov lus tawm tswv yim yog hom VARCHAR (255) qhov no txhais tau tias cov lus pom yuav yog cov ntawv uas tsis tuaj yeem ntev tshaj 255 cim.

QHOV TABLE cov lus ADD COLUMN comments VARCHAR (255) TSIS NULL;

255 sawv cev rau qhov ntev tshaj plaws rau kev tawm tswv yim. Tsis yog txhais tau tias thaum cov lus tau ntxig rau hauv lub database, cov lus teb hauv lub database tsis tuaj yeem khoob (tsis muaj; tsis muaj).

Kauj ruam 3: Tsim Php Cov Ntaub Ntawv

Cov kauj ruam hauv qab no yuav tshwm sim hauv koj cov ntawv kho kom raug xaiv. Kuv yuav siv cov ntawv zoo.

1. Peb yuav xav tsim ob daim ntawv. Ib qho yuav raug hu ua db.php uas yuav txuas lossis lub vev xaib mus rau MYSQL (kuv tus qauv lus nug lus) database thiab index.php, nplooj ntawv uas cov lus yuav tshwm sim.

2. Ua ntej, cia peb tsim db.php. Thaum lub database tau teeb tsa cov ntawv pov thawj kev sib txuas los txuas rau lub database. Cov ntaub ntawv no yuav txuas rau index.php rau lub database kom peb tuaj yeem sau nws nrog cov ntaub ntawv xav tau los tsim chav sib tham.

Code rau db.php

? php txhais ("DB_SERVER", "localhost");

txhais ("DB_USER", "kylel95"); txhais ("DB_PASSWORD", "password"); txhais ("DB_NAME", "lus"); $ txuas = mysqli_connect (DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);

yog (mysqli_connect_errno ())

{tuag ("Kev sib txuas Database ua tsis tiav:". mysqli_connect_error (). "(". mysqli_connect_errno (). ")"); }

?>

3. Tom ntej no, cia tsim index.php. Cov ntaub ntawv no yog qhov uas peb tsim chav sib tham. Peb yuav siv html cov lus teb rau lub npe, textarea rau cov lus, thiab cov ntsiab lus sib cais kom muaj cov lus. Peb yuav siv CSS (Cascase Style Sheet) los teeb tsa nplooj ntawv thiab kho cov nplooj ntawv cov ntsiab lus.

code rau index.php

? phprequire_once ('db.php');

yog (isset ($ _ POST ['xa'])) {$ sijhawm = hnub tim ("g: i: s A"); $ hnub = hnub tim ("n/j/Y"); $ msg = $ _POST ['message']; $ lub npe = $ _POST ['fname']; $ tshwm sim = ""; if (! empty ($ msg) &&! empty ($ name)) {// name time date message $ query = "INSERT INTO comments ("; $ query. = "name, time, date, comment"; $ query. = ") VALUES ("; $ query. = "'{$ Name}', '{$ time}', '{$ date}', '{$ msg}'"; $ query. = ")"; $ tshwm sim = mysqli_query ($ txuas, $ nug); }}?> Yog

textarea {ciam teb-vojvoog: 2%; } #thread {ciam teb: 1px #d3d3d3 khoom; qhov siab: 350px; dav: 350px; hla: scroll; }

? php $ select = "SELECT * FROM comments"; $ q = mysqli_query ($ txuas, $ xaiv); thaum ($ row = mysqli_fetch_array ($ q, MYSQLI_ASSOC)) {ncho $ row ['name']. ":". $ row ['saib']. ""; }?> Npe: Xa

4. Peb yuav siv php's built in function date () kom tau txais hnub thiab sijhawm cov lus tau tshaj tawm thiab khoob () kom paub tseeb tias cov neeg siv tsis nkag mus rau cov lus lossis npe.

5. Thaum cov ntaub ntawv xa tuaj, peb yuav ntxig lub npe, lus, lub sijhawm thiab hnub tim rau hauv lub database.

6. Tam sim no peb yuav nug los ntawm lub hauv paus thiab tau txais txhua cov lus pom. Cov lus pom yuav muab khaws cia rau hauv ib qho div.

?>

Kauj Ruam 4: Sau Db.php thiab Index.php Cov Ntaub Ntawv

Populate Db.php thiab Index.php Cov Ntaub Ntawv
Populate Db.php thiab Index.php Cov Ntaub Ntawv
Populate Db.php thiab Index.php Cov Ntaub Ntawv
Populate Db.php thiab Index.php Cov Ntaub Ntawv
Populate Db.php thiab Index.php Cov Ntaub Ntawv
Populate Db.php thiab Index.php Cov Ntaub Ntawv

1. Lub db.php (db luv rau database) siv lub npe siv thiab tus password kuv siv thaum lub database tau tsim thaum kuv tshaj tawm "GRANT ALL PRIVILEGES" hais kom ua.

Kev txhais ua haujlwm nyob rau sab saud ntawm cov ntaub ntawv qhia tias qhov sib txawv DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME yog qhov ruaj khov (lawv tsis hloov pauv tus nqi). Yog tias cov ntawv txheeb xyuas yog tias kev txuas mus rau lub database tsis ua tiav lossis tsis.

2. Hauv cov ntawv index.php, peb yuav siv php qhov xav tau los ua haujlwm txuas cov nplooj ntawv rau lub database. Tom ntej no, tsim cov lus teb rau lub npe. Tom qab ntawd ua lub textarea rau cov lus. Thaum cov no tau tsim mus ntsib https:// localhost: 8080/messages/index.php (koj yuav tsis muaj tus lej chaw nres nkoj piv txwv li 8080) kom paub tseeb tias koj pom lub thawv nkag thiab textarea.

3. Tom ntej no, cia peb sau cov ntaub ntawv cov ntaub ntawv thiab tom qab ntawd ntxig rau hauv lub database.

4. Tam sim no, cia peb ntxiv cov lus nug database thiab tso tawm txhua cov lus rau hauv div.

5. Txhawm rau ua kom yam zoo me ntsis, cia ntxiv qee qhov CSS los ua kom nws zoo nkauj me ntsis.

PS: Kuv tau sau yuam kev. Thov hloov 'lus' los tawm tswv yim ze rau INSERT hais kom ua rau $ cov lus nug sib txawv.

Kauj Ruam 5: Kauj Ruam 5: Piv Txwv thiab Cov Kauj Ruam Tom ntej

Kauj ruam 5: Piv txwv thiab Cov Kauj Ruam Tom ntej
Kauj ruam 5: Piv txwv thiab Cov Kauj Ruam Tom ntej
Kauj ruam 5: Piv txwv thiab Cov Kauj Ruam Tom ntej
Kauj ruam 5: Piv txwv thiab Cov Kauj Ruam Tom ntej

Vam tias koj pom qee yam zoo ib yam rau saum toj no tom qab ntxiv css thiab sim nws tawm.

Cov kauj ruam tom ntej: Ntxiv css ntau ntxiv kom ua rau lub xaib nyiam dua.

Pom zoo: