I need to pass the sessions user name (that they have logged in with) which is an email! I need to pass this to a separate page and output it in a table to represent a review submitted
$_SESSION['name'] = $_POST['name']; - sends page to login in when refreshed
$name = ['name'] - sends page back to login
<!-- logged in user information -->
<?php if (isset($_SESSION['email'])) : ?>
<p>Welcome you are logged in as: <strong><?php echo $_SESSION['email']; ?></strong></p>
the 'email' needs passing across from the code above 'index.php' to the code below 'reviews.php'
<p>
<input name="product_id" value="<?php echo "$var" ?>" readonly> <!-- get value from previous page-->
<input name="track_name" value="<?php echo "$var_value" ?>" readonly> <!-- get value from previous page-->
<input name="track_name" value="<?php echo "EMAIL_HERE" ?>" readonly> <!-- get value from previous page-->
<!-- get value from previous page-->
<input type="Submit" name="Submit" value="Submit"></p>
As this is an assignment I can only use PHP MYSQL HTML CSS
I would like the user name (email) to be echo out in a table as $var and $var_value is, they should then all print out beside each other in a form
UPDATE
using this code I have managed to now get the variable value across but cannot insert it to the DB
$email = $_SESSION['email'];
$sql = "INSERT INTO reviews (rating, review, track_name, product_id, email) values('$rate', '$text', '$track', '$artist', '$email')";
``" readonly>```
so the update is how can I now get this inserted to my database?
did you try to use $_SESSION['email'] variable in reviews.php page?
I hope that u are using CSRF tokens too
FINALLY THANK YOU FOR YOUR HELP ALL
$email = $_SESSION['email']; Get the email from session
$email = (isset($_POST['email']) ? $_POST['email'] : null); Remove index error
<input name="email" value="<?php echo "$email" ?>" readonly> <!-- get value from previous page--> Display the value
$review_query = mysqli_query($result,"SELECT rating, review, email FROM reviews WHERE track_name = '$var_value' AND product_id = '$var'"); Grab it from the DB
<td class='col-4 col-s-4' name='email'><?php echo $email ?></td> Output its value
Related
I have two pages page one and page_two, in page_one the user enter some information which will be inserted in the database and when the he press enter he should be directed to page_two and inside this page there are the same information that he entered in page_one. and the problem is every time the user refresh page_two the data is inserted in the database again. I tried to fix this issue by using header to a new page, it worked but in page_two the information that was entered in page_one is lost.
page_one
<form action="page_one.php" method="post" name="info" >
<input name="userName" type="text" />
<input name="userEmail" type="text" />
<input name="userPass" type="text" />
<input name="submit" type="submit" />
</form>
<?php
include('db.php');
if(isset($_POST['Login']))
{
$user_name = $_POST['userName'];
$user_email = $_POST['userEmail'];
$password = $_POST['userPass'];
mysql_query("INSERT INTO users VALUES ('$user_name',' $user_email',' $password')");
header("Location:page_two.php.php");
exit;
}
?>
page_two
<?php
$user_name = $_POST['userName'];
$user_email = $_POST['userEmail'];
$password = $_POST['userPass'];
echo 'your user name: '.$user_name;
echo 'your email: '.$user_email;
echo 'your password: '.$password;
<input name="userName" type="hidden" value="<?php echo $user_name; ?>" />
<input name="userEmail" type="hidden" value="<?php echo$user_email; ?>" />
<input name="userPass" type="hidden" value="<?php echo $password; ?>" />
when I try this code it gives me this error message from page_two:
notice undefined index userName
notice undefined index userEmail
notice undefined index userPass
Pass the variables via url to page_two.
So your header will be
header("Location:page_two.php.php?userName=user_name&userEmail=user_email&userPass=password");
Now catch these variables using $_GET on page_two
<?php
$user_name = $_GET ['userName'];
$user_email = $_GET ['userEmail'];
$password = $_GET ['userPass'];
echo 'your user name: '.$user_name;
echo 'your email: '.$user_email;
echo 'your password: '.$password;
You have the correct approach, but on page_2, instead of retrieving the values from the $_POST array, you should retrieve them from the database, as they now exist there. This will remove your undefined index problem.
Redirect using header to some safe page after inserting the data. You can rather use id of the inserted row to get data on page_2.
Hope this helps.
Since you're building a multi-page web-app. I suggest you have to use SESSION to save the posted information of the 1st page, then use the SESSION variable for the 2nd page.
I hope the link below helps.
http://www.html-form-guide.com/php-form/php-order-form.html
On page two you should include a Select statement which will select all the values that are stored in your table.
mysql_query("SELECT * FROM users ");
I am trying to create a page to allow users to edit their details using PHP, which validates the content before being submitted.
I want to allow users to update their username, first and second name and email address.
The validation consists of:
<?php
if(preg_match("/^[0-9a-zA-Z_]{3,}$/", $_POST["username"]) == 0)
$error_username = '<li>Usernames may contain only digits, upper and lower case letters and underscores</li>';
if(preg_match("/^[A-Za-z]+$/", $_POST["fname"]) == 0)
$error_fname = '<li>First Name may contain upper and lower case letters</li>';
if(preg_match("/^[A-Za-z]+$/", $_POST["sname"]) == 0)
$error_sname = '<li>Second Name may contain upper and lower case letters</li>';
if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\#\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) == 0)
$error_email = '<li>Email Addresses must have a valid email address format</li>';
else header("Location: edit.php");
?>
And to display the errors:
<ul>
<?php if(isset($error_username)) echo $error_username; ?>
<?php if(isset($error_fname)) echo $error_fname; ?>
<?php if(isset($error_sname)) echo $error_sname; ?>
<?php if(isset($error_email)) echo $error_email; ?>
</ul>
The form that I have is:
<form name="edit_account" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input class="form_field" name="username" type="text" value="<?php echo $_POST["username"]; ?>" placeholder="Username">
<input class="form_field" name="fname" type="text" value="<?php echo $_POST["fname"]; ?>" placeholder="First Name">
<input class="form_field" name="sname" type="text" value="<?php echo $_POST["sname"]; ?>" placeholder="Second Name">
<input class="form_field" name="email" type="text" value="<?php echo $_POST["email"]; ?>" placeholder="Email Address">
<input type="submit" name="Submit" value="Update Account">
</form>
Providing that all requirements of the validation are met, the user is taken to edit.php and then redirected to a success page:
<?php
$sql = $mysqli;
$id = htmlentities($_SESSION['user_id']);
$username = $sql->real_escape_string($_POST['username']);
$fname = $sql->real_escape_string($_POST['fname']);
$sname = $sql->real_escape_string($_POST['sname']);
$email = $sql->real_escape_string($_POST['email']);
$query = ("
UPDATE users
SET
username='$username',
fname='$fname',
sname='$sname',
email='$email'
WHERE id='$id'") ;
$sql->query($query) or die($query.'<br />'.$sql->error);
header ('Location: success.php');
?>
When I attempt to run this code, the updating fields are submitted into the database as blanks - However, without the validation, the users submitted details are successfully entered.
Can someone please point out what is causing the form to submit as a blank. Thanks.
It looks like you are redirecting to edit.php (which contains database insertion code) from the validation script. The issue is that the $_POST variable is reset when you redirect.
I would include('path/to/edit.php') the edit.php script rather than redirect to it. If that isn't possible, I would save the $_POST data in a $_SESSION variable.
Hope this helps
You're posting them to the validation page, but losing them when you redirect to the edit.php page. Store the information in session variables before going to edit.php, like this:
$_SESSION['username'] = $_POST["username"];
// other variables also
On the edit.php, instead of pulling from $_POST, pull from $_SESSION.
Side Notes
Don't forget session_start() at the top of each page. Also, you should look into prepared statements when using user input.
I make file details.php that show list of data from MySQL, and I make edit.php for it.
When I click edit.php it's automatically go to edit.php?id=detailsid (it works) but when I click submit on edit data, it's nothing changed and error.
This is my Edit.php
<form method="post" action="editdata.php">
<?php
include 'config.php';
$id=$_GET['id'];
$sqlTampil="select * from data_korban Where kasus_id=$id";
$qryTampil=mysql_query($sqlTampil);
$dataTampil=mysql_fetch_array($qryTampil);
$data=mysql_fetch_array($qryTampil);
?>
This is some of my HTML form
<input type="DATE" name="tanggal" size="20" value="<?php echo $dataTampil['tanggal']; ?>">
<input type="text" name="namakorban" size="40" value="<?php echo $dataTampil['namakorban']; ?>">
<input type="text" name="umurkorban" size="5" value="<?php echo $dataTampil['umurkorban']; ?>">
<input type="submit" name="submit" value="SUBMIT">
When I click Submit it's automatically call editdata.php but there is no change on my MySQL row ..
This is my editdata.php
<?php
include "config.php";
$id=$_GET['id'];
$tanggal = $_POST['tanggal'];
$namakorban = $_POST['namakorban'];
$umurkorban = $_POST['umurkorban'];
$update = "UPDATE data_korban SET tanggal='$tanggal', namakorban='$namakorban', umurkorban='$umurkorban' WHERE kasus_id = '$id'";
$hasil = mysql_query($update);
if ($hasil)
echo "<center>Update Success<center>";
else
echo "<center><h3><a href=pencarian.php>Back Tampil Data</a></h3></center>";
?>
There is something in my Error_Log
PHP Notice: Undefined index: id in /home/lombokdi/public_html/dbanak/editdata.php on line 4 ------ it's $id=$_GET['id'];
I have change to $_POST['id']; it's still error.
Your form does not seem to include the id named field.
And you are trying to read the non existing field from the request.
Request is formed to send data over post method but you tried to read id parameter using get method. As the value for id is not found, a null is used in where clause and that affected no rows updated.
$id=$_GET['id'];
$tanggal = $_POST['tanggal'];
$namakorban = $_POST['namakorban'];
$umurkorban = $_POST['umurkorban'];
the value for ID is $id=$_GET['id']; ... id come from detail.php?id=24 (example) go to edit.php?id=24 .. If I'm wrong, how to set the ID ??
In edit.php add id named element to the form with value read from the request. And on submitting the form, id goes to editdata.php page.
<input type="DATE" name="tanggal" size="20" value="<?php echo $dataTampil['tanggal']; ?>">
<input type="hidden" name="id" value="<?php echo $id['id']; ?>">
<input type="text" name="namakorban" size="40" value="<?php echo $dataTampil['namakorban']; ?>">
<input type="text" name="umurkorban" size="5" value="<?php echo $dataTampil['umurkorban']; ?>">
And in editdata.php:
Change:
$id=$_GET['id'];
To:
$id=$_POST['id'];
It's because you don't have the filed "id" if you switch to editdata.php
add: <input type="hidden" name="id" value="<?php $_GET['id']; ?>">
to your form in Edit.php
and replace
$id=$_GET['id']; with $id= $_POST['id']; in editdata.php.
Try like this
$id=$_GET['id'];
$sql=mysqli_query($con,"Update client set nama='$nama',instansi='$instansi',telepon='$telepon',email='$email' where id = '".$_GET['id']."'");
if($sql)
{
$msg="Your Client updated Successfully";
header('location:manage-client.php');
}
for after body add this query
$sql=mysqli_query($con,"select * from client where id = '".$_GET['id']."'");
while($data=mysqli_fetch_array($sql))
You are using $_GET to get the id. It is best do var_dump($_REQUEST);
I have the following code:
$sql = mysql_query("UPDATE users SET name='$name' WHERE email='$email'") or die(mysql_error());
echo "<h4>Your information has been updated</h4><br />";
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for='name'>Name</label><br /><br />
<input class="input-text required" name="name" type="text" value="<?php echo $row[name]; ?>" />
</p>
<p><input type="submit" name="update" value="Update" /></p>
</form>
After clicking Update, it updates the information in the database, but the <?php echo $row['name']; ?> still shows the old value. Only after refreshing the page does it show the updated information. I could have it refresh the page after updating by echoing meta refresh but I want it to still show the echo saying "Your info has been updated" which doesn't happen if I set it to refresh. Is there any solution?
You will need to requery the database to repopulate $row or explicitly set the $row variable to your values (I would recommend repopulating it, just to be safe).
Or echo $name, instead of $row[name]
A update of the database is not a new select of the data.
You have 2 options:
after the update, do a new select of the data
instead of writing the row back to the form, write the posted value back
I am very new to PHP.
I want to transfer data between pages.
In my requirements I have the home page first, in that I have 3 fields: name, address, pin and then submit button.
When I entered the above fields and then click on submit, it moves to page2.php, it has form data.
I have transferred the first form data to second page. Now in second page I have a submit button. When I click on that button the data is submitted to a MySQL database.
My problem is, how can I move first page values to insertdata.php page and submit the data ?
There are two ways with which you can do this
Sessions
Hidden input fields
Sessions
To pass data from one page to another, you first need to call session_start() on all pages that are going to use $_SESSION superglobal variable.
Then you can store your values in sessions by using
$_SESSION['name'] = $_POST['name'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['pin'] = $_POST['pin'];
To use these values in the second page, simply call them by their names. Ex:
$name = $_SESSION['name']; // will contain the value entered in first page
==================================================================================
Hidden Input Fields
This is more of a tedious approach but it does the job none the less. The process involves storing data that needs to be passed onto to different pages in hidden fields and later accessing them via the $_POST or $_GET superglobal.
page1.php (which posts to page2.php)
<input type="text" value="Page 1 content" name="content" />
<input type="text" value="Page 1 body" name="body" />
page2.php (which posts to page3.php)
<input type="hidden" value="<?php echo $_POST['content']; ?>" name="content" />
<input type="hidden" value="<?php echo $_POST['body']; ?>" name="body" />
<input type="text" value="Page 2 content" name="content2" />
<input type="text" value="Page 2 body" name="body2" />
page3.php
echo $_POST['content']; // prints "Page 1 content"
echo $_POST['body']; // prints "Page 1 body"
echo $_POST['content2']; // prints "Page 2 content"
echo $_POST['body2']; // prints "Page 2 body"
just use the below code in your first page <?php session_start(); ?>
and use the following code in your sencond page
<?php
$name = $_SESSION['name'];
$address = $_SESSION['address'];
$pin = $_SESSION['pin'];
echo $name."<br/>";
echo $address."<br/>";
echo $pin."<br/>";
?>
or you can use post or get method as below
For GET Method
<?php
$name = $_GET['name'];
$address = $_GET['address'];
$pin = $_GET['pin'];
echo $name."<br/>";
echo $address."<br/>";
echo $pin."<br/>";
?>
For POST METHOD
<?php
$name = $_POST['name'];
$address = $_POST['address'];
$pin = $_POST['pin'];
echo $name."<br/>";
echo $address."<br/>";
echo $pin."<br/>";
?>
To store data between page changes, you can use the superglobal $_SESSION array.
Example
Page 1
$_SESSION['name'] = "John";
Page 2
echo $_SESSION['name'];
Output
John
Make sure you put session_start() at the top of each page that requires the use of sessions.
Click here for more information on Sessions.
Basically when you submit the data in the first page save them in the session then redirect to the second page.
In the second page just load them.
Page 1:
<?php session_start();
$_SESSION["var_name"] = $some_value;
?>
page 2:
<?php session_start();
$my_var = $isset($_SESSION["var_name"])?$_SESSION["var_name"]:null;
?>
you test the value of my_var, if it's empty then redirect to the first page or show an error message.
session tutorial
http://www.w3schools.com/php/php_sessions.asp
Mysql insert tutorial
http://www.tizag.com/mysqlTutorial/mysqlinsert.php
POST/GET tutorial
http://www.tizag.com/phpT/postget.php
Like REQUEST, PHP maintains a map for session in which you could dump these values and across the pages.
e.g.
$_SESSION['name']='foo';
But the page where you would like to use session, you need to start the session using the method session_start(); . But this method call should be the first line in your php.
http://www.w3schools.com/php/php_sessions.asp