0

I want to display the the respective username and its email from database table (create_acc) to the profile page of individual but every time whenever I'm executing the code it's giving the error of undefined index:uname and same for the email undefined index:email_id.

where is the problem ?

<?php

$a1 = $_GET["uname"];
$a2 = $_GET["email_id"];

$con = mysqli_connect('localhost', 'root', '');
if (!$con)
{
    die("could not connect to the server" . mysqli_error());
}

mysqli_select_db($con, 'forum');



$q = "select * from create_acc where username = '" . $a1 . "' and user_email = '" . $a2 . "'";

$result = mysqli_query($con, $q);
echo '<div class="user">';
while ($row = mysqli_fetch_assoc($result))
{
    echo'<div class="rows">';

    echo $row["username"];
    echo $row["user_email"];

    echo '</div>';
}

echo '</div>';
mysqli_close($con);

?>
Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
monika
  • 51
  • 7

3 Answers3

1

You did a mistake with the database connection.

Try this.

Getting the data from the URL.

$a1 = $_GET["uname"];
$a2 = $_GET["email_id"];

Establish database connection

$con = mysqli_connect('localhost','root','', 'forum');
if(!$con){
  die("could not connect to the server".mysqli_error());
}

Query and the result

$q = "SELECT * FROM `create_acc` where `username`='".$a1."' AND `user_email`='".$a2."'";
$result = mysqli_query($con, $q);
?>
<div class="user">
<?php while($row = mysqli_fetch_object($result)){?>
    <div class="rows">
    <?php
    echo $row->username;
    echo $row->user_email;
    ?>
  </div>
    <?php }?>
</div>
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
  • 1
    thanx for ur help -frayne – monika Apr 06 '16 at 16:33
  • @monika Is my answer not useful to you? Is the accepted answer is more useful then my answer? – Murad Hasan Apr 06 '16 at 16:36
  • itz not just about the acceptance of the answer ... actually i appreciate the valuable time given by you and the other users as well ... nd im new to this site so i gave the acceptance tick to everybody who is present here .. i thought that this is for the appreciation but now i gt to knw that this can be given only to one person :D – monika Apr 06 '16 at 16:53
  • This is not the way, if you got more points then you can vote them, now you are not able to do that. – Murad Hasan Apr 06 '16 at 16:54
0

Ensure that your method attribute correspond to the predefined variable you're calling. For example in your form

<form action=".php" method="get">    
    Username:<input type="text" name="uname" value=""><br />
    Email:<input type="text" name=email_id" value="">
</form>

And make sure the name parameters of the input matches the $_GET index

Not to make mistake you can use this in your php file

if(isset($_SERVER["REQUEST_METHOD"] == "POST")) {
    foreach($_POST as $key => $value) {
       item[$key] = $value;
    }
} elseif(isset($_SERVER["REQUEST_METHOD"] == "GET") {
    foreach($_GET as $key => $value) {
        item[$key] = $value;
    }
}
$username = item["uname"];
$email = item["email_id"];
Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
0

please replace your mysqli_select_db($con,'forum'); with my code mysqli_select_db($con,"forum"); and then $result = $con->query($q);

Shadow
  • 244
  • 1
  • 3
  • 23