obtain username from databas e

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris93
Forum Newbie
Posts: 10
Joined: Fri Aug 01, 2014 4:43 pm

obtain username from databas e

Post by chris93 »

ok, basically i have 2 forms

form1 will ask the user to enter a username,

then once the user clicks on submit,

it will redirect to form2 and form2 will check in the database for the username, for some reason im getting


Notice: Undefined index: username in C:\xampp2\htdocs\tutorials\form2.php on line 15
SELECT * FROM test WHERE UserName LIKE ''
Warning: mysql_error() expects parameter 1 to be resource, string given in C:\xampp2\htdocs\tutorials\form2.php on line 21

for form2

form1.php:

Code: Select all

<?php
$action=$_REQUEST['action'];
if ($action=="")    /* display the contact form 

$action=="" is an empty string ,
when the user loads the page the first time,
after the user entered data and hit submit button.,
the post request runs , and we go into the else part 

*/
    {
    ?>// try without redirection to another form by setting action=""
    <form  action="form2.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="action" value="submit">
    Your name:<br>
    <input name="username" type="text" value="" size="30"/><br>
    Your email:<br>
    <input name="email" type="text" value="" size="30"/><br>
    Your message:<br>
    <textarea name="message" rows="7" cols="30"></textarea><br>
    <input type="submit" value="Send email"/>
    </form>
    <?php
    } 
else                /* runs after the post request 
 if we did not specify an action the like action"" , 
 we will not redicrect to another page , 
 so after postrequest the else part runs send the submitted data */
    {
    $name=$_REQUEST['username'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($message==""))
        {
        echo "All fields are required, please fill <a href=\"\">the form</a> again.";
        }
    else{        
        $from="From: $name<$email>\r\nReturn-path: $email";
        $subject="Message sent using your contact form";
        mail("youremail@yoursite.com", $subject, $message, $from);
        echo $from ." ";
        echo		$subject ." " ;
        echo 	"Email sent!";
        }
    }  
?>



form2.php

Code: Select all

<?php




$db = 'testdb'; 

$con=mysqli_connect("localhost","root","fireman9");


mysqli_select_db($con,$db) ;



$username = $_POST['username'];
//$password = $_POST['password'];


$query = "SELECT * FROM test WHERE UserName LIKE '$username'";
echo $query;
$result = mysql_query( $query ) or die(mysql_error("error message for the user"));


$row = mysql_fetch_array($result);

		//Return result to jTable
		$jTableResult = array();
		$jTableResult['Result'] = "OK";
		
		// add $row to the array $jTableResult
		$jTableResult['Record'] = $row;
		print json_encode($jTableResult);

//if($result === FALSE) {$
  //  die(mysql_error("error message for the user")); 
//}

while($row = mysql_fetch_array($result))
{
    echo $row['name33'];
	echo $row['id'];
		echo $row['UserName'];
}


?>
Last edited by Celauran on Fri Aug 01, 2014 8:00 pm, edited 1 time in total.
Reason: Please wrap your code in syntax tags.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: obtain username from databas e

Post by Christopher »

When you redirect (I don't see in the code where you are doing that) the values in $_POST are gone. You would need to pass $username as a parameter in the redirect, or save it in a session variable/cookie.
(#10850)
vcan
Forum Newbie
Posts: 1
Joined: Mon Aug 04, 2014 7:02 am

Re: obtain username from databas e

Post by vcan »

Try This
From1.php

Code: Select all

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<?php


if (isset($_POST['submit'])){
	
session_start();	
$_SESSION['username']=$_POST['username'];
header("location:form2.php");
}
?>
<body>
<form id="frm1" name="frm1" method="post" action="">
<input type="text" id="name" name="username">
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
form2.php

Code: Select all

$db = 'testdb'; 

$con=mysqli_connect("localhost","root","fireman9");


mysqli_select_db($con,$db) ;



$username = $_POST['username'];
//$password = $_POST['password'];


$query = "SELECT * FROM test WHERE UserName LIKE '$username'";
echo $query;
$result = mysql_query( $query ) or die(mysql_error("error message for the user"));


$row = mysql_fetch_array($result);

                //Return result to jTable
                $jTableResult = array();
                $jTableResult['Result'] = "OK";
                
                // add $row to the array $jTableResult
                $jTableResult['Record'] = $row;
                print json_encode($jTableResult);

//if($result === FALSE) {$
  //  die(mysql_error("error message for the user")); 
//}

while($row = mysql_fetch_array($result))
{
    echo $row['name33'];
        echo $row['id'];
                echo $row['UserName'];
}


?>
Post Reply