I am currently creating an interface for users to enter details in regarding their pets. I am using two php pages, one with a html form in which allows the users to enter the data and the other one to display the data. I have currently encountered two problems,
one being that when the "submit" button on the HTML form is clicked, it does not divert to the second page.
The second one being that the data which is being entered onto the first page through text boxes is not being posted to the next page for the users to get an overview.
I am very new to php, I will display all of the code as I feel that there are many errors. The code is not too overwhelming and I hope someone can help with these issues, thanks.
p.s. the first section of code is the page where users can enter the data, and the second section is the second page.
<?php
echo '<link href="style.css" rel="stylesheet">';
session_start();
$fname = $_POST["fname"];
$petname = $_POST["petname"];
$pettype = $_POST["pettype"];
$pdob = $_POST["pdob"];
$appdate = $_POST["appdate"];
$apptime = $_POST["apptime"];
$reason = $_POST["reason"];
$validated = false;
$_SESSION['login'] = "";
if ($result > 0) $validated = true;
if($validated)
{//Divert to the protected page to display information after.
$_SESSION['login'] = "OK";
$_SESSION['fname'] = $fname;
$_SESSION['petname'] = $petname;
$_SESSION['pettype'] = $pettype;
$_SESSION['pdob'] = $pdob;
$_SESSION['appdate'] = $appdate;
$_SESSION['apptime'] = $apptime;
$_SESSION['reason'] = $reason;
header('Location: protected.php');
}
$_SESSION['login'] = "";
?>
<html>
<body>
<div align="center">
<h1 class="ribbon">
<strong class="ribbon-content">Veterinary Appointment Form</strong>
</h1>
<p><i>Please enter information reguarding your pet</i></p>
<form action="protected.php"
method="post">
<table>
<tr>
<td
align="right">Full Name* : </td>
<td><input size=\"20\"
type="text" size="20" maxlength="50"
name="fname"></td>
</tr>
<tr>
<td
align="right">Name of Pet* : </td>
<td><input size=\"20\"
type="text" size="20"
maxlength="50" name="petname"></td>
</tr>
<tr>
<td
align="right">Pet Type* : </td>
<td><input size=\"20\"
type="text" size="20"
maxlength="50" name="pettype"></td>
</tr>
<tr>
<td
align="right">Pet D.O.B : </td>
<td><input size=\"20\"
type="text" size="20"
maxlength="50" name="petdob"></td>
</tr>
<tr>
<td
align="right">Date of Appointment* : </td>
<td><input size=\"20\"
type="text" size="20"
maxlength="50" name="appdate"></td>
</tr>
<tr>
<td
align="right">Time of Appointment* : </td>
<td><input size=\"20\"
type="text" size="40"
maxlength="50" name="apptime"></td>
</tr>
<tr>
<td
align="right">Why Appoitnment is Needed : </td>
<td><input size=\"20\"
type="text" size="20"
maxlength="50" name="reason"></td>
</tr>
<tr>
<td> </td>
<td colspan="2"
align="left"><input type="submit"
value="Login"></td>
</div> </tr>
<br>
</table>
</form>
</body>
<img src="doglogo.png" alt="Logo" style="width:150;height:130;">
</html>
<?php
echo '<link href="style.css" rel="stylesheet">';
{
header('Location: ManageUser.php');
exit();
}
?>
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<div align="center">
<h1 class="ribbon">
<strong class="ribbon-content">Veterinary Appointment Form</strong>
</h1>
<p><i>Here are your details</i></p>
<?php
echo "<p>Full Name: ";
echo $_SESSION['fname'];
echo "<br/>";
echo "<p>Name of Pet: ";
echo $_SESSION['petname'];
echo "<br/>";
echo "<p>Pet Type: ";
echo $_SESSION['pettype'];
echo "<br/>";
echo "<p>Pet D.O.B: ";
echo $_SESSION['pdob'];
echo "<br/>";
echo "<p>Date of Appointment: ";
echo $_SESSION['appdate'];
echo "<br/>";
echo "<p>Time of Appointment: ";
echo $_SESSION['apptime'];
echo "<br/>";
echo "<p>Why Appoitnment is Needed: ";
echo $_SESSION['reason'];
echo "<br/>";
echo "</p>";
{
echo "<p><a href='ManageUser.php'><i>Click here to return</i></a></p>";
}
?>
Print this page
<br>
<img src="doglogo.png" alt="Logo" style="width:150;height:130;">
</div>
</body>
</html>
This will always exit the script.
{
header('Location: ManageUser.php');
exit();
}
Maybe you wanted to do something like this :
if (something) // if the user didn't fill the form
{
header('Location: ManageUser.php');
exit();
}
Also, be careful with :
echo '<link href="style.css" rel="stylesheet">';
This will display the tag before <html>, you may want to place it inside the <head> tag.
You also forgot to add session_start() on the second page.
To summarize :
<?php
session_start();
if (something)
{
header('Location: ManageUser.php');
exit();
}
?>
<html>
<head>
<title>Welcome!</title>
<link href="style.css" rel="stylesheet">
</head>
Bonus : this code
<?php
echo "<p>Full Name: ";
echo $_SESSION['fname'];
echo "<br/>";
echo "<p>Name of Pet: ";
echo $_SESSION['petname'];
echo "<br/>";
echo "<p>Pet Type: ";
echo $_SESSION['pettype'];
echo "<br/>";
echo "<p>Pet D.O.B: ";
echo $_SESSION['pdob'];
echo "<br/>";
echo "<p>Date of Appointment: ";
echo $_SESSION['appdate'];
echo "<br/>";
echo "<p>Time of Appointment: ";
echo $_SESSION['apptime'];
echo "<br/>";
echo "<p>Why Appoitnment is Needed: ";
echo $_SESSION['reason'];
echo "<br/>";
echo "</p>";
{
echo "<p><a href='ManageUser.php'><i>Click here to return</i></a></p>";
}
?>
can be replaced by :
<p>Full Name: <?=$_SESSION['fname']?></p>
<br/>
<p>Name of Pet: <?=$_SESSION['petname']?></p>
<br/>
<p>Pet Type: <?=$_SESSION['pettype']?></p>
<br/>
<p>Pet D.O.B: <?=$_SESSION['pdob']?></p>
<br/>
<p>Date of Appointment: <?=$_SESSION['appdate']?></p>
<br/>
<p>Time of Appointment: <?=$_SESSION['apptime']?></p>
<br/>
<p>Why Appoitnment is Needed: <?=$_SESSION['reason']?></p>
<br/>
<p><a href='ManageUser.php'><i>Click here to return</i></a></p>
Be sure to check the rendered code with the W3C Validation tool. This will check if the HTML code is correct and tell you what is wrong.
I think the the problem is here:
if ($result > 0)
where did you define $result ?
Try to do check print_r($_POST); in protected.php and check you if condition.
Related
please help me solve this problem. I want to access content of a page only when Session is activated, else redirect user to activate session first. But when I redirect user to session page, it is stuck and cannot go back to content page. I am new here so please help me out from this problem.
<?php
session_start();
if(!isset($_SESSION['username'])){
echo "cookie is not activated" ;
header('Location: http://localhost/CC/Loginsession.php');
die;
}
else {
?>
<!doctype html>
<html>
<head>
<title>Update in PHP</title>
</head>
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$conn=mysql_connect($servername,$username,$password);
if(!$conn ) {
die('Could not connect: ' . mysql_error());
}
$sq1 = 'select * from biodata';
mysql_select_db('firstdb');
$display=mysql_query($sq1,$conn);
if(!$display ) {
die('Could not get data: ' . mysql_error());
exit;
}
if (mysql_num_rows($display) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>Fname</th>
<th>Lname</th>
<th>Email</th>
<th>Phone</th>
<th>Message</th>
<th>Update</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $display ) ){
echo
"<form method= 'post' />
<tr>
<td ><input name='UID' value='{$row['ID']}' readonly/></td>
<td ><input name='upfname' value='{$row['fname']}' /></td>
<td ><input name='uplname' value='{$row['lname']}' /></td>
<td ><input name='upemail' value='{$row['email']}' /></td>
<td ><input name='upphone' value='{$row['phone']}' /></td>
<td ><input name='upmessage' value='{$row['message']}' /></td>
<td><input type='Submit' name='update' value='Update' id='".$row["ID"]."' </td>
</tr>
</form>";
}
?>
</tbody>
</table>
<?php
if(isset($_REQUEST['update']))
{
$id = $_REQUEST['UID'];
$upfn = $_REQUEST['upfname'];
$upln = $_REQUEST['uplname'];
$upem = $_REQUEST['upemail'];
$upph = $_REQUEST['upphone'];
$upms = $_REQUEST['upmessage'];
$up="UPDATE biodata
SET
fname='$upfn',
lname='$upln',
email='$upem',
phone='$upph',
message='$upms'
WHERE ID = $id";
$updbb=mysql_query($up,$conn);
if($updbb){
header('Location: http://localhost/Prac/updateinsamepage.php');
}
}
}
?>
</body>
</html>>
and My session Login form code is here
<?php
session_start();
if(isset($_SESSION['username'])){
echo "Already registered as $_SESSION[username]" ;
}
else if($_SERVER['REQUEST_METHOD'] == 'POST'){
$uname=htmlentities($_POST['username']);
$pass=htmlentities($_POST['password']);
if(!empty($uname) && !empty($pass)) {
$_SESSION['username']=$uname;
echo "Thanks<br />" . "UserName: $uname " . "Password: $pass";
}
else{
echo "Please fill out the both fields";
}
}
else {
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Loginsession</title>
</head>
<body>
<form method="post">
Username:<input type="text" id="username" name="username" /> <br /><br />
Password:<input type="password" id="password" name="password"/><br /><br />
<input type="hidden" name="hiddenvalue" value="<?php http://localhost/CC/Loginsession.php?username=overwritten ?>"/>
<input type="Submit" value="Login" name="Submit" id="submit" />
</form>
<?php }?>
<?php
session_unset();
session_destroy();
?>
</body>
</html>
You can try this script in place of header
echo '<script>window.location="localhost/CC/Loginsession.php"</script>';
I'm fetching the data from database for a particular record using email id in the hidden field.
If I try to add as input type="text" it is not working for me, can any one check this.
Here is my code.
Index.php:
<?php
session_start();
$username = $_SESSION['username'];
if($username) {
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
} else {
echo "no";
}
?>
<html>
<body>
<table class="table table-hover">
<form action="personalinfo.php" METHOD="POST">
<input type="hidden" value="<?php echo $username;?>" name="email">
<tr>
<th><label for="first_name">Name</label></th>
</tr>
<?php
include "personalinfo.php";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
<input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
echo "</tr>";
}
echo "</table>";
?>
</form>
</body>
</html>
personalinfo.php :
<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$result = mysql_query("SELECT * FROM registered where email='$username'");
?>
In index.php, if I use like this in while condition 'first_name';?> name="first_name"/> so that I can update the record but if i remove this
echo "<td>" . $row['first_name'] . "</td>";
and if I add like this instead of that echo
<?php echo $row->first_name;?>
it gives error as
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\index\index.php on line 44
You have an error in your syntax try using this code
<?php
session_start();
?>
<?php
$username = $_SESSION['username'];
if($username) {
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
} else {
echo "no";
}
?>
<html>
<body>
<table class="table table-hover">
<form action="personalinfo.php" METHOD="POST">
<input type="hidden" value="<?php echo $username;?>" name="email">
<tr>
<th><label for="first_name">Name</label></th>
</tr>
<tr>
<?php
include "personalinfo.php";
while($row = mysql_fetch_array($result)) {
?>
<tr>
<input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
</tr>
<?php }?>
</table>
</form>
</body>
</html>
You are getting error because there's a HTML tag in php code.
This line has issue, as it was written in PHP code:
<input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
Try replacing the code with this :
<?php
include "personalinfo.php";
while($row = mysql_fetch_array($result)) {
?>
<tr>
<input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
<!-- //or this can be used <input type="text" name="first_name" value="<?php echo $row->first_name;?>" />
-->
</tr>
<?php
}
?>
</table>
I am working on a project of PHP. I have a strange error. My PHP project is working well on local server (WAMP server). But after hosting it on live server, it is not working.
Problem: Session variable created on Login.php page is not passing value on Report.php page. The code is as below:
Login.php
<?php
$con=mysql_connect("mysql51****************","username","password")or die(mysql_error());
$select_db=mysql_select_db("database_name",$con);
$error="";
if(isset($_POST['submit']))
{
$userid=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$sql="SELECT * FROM user_details WHERE user_name='{$userid}' AND password='{$password}' ";
$result = mysql_query($sql);
if(mysql_num_rows($result) <= 0)
{
$error="Invalid UserId or Password.";
}
else
{
//session_set_cookie_params(60*60*60, '/', '.abcxyz.com');
ini_set('session.cookie_domain','.abcxyz.com');
session_start();
//$userid=mysql_real_escape_string($_POST['abc']);
//echo($userid);
$_SESSION['user']=$userid;
$_SESSION['userid']="true";
//echo $_SESSION['userid'];
$error="Successfully Login";
//header("Location: admin.php?page=report");
/*echo '<script type="text/javascript">alert("header is not working.' . $_SESSION['user'] . '");</script>';
*/ ?>
<script type="text/javascript">
/*alert("Please select a Source And Destination Country"); */
window.location.href='admin.php?page=report';
</script>
<?php
}
}
?>
<br />
<h1 style="text-align:center">Login</h1>
<form class="login" action="login.php" method="post" name="form1" id="form1">
<p>Username:</p>
<input class="login-input" type="text" name="username" value=""/>
<p> Password : </p>
<input class="login-input" type="password" name="password" value=""/>
<p>
<p style="color:#F00; font-size:12px; font-weight:100"><?php echo htmlentities($error); ?> </p>
<input class="login-submit" type="submit" name="submit" value="Login"/>
</form>
Report.php
<?php
include("connections/Connections.php");
/*$con=mysql_connect("localhost","admin","")or die(mysql_error());
$select_db=mysql_select_db("test",$con);*/
/*session_start();
if(session_is_registered('test'))
{
echo"registered";
}
else
{
header("Location: login.php");
}*/
//$user=$_SESSION['userid'];
$user=$_SESSION['userid'];
echo '<script type="text/javascript">alert("header is not working.' .$user . '");</script>';
if($user=="true")
{
//echo " Logining Successfully.";
}
else
{
?>
<script type="text/javascript">
//alert("Please select a Source And Destination Country");
window.location.href='login.php';
</script>
<?php
//header("Location: login.php");
}
?>
<?php
/*session_start();
$user=$_SESSION['userid'];
echo($user);
if($user=="Admin")
{
echo " Logining Successfully.";*/
$sql="SELECT * FROM register ";
if(isset($_POST['btnfilter']))
{
$search_term=mysql_real_escape_string($_POST['search_text']);
$answer = $_POST['filter'];
if ($answer == "ID") {
$sql .="WHERE id= '{$search_term}' ";
}
elseif ($answer == "Name") {
$sql .="WHERE fullname Like '%{$search_term}%'";
}
elseif ($answer == "DOB") {
$sql .="WHERE dob Like '%{$search_term}%' ";
}
elseif ($answer == "Occupation") {
$sql .="WHERE occupation Like '%{$search_term}%' ";
}
else
{
echo("Pealse Enter a valid value");
}
}
elseif(isset($_POST['btnrmfilter']))
{
$sql="SELECT * FROM register";
}
$query=mysql_query($sql) or die(mysql_errno());
//}
/*else
{
header('Location: /login.php');
}*/
?>
<style type="text/css">
table
{
font-size:12px;border-bottom:1px solid #ccc;
border-left:1px solid #ccc}
td
{
padding:5px 3px;
border-top:1px solid #ccc;
border-right:1px solid #ccc}
</style>
<div class="content">
<h1>Data Reading From database.</h1>
<form id="search_form" method="post" action="">
<div class="radio">
<input type="radio" name="filter" value="ID" />ID
<input type="radio" name="filter" value="Name" />Name
<input type="radio" name="filter" value="DOB"/>DOB
<input type="radio" name="filter" value="Occupation"/>Occupation<br />
</div>
<div class="input" >
<input type="text" name="search_text" value=""/>
<input type="submit" value="FilterData" name="btnfilter" />
<input type="submit" value="RemoveFilter" name="btnrmfilter" />
</div>
</form>
<table border="0" cellpadding="0" cellspacing="0">
<colgroup>
<col width="2%" style="color:#f60" valign="middle" align="center" >
<col width="12%" >
<col width="8%" >
<col width="5%" align="center" >
<col width="10%" >
</colgroup>
<tr style="background:#eee; height:30px;">
<td>ID</td>
<td>Name</td>
<td>DOB</td>
<td>Nationality</td>
<td>Mobile No</td>
<td>Phone No</td>
<td>Email</td>
<td>Education</td>
<td>Occupation</td>
<td>Comment</td>
<td >Noofexp</td>
</tr>
<?php while($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['dob']; ?></td>
<td><?php echo $row['nationality']; ?></td>
<td><?php echo $row['mobno']; ?></td>
<td><?php echo $row['phno']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['education']; ?></td>
<td><?php echo $row['occupation']; ?></td>
<td><?php echo $row['comment']; ?></td>
<td><?php echo $row['noofexp']; ?></td>
</tr>
<?php } ?>
</table>
<h2> </h2>
<h3>Thanks for view.</h3>
<!-- end .content --></div>
<div class="footer">
<p>Footer</p>
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>
Edit:
As the answers suggested, i edited as below:
Login.php:
<?php
session_start();
$con=mysql_connect("mysql51*************","username","password")or die(mysql_error());...
...
Report.php:
<?php
session_start();
include("connections/Connections.php");...
...
But still i am not getting the session variable value in Report.php.
session_start(); should be placed on top of the both pages.
have you checked your phpinfo() ?
make shure you have
Session Support - enabled
You must add session_start(); in your report.php page
If it runs on your WAMP successfully, It is very possible that the problem is not from your code but from your hosting company.
Something like this happened to me before.
Just call you hosting company and tell them to enable session on their server for you and they will it for you.
and always start session first before any script just as you did above.
session_start();
Enjoy
Go try adding this on your code:
if(!$con)
{
die('Could not connect:'. mysql_error());
}
and see what the error is.
very easy solve to this problem copy this code
error_reporting(0);
session_start();
Note: give space from left side of session_start(); if you want understand about this watch this video
I am trying to create a Registration System using only PHP. This is an example of that. But I think I have done something wrong. I tried to find similar solution in StackOverFlow posts but didn't get any exact solution. It would be really get if someone would help me to find the error in my code below.
<?php
// POST HANDLER -->
if(isset($_POST['registerForm']))
{
conFunc(); // Connection Function
$userid = $_POST['userid'];
$name = $_POST['name'];
$getUserId = mysql_query("SELECT * FROM `user` WHERE `userid` = '".$userid."'");
$id = mysql_fetch_array($getUserId);
if($id)
{
echo "This User ID is Already Available on the System, Please Choose Something Else!";
}
else
{
$query = mysql_query("INSERT INTO `user` (`userid`, `name`");
if($query)
{
echo "A New User is Registered Successfully:<br /><br />";
echo "<b>User ID:</b> " . $userid . "<br />";
echo "<b>User Name:</b> " . $name . "<br />";
}
else
{
echo "There is an Error while Saving: " . mysql_error();
echo "<br />Please click on Create User from menu, and try again<br /><br />.";
}
}
exit;
}
// POST HANDLER -->
?>
<!-- FORM GOES BELOW -->
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="registerForm">
<table style="width: 100%">
<tr>
<td>User ID</td>
<td><input name="userid" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td>Name</td>
<td><input name="name" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td></td>
<td><input style="width: 130px; height: 30px" type="submit" name="submit" value="Register Now" /><br /></td>
</tr>
</table>
</form>
You have to check the submit button is set or not.
if(isset($_POST['registerForm']))
should be
if(isset($_POST['submit'])) {
// your php code
} else {
// your html code
}
registerform element is not treated as post element so check with submit button.
Try following code :
<?php
// POST HANDLER -->
if(isset($_POST['submit'])){
conFunc(); // Connection Function
$userid = $_POST['userid'];
$name = $_POST['name'];
$getUserId = mysql_query("SELECT * FROM `user` WHERE `userid` = '".$userid."'");
$id = mysql_fetch_array($getUserId);
if($id)
{
echo "This User ID is Already Available on the System, Please Choose Something Else!";
}
else
{
$query = mysql_query("INSERT INTO `user` (`userid`, `name`");
if($query)
{
echo "A New User is Registered Successfully:<br /><br />";
echo "<b>User ID:</b> " . $userid . "<br />";
echo "<b>User Name:</b> " . $name . "<br />";
}
else
{
echo "There is an Error while Saving: " . mysql_error();
echo "<br />Please click on Create User from menu, and try again<br /><br />.";
}
}
exit;
}else{
// POST HANDLER -->
?>
<!-- FORM GOES BELOW -->
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="registerForm">
<table style="width: 100%">
<tr>
<td>User ID</td>
<td><input name="userid" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td>Name</td>
<td><input name="name" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td></td>
<td><input style="width: 130px; height: 30px" type="submit" name="submit" value="Register Now" /><br /></td>
</tr>
</table>
</form>
<?php } ?>
Your script should be like
$getUserId = mysql_query("SELECT id FROM `user` WHERE `userid` = '".$userid."'");
because you are getting all the results and you need to retrive the id only and your form action should be your same page itself
you need to read about mysql queries http://php.net/manual/en/book.mysql.php
also check your insert query not any data values inserted.
<?php if(isset($_POST['submit']))
{
conFunc(); // Connection Function
$userid = $_POST['userid'];
$name = $_POST['name'];
$getUserId = mysql_query("SELECT * FROM user WHERE userid = '".$userid."'");
$id = mysql_fetch_array($getUserId);
if($id)
{
echo "This User ID is Already Available on the System, Please Choose Something Else!";
}
else
{
$query = mysql_query("INSERT INTO user(userid, name) values('" .$userid . "','" . $name . "')";
if($query)
{
echo "A New User is Registered Successfully:<br /><br />";
echo "<b>User ID:</b> " . $userid . "<br />";
echo "<b>User Name:</b> " . $name . "<br />";
}
else
{
echo "There is an Error while Saving: " . mysql_error();
echo "<br />Please click on Create User from menu, and try again<br /><br />.";
}
}
exit;
}
// POST HANDLER -->
?>
<!-- FORM GOES BELOW -->
<form action="" method="post" name="registerForm">
<table style="width: 100%">
<tr>
<td>User ID</td>
<td><input name="userid" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td>Name</td>
<td><input name="name" type="text" style="width: 300px" /><br /></td>
</tr>
<tr>
<td></td>
<td><input style="width: 130px; height: 30px" type="submit" name="submit" value="Register Now" /><br /></td>
</tr>
</table>
</form>
hey i am new here any one can help me with this chunk of code in php and mysql
i know that this is a little mistake but i could not know where is the error and thank you.
this is the code :
//index.php
<html>
<head>
<title>Search for a user</title>
</head>
<body>
<h2> Search for a user below:</h2><br /><br />
<form action="profileprocess.php" method="get">
<table>
<tr>
<td>Username:</td><td><input type="text" id="username" name="username" /></td></tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="View Profile" /></td>
</tr>
</table>
</form>
</body>
</html>
// profileprocess.php
<html>
<head>
<title><?php echo $username; ?> <?php echo $lastname; ?>s profile</title>
</head>
<body>
<?php
if(isset($_GET['username'])){
$username = $_GET['username'];
mysql_connect("localhost", "root", "") or die ("could not connect t the server");
mysql_select_db("users") or die("this database was not found");
$userquery = mysql_query("SELECT * FROM users WHERE username='$username'") or die("the query could be fale please try again");
if(mysql_num_rows($userquery) != 1){
die("that username could not be found!");
}
while($row = mysql_fetch_array($userquery, MYSQL_ASSOC)){
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$dbusername = $row['username'];
$activated = $row['activated'];
$access = $row['access'];
}
if($username != $dbusername){
die ("there has been a fatal error please try again. ");
}
if($activated == 0){
$active = "this account has not been activated";
}else{
$active = "ths account has been activated";
}
if($access == 0){
$admin = "this user is not administrator";
}else{
$admin = "this user is an administrator";
}
?>
<h2><?php echo $username; ?> <?php echo $lastname; ?>s profile</h2>
<table>
<tr>
<td>firstname:</td><td><?php echo $firstname; ?></td>
</tr>
<tr>
<td>lastname:</td><td><?php echo $lastname; ?></td>
</tr>
<tr>
<td>email:</td><td><?php echo $email; ?></td>
</tr>
<tr>
<td>username:</td><td><?php echo $dbusername; ?></td>
</tr>
<tr>
<td>activated:</td><td><?php echo $active; ?></td>
</tr>
<tr>
<td>access:</td><td><?php echo $admin; ?></td>
</tr>
</table>
<?php
}else die("You need to specify a username!");
?>
</body>
</html>
//// any help????
I just run this code on my XAMPP server and it seems to work fine.
<html>
<head>
<title>Search for a user</title>
</head>
<body>
<h2> Search for a user below:</h2><br /><br />
<form action="" method="get">
<table>
<tr>
<td>Username:</td><td><input type="text" id="username" name="username" /></td></tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="View Profile" /></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if($_GET['username'] != ''){
echo $_GET['username'];
} else
die('doesnt work'); ?>
One problem i definitely see is that you have used echo at the start of the page and the query is not running. That is going to throw up errors.
Also, please tell us what the errors are, so that we can try and help you better.