Lihtsaid php ja mysql näiteid: erinevus redaktsioonide vahel
Resümee puudub |
Resümee puudub |
||
| (ei näidata sama kasutaja 13 vahepealset redaktsiooni) | |||
| 1. rida: | 1. rida: | ||
Allpool on näited kasutajana st2014 ja andmebaasiga st2014. Lisainfot tasub vaadata w3schoolsi [https://www.w3schools.com/php/default.asp php tutorialist], aga siintoodud näidetest peaks neljanda praksi jaoks juba piisama. | |||
Hello world php-s: | |||
http://dijkstra.cs.ttu.ee/~tammet/k1.php | http://dijkstra.cs.ttu.ee/~tammet/k1.php | ||
<pre> | <pre> | ||
| 8. rida: | 11. rida: | ||
</pre> | </pre> | ||
Nii saab uurida PHP settungeid, praksis see otseselt oluline ei ole: | |||
http://dijkstra.cs.ttu.ee/~tammet/k2.php | http://dijkstra.cs.ttu.ee/~tammet/k2.php | ||
<pre> | <pre> | ||
| 22. rida: | 26. rida: | ||
</pre> | </pre> | ||
Esimene andmebaasipäring: | |||
http://dijkstra.cs.ttu.ee/~tammet/k3.php | http://dijkstra.cs.ttu.ee/~tammet/k3.php | ||
<pre> | <pre> | ||
Algus.<p> | Algus.<p> | ||
<?php | <?php | ||
$con = | $con = mysqli_connect("localhost","st2014","progress","st2014"); | ||
if (!$con) { | if (!$con) { | ||
die('Could not connect: ' . | die('Could not connect: ' . mysqli_connect_error()); | ||
} | } | ||
$result = mysqli_query($con,"SELECT * FROM tt_users"); | |||
$result = | |||
while($row = | while($row = mysqli_fetch_array($result)) { | ||
echo $row['username'] . " " . $row['name']; | echo $row['username'] . " " . $row['name'] ; | ||
echo "<br />"; | echo "<br />"; | ||
} | } | ||
mysqli_close($con) | |||
?> | ?> | ||
| 49. rida: | 50. rida: | ||
</pre> | </pre> | ||
Kui $_REQUEST väljadel ei ole väärtust, siis võib mõni PHP versioon anda rämeda warningu (ja mõni teine jälle ei pruugi): | |||
http://dijkstra.cs.ttu.ee/~tammet/k4.php | http://dijkstra.cs.ttu.ee/~tammet/k4.php | ||
<pre> | <pre> | ||
| 75. rida: | 77. rida: | ||
</pre> | </pre> | ||
Warningu vältimiseks on mõistlik kasutada isset kontrollifunktsiooni: | |||
http://dijkstra.cs.ttu.ee/~tammet/k5.php | http://dijkstra.cs.ttu.ee/~tammet/k5.php | ||
<pre> | <pre> | ||
| 106. rida: | 109. rida: | ||
</pre> | </pre> | ||
Andmebaasipäring, kus me ise paneme kokku sql where tingimuse vastavalt kasutaja antud väärtustele (siin näites ei ole see turvaliselt tehtud, vaata paari järgmist). | |||
http://dijkstra.cs.ttu.ee/~tammet/k6.php | http://dijkstra.cs.ttu.ee/~tammet/k6.php | ||
<pre> | <pre> | ||
<body> | <body> | ||
Algus. | Algus. | ||
| 114. rida: | 119. rida: | ||
<?php | <?php | ||
$con = | $con = mysqli_connect("localhost","st2014","progress","st2014"); | ||
if (!$con) { | if (!$con) { | ||
die('Could not connect: ' . | die('Could not connect: ' . mysqli_connect_error()); | ||
} | } | ||
$ | $username = "tanel"; | ||
if (isset($_REQUEST[' | if (isset($_REQUEST['username'])) { | ||
$ | $username = $_REQUEST['username']; | ||
} | } | ||
$q="SELECT * from users where | $q="SELECT * from users where username='$username'"; | ||
echo "query: $q <br>"; | echo "query: $q <br>"; | ||
$result = | $result = mysqli_query($con,$q); | ||
echo "leitud:<p>"; | echo "leitud:<p>"; | ||
while($row = | while($row = mysqli_fetch_array($result)) { | ||
echo $row['username'] . " " . $row['name']; | echo $row['username'] . " " . $row['name']; | ||
echo "<br />"; | echo "<br />"; | ||
} | } | ||
mysqli_close($con); | |||
?> | ?> | ||
<form method="post"> | <form method="post"> | ||
username <input type="text" name="username" autocomplete="off"><br> | |||
<input type="submit"> | <input type="submit"> | ||
</form> | </form> | ||
| 149. rida: | 152. rida: | ||
<p> | <p> | ||
Lopp. | Lopp. | ||
</body> | |||
</pre> | |||
< | <!-- | ||
Veidi keerukama where tingimuse kokkuklopsimine kasutaja antud väärtustest (endiselt ei ole turvaline): | |||
http://dijkstra.cs.ttu.ee/~tammet/k7.php | http://dijkstra.cs.ttu.ee/~tammet/k7.php | ||
<pre> | <pre> | ||
| 175. rida: | 181. rida: | ||
global $q, $c; | global $q, $c; | ||
$con = mysql_connect("localhost"," | $con = mysql_connect("localhost","st2014","..."); | ||
if (!$con) { | if (!$con) { | ||
die('Could not connect: ' . mysql_error()); | die('Could not connect: ' . mysql_error()); | ||
} | } | ||
mysql_select_db(" | mysql_select_db("st2014", $con); | ||
$q="SELECT * from users where "; | $q="SELECT * from users where "; | ||
| 222. rida: | 228. rida: | ||
</pre> | </pre> | ||
--> | |||
Siin paneme juurde esmaselt vajaliku turva (et kasutaja ei saaks sql süntaksit sisestada), selleks on funktsioon mysqli_real_escape_string: | |||
http://dijkstra.cs.ttu.ee/~tammet/k8.php | http://dijkstra.cs.ttu.ee/~tammet/k8.php | ||
<pre> | <pre> | ||
| 233. rida: | 243. rida: | ||
$c=""; | $c=""; | ||
function addcond($fld) { | function addcond($con,$fld) { | ||
global $q, $c; | global $q, $c; | ||
if (isset($_REQUEST[$fld]) && $_REQUEST[$fld]!="") { | if (isset($_REQUEST[$fld]) && $_REQUEST[$fld]!="") { | ||
if ($c!="") $c=$c . " and "; | if ($c!="") $c=$c . " and "; | ||
$c=$c . " $fld='" . | $c=$c . " $fld='" . mysqli_real_escape_string($con,$_REQUEST[$fld]) ."' "; | ||
} | } | ||
} | } | ||
| 244. rida: | 254. rida: | ||
function main() { | function main() { | ||
global $q, $c; | global $q, $c; | ||
$con = mysqli_connect("localhost","st2014","progress","st2014"); | |||
$con = | |||
if (!$con) { | if (!$con) { | ||
die('Could not connect: ' . | die('Could not connect: ' . mysqli_error()); | ||
} | } | ||
$q="SELECT * from users where "; | $q="SELECT * from users where "; | ||
$c=""; | $c=""; | ||
addcond("username"); | addcond($con,"username"); | ||
addcond("name"); | addcond($con,"name"); | ||
addcond("sex"); | addcond($con,"sex"); | ||
if ($c=="") { | if ($c=="") { | ||
| 264. rida: | 271. rida: | ||
$q=$q . $c; | $q=$q . $c; | ||
echo "query: " . $q . "<br>"; | echo "query: " . $q . "<br>"; | ||
$result = | $result = mysqli_query($con,$q); | ||
echo "leitud:<p>"; | echo "leitud:<p>"; | ||
while($row = mysqli_fetch_array($result)) { | |||
echo $row['username'] . " " . $row['name']; | echo $row['username'] . " " . $row['name']; | ||
echo "<br />"; | echo "<br />"; | ||
} | } | ||
} | } | ||
mysqli_close($con); | |||
} | } | ||
| 292. rida: | 299. rida: | ||
</pre> | </pre> | ||
<!-- | |||
Siin on näide faili uploadimisest: | |||
http://dijkstra.cs.ttu.ee/~tammet/k9.php | http://dijkstra.cs.ttu.ee/~tammet/k9.php | ||
<pre> | <pre> | ||
| 315. rida: | 327. rida: | ||
<form enctype="multipart/form-data" method="POST"> | <form enctype="multipart/form-data" method="POST"> | ||
<input type="hidden" name="MAX_FILE_SIZE" value="30000" /> | <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> | ||
Send this file: <input name="userfile" type="file" /> | Send this file: <input name="userfile" type="file" /> | ||
<input type="submit" value="Send File" /> | <input type="submit" value="Send File" /> | ||
| 328. rida: | 340. rida: | ||
</pre> | </pre> | ||
--> | |||
Viimane redaktsioon: 4. november 2017, kell 15:54
Allpool on näited kasutajana st2014 ja andmebaasiga st2014. Lisainfot tasub vaadata w3schoolsi php tutorialist, aga siintoodud näidetest peaks neljanda praksi jaoks juba piisama.
Hello world php-s: http://dijkstra.cs.ttu.ee/~tammet/k1.php
Algus.<p> <?php echo '<p>Hello World</p>'; ?> <p>Lopp.
Nii saab uurida PHP settungeid, praksis see otseselt oluline ei ole: http://dijkstra.cs.ttu.ee/~tammet/k2.php
Algus.<p> <?php echo '<p>calling phpinfo():</p>'; phpinfo(); ?> <p>Lopp.
Esimene andmebaasipäring: http://dijkstra.cs.ttu.ee/~tammet/k3.php
Algus.<p>
<?php
$con = mysqli_connect("localhost","st2014","progress","st2014");
if (!$con) {
die('Could not connect: ' . mysqli_connect_error());
}
$result = mysqli_query($con,"SELECT * FROM tt_users");
while($row = mysqli_fetch_array($result)) {
echo $row['username'] . " " . $row['name'] ;
echo "<br />";
}
mysqli_close($con)
?>
<p>Lopp.
Kui $_REQUEST väljadel ei ole väärtust, siis võib mõni PHP versioon anda rämeda warningu (ja mõni teine jälle ei pruugi): http://dijkstra.cs.ttu.ee/~tammet/k4.php
<body> Algus. <p> <?php echo "Hi, I'm a <b>PHP</b> script with input a: "; echo $_REQUEST['a']; echo " and input b: "; echo $_REQUEST['b']; ?> <form method="post"> a <input type="text" name="a" autocomplete="off"><br> b <input type="text" name="b" autocomplete="off"><br> <input type="submit"> </form> <p> Lopp. </body>
Warningu vältimiseks on mõistlik kasutada isset kontrollifunktsiooni: http://dijkstra.cs.ttu.ee/~tammet/k5.php
<body>
Algus.
<p>
<?php
if (isset($_REQUEST['a'])) {
echo "<br>input a: ";
echo $_REQUEST['a'];
}
if (isset($_REQUEST['b'])) {
echo "<br>input b: ";
echo $_REQUEST['b'];
}
?>
<form method="post">
a <input type="text" name="a" autocomplete="off"><br>
b <input type="text" name="b" autocomplete="off"><br>
<input type="submit">
</form>
<p>
Lopp.
</body>
Andmebaasipäring, kus me ise paneme kokku sql where tingimuse vastavalt kasutaja antud väärtustele (siin näites ei ole see turvaliselt tehtud, vaata paari järgmist). http://dijkstra.cs.ttu.ee/~tammet/k6.php
<body>
Algus.
<p>
<?php
$con = mysqli_connect("localhost","st2014","progress","st2014");
if (!$con) {
die('Could not connect: ' . mysqli_connect_error());
}
$username = "tanel";
if (isset($_REQUEST['username'])) {
$username = $_REQUEST['username'];
}
$q="SELECT * from users where username='$username'";
echo "query: $q <br>";
$result = mysqli_query($con,$q);
echo "leitud:<p>";
while($row = mysqli_fetch_array($result)) {
echo $row['username'] . " " . $row['name'];
echo "<br />";
}
mysqli_close($con);
?>
<form method="post">
username <input type="text" name="username" autocomplete="off"><br>
<input type="submit">
</form>
<p>
Lopp.
</body>
Siin paneme juurde esmaselt vajaliku turva (et kasutaja ei saaks sql süntaksit sisestada), selleks on funktsioon mysqli_real_escape_string: http://dijkstra.cs.ttu.ee/~tammet/k8.php
<body>
Algus.
<p>
<?php
$q="";
$c="";
function addcond($con,$fld) {
global $q, $c;
if (isset($_REQUEST[$fld]) && $_REQUEST[$fld]!="") {
if ($c!="") $c=$c . " and ";
$c=$c . " $fld='" . mysqli_real_escape_string($con,$_REQUEST[$fld]) ."' ";
}
}
function main() {
global $q, $c;
$con = mysqli_connect("localhost","st2014","progress","st2014");
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
$q="SELECT * from users where ";
$c="";
addcond($con,"username");
addcond($con,"name");
addcond($con,"sex");
if ($c=="") {
echo "<p>palun sisesta midagi otsinguvaljadele!<p>";
} else {
$q=$q . $c;
echo "query: " . $q . "<br>";
$result = mysqli_query($con,$q);
echo "leitud:<p>";
while($row = mysqli_fetch_array($result)) {
echo $row['username'] . " " . $row['name'];
echo "<br />";
}
}
mysqli_close($con);
}
main();
?>
<form method="post">
username <input type="text" name="username" autocomplete="off"><br>
name <input type="text" name="name" autocomplete="off"><br>
sex <input type="text" name="sex" autocomplete="off"><br>
<input type="submit">
</form>
<p>
Lopp.
</body>