PHP MYSQLI number of rows doesnt work no errors -


i have login form when user clicks login checklogin.php called , should check username , password matches record on database if true else print wrong password or username

so far wrong password username though correct username&&password. have made somechanges no echo, printf or error

how can fix issue?

form

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#cccccc"> <tr> <form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#ffffff"> <tr> <td colspan="3"><strong>member login </strong></td> </tr> <tr> <td width="78">username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"></td> </tr> <tr> <td>password</td> <td>:</td> <td><input name="mypassword" type="text" id="mypassword"></td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> <td><input type="submit" name="submit" value="login"></td> </tr> </table> </td> </form> </tr> </table> 

checklogin.php

<?php $mysqli = new mysqli('localhost', 'root', 'password', 'aiesec'); /* check connection */ if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } // username , password sent form $myusername=$_post['myusername']; $mypassword=$_post['mypassword']; // protect mysql injection (more detail mysql injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysqli_real_escape_string($myusername); $mypassword = mysqli_real_escape_string($mypassword); // if result matched $myusername , $mypassword, table row must 1 row $sql = "select * members username='$myusername' , password='$mypassword"; if($result = mysqli->query($sql, mysqli_use_result)) { printf("errormessage: %s\n", $mysqli->error); echo $result->num_rows; //zero while($row = $result->fetch_row()) { printf("errormessage: %s\n", $mysqli->error); echo $result->num_rows; //incrementing 1 each time } echo $result->num_rows; // total count } if($row==1){ echo "correct username , pass"; // register $myusername, $mypassword , redirect file "login_success.php" // session_register("myusername"); //session_register("mypassword"); //header("location:login_success.php"); } else { echo "wrong username or password"; } mysqli_close(); ?> 

i have tried

$sql="select * $tbl_name username='$myusername' , password='$mypassword'"; $result=mysqli_query($sql); // mysql_num_row counting table row $count=mysqli_num_rows($result); 

to sure see php errors, add code on top of script:

error_reporting(e_all); ini_set('display_errors', 1); 

you must correct calls mysqli_real_escape_string. according documentation, there must 2 parameters, , first parameter must mysql link. in case link $mysqli.

also, replace:

if($row==1){ 

with:

if($result->num_row==1){ 

you misunderstanding $result->num_rows is: contains total number of rows returned query result stored in $result. so, useless check value of $result->num_rows inside loop retrieve records returned query.

i removed constant mysqli_use_result query() because documentation mysqli_query says:
if use mysqli_use_result subsequent calls return error commands out of sync unless call mysqli_free_result().

new code:

<?php $mysqli = new mysqli('localhost', 'root', 'password', 'aiesec'); /* check connection */ if (mysqli_connect_errno()) { printf("connect failed: %s\n", mysqli_connect_error()); exit(); } // cleanup post variables $myusername = mysqli_real_escape_string($mysqli, stripslashes(trim($_post['myusername']))); $mypassword = mysqli_real_escape_string($mysqli, stripslashes(trim($_post['mypassword']))); // if result matched $myusername , $mypassword, table row must 1 row $sql = "select * members username='$myusername' , password='$mypassword'"; $result = mysqli->query($sql); if($mysqli->errno<>0) die("errormessage: %s\n", $mysqli->error); echo $result->num_rows; if($result->num_rows==1){ echo "correct username , pass"; // register $myusername, $mypassword , redirect file "login_success.php" // session_register("myusername"); //session_register("mypassword"); //header("location:login_success.php"); } else { echo "wrong username or password"; } mysqli_close(); ?> 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -