php - How can I get the field id for a session? -
i new @ php , followed youtube video's build littel project.
i have table of users:
id username email password
and i'm trying build private message system. want id
of user logged in. when write my_id = $_session[ 'id' ]
error : "undefined index: id"
when write $my_id = $_session['username']
username no error , echo's me username. difference?
that code:
<?php session_start(); $db = mysqli_connect( "localhost", "root", "", "travelersdb" ); if( @$_session[ "username" ] ) { ?> <html> <head> <title>home page</title> </head> <?php include( "header.php" ); echo "<center><h1>private message system</h1>"; include( "message_title_bar.php" ); if( isset( $_get[ "user" ] ) && !empty( $_get[ "user" ] ) ) { ?> <form method = 'post' > <?php if( isset( $_post[ "message"] ) && !empty( $_post[ "message" ] ) ) { $user=$_get['user']; $my_id = $_session['username']; // ------> doesn't work when changed 'id' $random_number = rand(); $sql_m = "select 'hash' message_group ( user_one = '" . $my_id . "' , user_two = '" . $user . "' ) or ( user_one = '" . $user . "' , user_two = '" . $my_id . "' )"; $check_con = mysqli_query( $db, $sql_m ); $rows = mysqli_num_rows( $check_con ); if( $rows == 1 ) { echo "<p>conversation started!</p>"; } else { echo $user . "</br>"; echo $my_id; -------> wanted echo $my_id check... echo $random_number; // $sql_in = "insert message_group( user_one, user_two, hash ) // values( '1111', '2222', '2222' )"; // mysqli_query( $db, $sql_in ); echo "<p>conversation started</p>"; } } ?> enter message : </br> <textarea name = 'message' rows = '7' cols = '60'> </textarea> <br></br> <input type='submit' value="send message" /> </form> <?php } else { echo "<b>select user</b>"; $sql = "select id,username users"; $check = mysqli_query($db,$sql); while ( $run_user = mysqli_fetch_array( $check ) ) { $user = $run_user[ 'id' ]; $username = $run_user[ 'username' ]; echo "<p><a href = 'send.php?user=$user'>$username</a></p>"; } } ?> </html> <?php } else { echo "you must logged in."; } ?>
update: done still doesn't work. doesn't identify id. login.php:
<?php session_start(); //connect database $db=mysqli_connect("localhost","root","","travelersdb"); if(isset($_post['login_btn'])) { //$username=mysql_real_escape_string($_post['username']); //$password=mysql_real_escape_string($_post['password']); $username = mysqli_real_escape_string($db, $_post['username']); $password = mysqli_real_escape_string($db, $_post['password']); $password=md5($password); //remember hashed password before storing last time $sql="select * users username='$username' , password='$password'"; $result=mysqli_query($db,$sql); if(mysqli_num_rows($result)==1) { $_session['message']="you loggged in"; $_session['username']=$username; $sql_t = "select id users username='$username' , password='$password'"; $id = mysqli_query($db, $sql_t); $_session['id']=$id; header("location:index.php"); } else { $_session['message']="username , password combiation incorrect"; } }
?>
you may haven't set session. use print_r($_session);
, check whether index -> id
there or not.
Comments
Post a Comment