-1
<?php
require('includes/core.inc.php');
require('includes/database/connect.db.php');
session_start();

if(isset($_POST['Register'])) {

    $username=$_POST['Username'];
    $password=$_POST['Password'];
    $query= "INSERT into users (Username,Password) VALUES ('$username','$password')";
    $res = mysql_query($query);
    if($res){
        header("Location:index.php");
    }     
}
?>

I am facing problems while inserting data in the database don't know why the query is correct this problem is while selecting from database

Notice: Undefined index: Username in C:\xamp\htdocs\chatbox\index.php on line 4

<?php
require('includes/core.inc.php');
session_start();
echo "Welcome".$_SESSION['Username'];
if(isset($_POST['send'])){
    if(send_msg($_POST['sender'],$_POST['message'])){
        //echo "Message sent ...";
    }else{
        //echo "failed to sent ";
    }
}   
?>
Qirel
  • 25,449
  • 7
  • 45
  • 62

1 Answers1

1

try this remember you have to set session variable before using it.

<?php
require('includes/core.inc.php');
require('includes/database/connect.db.php');
session_start();

if(isset($_POST['Register'])) {

    $username=$_POST['Username'];
    $password=$_POST['Password'];

$_SESSION['Username'] = $username;

    $query= "INSERT into users (Username,Password) VALUES ('$username','$password')";
    $res = mysql_query($query);
    if($res){
        header("Location:index.php");
    }     
}
?>

also to get rid of undefine index issue you should always use

echo "Welcome".isset($_SESSION['Username'])?$_SESSION['Username']:"";

hope your issue will get resolved.

Mukesh Rawat
  • 894
  • 2
  • 8
  • 27