obtain username from databas e
Posted: Fri Aug 01, 2014 4:52 pm
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:
form2.php
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'];
}
?>