Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Sprawdzanie poprawności danych w Mysql i wyświetlanie różnych formularzy
Cherob
post
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 10.06.2011

Ostrzeżenie: (0%)
-----


Cześć chcę aby po przekazaniu zmiennej URl index.php?id=użytkownik wyświetlały się dwa różne formularze w zależności od podania prawidłowej nazwy użytkownika i nie. Możecie mi powiedzieć co tu nie gra w tym skrypcie i jak to naprawić.
  1. <?php
  2.  
  3. $id=$_POST['id'];
  4.  
  5. $host=""; // Host name
  6. $username=""; // Mysql username
  7. $password=""; // Mysql password
  8. $db_name=""; // Database name
  9. $tbl_name="yet"; // Table name
  10.  
  11. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  12. mysql_select_db("$db_name")or die("cannot select DB");
  13.  
  14. $sql="SELECT * FROM $tbl_name WHERE username='$id'";
  15. $result=mysql_query($sql);
  16.  
  17.  
  18. $count=mysql_num_rows($result);
  19.  
  20.  
  21. if($count==1){
  22. <form action="rejestruj.php" method="post"
  23. <tr>
  24. <th>Email: </th>
  25. <td><input name="fname" type="text" valu="" /></td>
  26. </tr>
  27. <input type="submit" name="Wyślij"></form>';
  28. }
  29. else {
  30. <form action="index.php" method="post"
  31. <tr>
  32. <th>Wprowadź nazwę użytkownika: </th>
  33. <td><input name="fname" type="text" valu="" /></td>
  34. </tr>
  35. <input type="submit" name="Wyślij"></form>';
  36. }
  37. ?>


Ten post edytował Cherob 25.07.2011, 16:17:42
Go to the top of the page
+Quote Post
lukasz91
post
Post #2





Grupa: Zarejestrowani
Postów: 149
Pomógł: 7
Dołączył: 6.01.2010
Skąd: Opole Lubelskie

Ostrzeżenie: (0%)
-----


Zapewne błąd w zapytaniu. Daj:
  1. if(!$result) {
  2. }


--------------------
Go to the top of the page
+Quote Post
Cherob
post
Post #3





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 10.06.2011

Ostrzeżenie: (0%)
-----


Czemu gdy przez formularz wysyłam dane wszystko działa a gdy próbuję przez przeglądarkę wpisując checklogin.php?myusername=john już nie

  1. <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  2. <tr>
  3. <form name="form1" method="post" action="checklogin.php">
  4. <td>
  5. <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
  6. <tr>
  7. <td colspan="3"><strong>Member Login </strong></td>
  8. </tr>
  9. <tr>
  10. <td width="78">Username</td>
  11. <td width="6">:</td>
  12. <td width="294"><input name="myusername" type="text" id="myusername"></td>
  13. </tr>
  14. <tr>
  15. <td>&nbsp;</td>
  16. <td>&nbsp;</td>
  17. <td><input type="submit" name="Submit" value="Login"></td>
  18. </tr>
  19. </td>
  20. </form>
  21. </tr>
  22. </table>

checklogin.php
  1. <?php
  2. $host="mysql.cba.pl"; // Host name
  3. $username="gebit"; // Mysql username
  4. $password="55845555"; // Mysql password
  5. $db_name="donaukiphp_cba_pl"; // Database name
  6. $tbl_name="yet"; // Table name
  7.  
  8.  
  9. $zmienna1='<form action="index.php" method="post"
  10. <tr>
  11. <th>Wprowadź urzytkownika: </th>
  12. <td><input name="fname" type="text" valu="" /></td>
  13. </tr>
  14. <input type="submit" name="Wyślij"></form>';
  15.  
  16. $zmienna2='<form action="index.php" method="post"
  17. <tr>
  18. <th>Wprowadź nazwę urzytkownika: </th>
  19. <td><input name="fname" type="text" valu="" /></td>
  20. </tr>
  21. <input type="submit" name="Wyślij"></form>';
  22.  
  23.  
  24. // Connect to server and select databse.
  25. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  26. mysql_select_db("$db_name")or die("cannot select DB");
  27.  
  28. // Define $myusername and $mypassword
  29. $myusername=$_POST['myusername'];
  30.  
  31. // To protect MySQL injection (more detail about MySQL injection)
  32. $myusername = stripslashes($myusername);
  33. $myusername = mysql_real_escape_string($myusername);
  34.  
  35.  
  36. $sql="SELECT * FROM $tbl_name WHERE username='$myusername'";
  37. $result=mysql_query($sql);
  38.  
  39. // Mysql_num_row is counting table row
  40. $count=mysql_num_rows($result);
  41. // If result matched $myusername and $mypassword, table row must be 1 row
  42.  
  43. if($count==1){
  44.  
  45. session_register("myusername");
  46. echo "$zmienna1";
  47. }
  48. else {
  49. echo "$zmienna2";
  50. }
  51.  
  52. ?>
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Bo w kodzie dane odbierasz z $_POST,a to co wstawiasz w linku to $_GET.

ps: przenosze


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 20:34