Witam,
Chciałem pobrać informację z bazy danych czy wartość jest równa 0 lub 1
Jeśli mamy 0
if ($rezultat = $conn->query( http://www.php.net/sprintf("SELECT postac FROM users WHERE login='%s'", mysqli_real_escape_string($conn,$username)))) { $postac = $rezultat->num_rows; if($postac == 0) { http://www.php.net/header('Location: index.php?page=stworz'); }
else{ $_SESSION['loggedin'] = $username; http://www.php.net/header('Location: index.php?page=gra'); }
Mowisz o tym
$postac = $rezultat->num_rows;
?
To sprawdz co to zawiera
$postac = $rezultat->num_rows;
var_dump($postac);
exit;
No chyba ze ELSE podpiales pod to
($rezultat = $conn->query(
? No to wtedy zapytanie zwraca blad. No ale ciezko powiedziec jak zamiast calego kodu podajesz wycinki i zgaduj zgadula....
<?php http://www.php.net/global $tytul; http://www.php.net/global $separator; http://www.php.net/global $description; ?> <html> <head> <title><?php http://www.php.net/echo $tytul.$separator.$description;?> </title> <script src="design/js/script.js"></script> <link href="https://fonts.googleapis.com/css?family=EB+Garamond&display=swap" rel="stylesheet"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link href="design/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous"> <link rel="stylesheet" type="text/css" href="../design/css/style.css"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </head> <body> <div class="wrapper"> <?php require_once("temp/header.php"); ?> <div class="layer"> <div class="menu"> <?php require_once("temp/menu.php"); ?> </div> </div> <div class="layer"> <div class="content"> <fieldset> <center><legend>Logowanie</legend></center> <form role="form" method="post"> <div class="form-group"> <label for="username">Nazwa Postaci:</label> <input type="text" class="form-control" id="username" name="login" required /> </div> <div class="form-group"> <label for="password">Wprowadź hasło:</label> <input type="password" class="form-control" id="password" name="haslo" required /> </div> <button type="submit" class="btn btn-outline-success" name="send">Zaloguj się</button> </form> <?php function inject_checker ($conn, $field){ return (http://www.php.net/htmlentities(http://www.php.net/trim(mysqli_real_escape_string($conn, $field)))); } $dbserver = "localhost"; $dbusername = "root"; $dbpassword = ""; $db = "game"; $conn = new mysqli($dbserver, $dbusername, $dbpassword, $db); $conn -> query ('SET NAMES utf8'); $conn -> query ('SET CHARACTER_SET utf8_unicode_ci'); //Sprawdza Połączenie if ($conn->connect_error) { http://www.php.net/die("Połączenie nie udane z bazą dany.".$conn->connect_error); } if(http://www.php.net/isset($_POST['send'])){ $username = inject_checker($conn, http://www.php.net/strtolower($_POST['login'])); $password = inject_checker($conn, $_POST['haslo']); $haslo_hash = password_hash($password, PASSWORD_DEFAULT); if ($rezultat = $conn->query( http://www.php.net/sprintf("SELECT * FROM users WHERE login='%s'", mysqli_real_escape_string($conn,$username)))) { $ilu_userow = $rezultat->num_rows; if($ilu_userow == 1) { $wiersz = $rezultat->fetch_assoc(); if (password_verify($password, $wiersz['haslo'])) { if ($rezultat = $conn->query( http://www.php.net/sprintf("SELECT postac FROM users WHERE login='%s'", mysqli_real_escape_string($conn,$username)))) { $postac = $rezultat->num_rows; if($postac == 0) { http://www.php.net/header('Location: index.php?page=stworz'); }else{ $_SESSION['loggedin'] = $username; http://www.php.net/header('Location: index.php?page=gra'); } } else { ?> <div class="alert alert-danger" role="alert"> <center>Podane dane są błędne!</center> </div> <?php } } } } } ?> </fieldset> </div> </div> <?php require_once("temp/footer.php"); ?> </div> </body> </html>
Zrobiles var_dump jak prosilem?
A sprawdziłeś w dokumentacji co zawiera num_rows? Bo na pewno nie jest to pole z bazy. I czemu tak mieszasz kod obiektowy ze strukturalnym? Zamiast mysqli_real_escape_string użyj bindowania.
po co 2 razy to ?
http://www.php.net/sprintf("SELECT postac FROM users WHERE login='%s'",
<?php function query($host, $user, $pwd, $database, $table) { $mysqli = new mysqli($host, $user, $pwd, $database); if($mysqli->connect_error) { http://www.php.net/exit('Błąd polączenia z MySQL'); } $mysqli->set_charset("utf8"); $loginf = 'qqq2'; // login z forma $haslof = 'qwerty2'; // haslo z forma $stmt = $mysqli->prepare("SELECT id, login, haslo, postac FROM $table WHERE login = ?"); $stmt->bind_param("s", $loginf); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($id, $login, $haslo, $postac); $stmt->fetch(); if($stmt->num_rows === 1) { if (password_verify($haslof, $haslo)) { if($postac == 0) { http://www.php.net/echo 'Postac ma wartosc: ' . $postac; http://www.php.net/exit; http://www.php.net/header('Location: index.php?page=stworz'); }else{ http://www.php.net/echo 'Postac ma wartosc: ' . $postac; http://www.php.net/exit; $_SESSION['loggedin'] = $loginf; http://www.php.net/header('Location: index.php?page=gra'); } } else { http://www.php.net/exit('Błędne hasło'); } } else { http://www.php.net/echo $stmt->num_rows . ' wyników'; } $stmt->close(); } query('localhost', 'root', 'haslo', 'nazwabazy', 'nazwatabeli'); ?>
@dublinka jaki sens ma twoja funkcja? To jest jakis groch z kapusta. Czemu tam sa przekazywane wszystkie dane do polaczenia? Czemu ta funkcja tworzy nowe polaczenie za kazdym razem jak jest wywolywana?
Czemu tam jest jako parametr nazwa tabeli? A jak sie kod rozrosnie w niej i bede zapytania do dwoch tabel? Kolejny parametr dodasz?
Rozumiem chec pomocy ale nie ucz ludzi pisac tak nieoptymalnych rzeczy.
To jest tylko przyklad jak mozna zrobic to na czym koledze zalezy.
Jesli ktos chcialby bardzo zeby to przerabiac dla dalszego kodu to oczywiscie trzeba to pozmieniac. Parametry podalem ot tak jak i nazwe tabeli.
Zalozeniem bylo pokazanie jak dobrac sie do rekordu POSTAC i to bylo najwazniejsze. Reszta to ogolny zarys.
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)