I have made a form page with some radio buttons, text/textarea inputs and a total amount (price) at the end.
It is working/possible to input this into a table in MYSQL with a unique ID (AUTO INCREMENT at 100000).
Here is the situation:
When I submit my page i would like to automatically navigate to another page that still holds the "UNIQUE ID" and the "TOTAL AMOUNT (price)" so I could use it there to put it into another variable that i have to use for the redirection to the payment website.
I thought just to read the last entry in my database but what if 2 people are paying at the same time (no option!).
My unique ID is made into the database itself maybe that is my problem?
Is there somebody who could help me and provide me a walktrough?
Ex. My code:
if(isset($_POST['verzenden'])) {
$firstname = htmlentities ($_POST['firstname']);
$name = htmlentities ($_POST['name']);
$con= mysqli_connect("sqladres","username","password","databasename");
$query = 'INSERT INTO `inputorder`
(`contact_firstname`, `contact_name`)
VALUES ("'.$firstname.'","'.$name.'")';
Now I think I have to use $_SESSION to generate a session ID and also to write the amount (price) into this session and take it to the action page. But I've never used it before and really dont know how to use it in a good safe way!
see similar question to get last insert id:
How do I get the last inserted ID of a MySQL table in PHP?
To redirect with your variables you can use get method as:
header("Location:yourwebsite.com/payment.php?uniqe_id=". $uniqe_id . "&total=" . $total);
Related
I have a coins table in phpMyAdmin and I need it to be automatically updated when a user completes a purchase via PayPal. The way it is setup is that PayPal will automatically redirect them to a link I choose.
So, how can I run a script that takes the parameters from the URL and adds a new record to the table?
Thank you for all your help.
So that link would need to be a page called something like payment_success.php
On that page you would need to have a script that looks like this:
<?php
include 'credentials.php'; /*this is a file that will contain your database connection info
Then here you will establish the connection and enter the data into the desired table*/
$var1 = 'some information collected from paypal that you want to update';
$id = 'some identifiable information about your user that can connect them to the correct row in your table';
I won't hold your hand through all this but the SQL query you would need would be:
UPDATE table_name SET column_1 = '$var1' WHERE ID = '$id'
Set the confirmation PayPal link to increment the number of coins value in the MySQL table for the particular user....!!!
I hope that will work out..... If I could get a idea of your table or some more info, I could give you a clear idea about that.
I have two pages. One is a form that I use to simply input data that will be sent to my database and the second page that actually takes the data inputted into the form and sends it to the database and is supposed to display the information that I've just added.
Everything works fine, however I'm struggling with the query slightly. What I need it to do is display all the information for the last data inputted to the database.
The query I currently have just displays the data with the highest ID:
$sql = "SELECT * FROM Results ORDER BY ID DESC LIMIT 1";
So as an example I would be left with the following information after completing my form:
Success! Data being saved:
ID = 900 Amount = 206 Date = 2016-12-26
This is obviously just showing the data with the highest ID, but since the ID and all the data fluctuates, I need it to just show the data that has just been inputted.
I came accross this: Query to select newly added records only. But I don't believe this soultion to be viable as the database is external and I don't want to be creating new tables.
I was thinking that it might be possible to assign a hidden value to each newly added record via the query. e.g. New 1, New 2, New 3 etc. Then printing the latest record for New. However, I couldn't find anything on how to do this.
Any help would be greatly appreciated!
You must use this method to have very correct value:
Input form must send to another file that do inserting (we call it here insert.php)
insert.php must insert the data after validation and after that you can fetch the last ID number from database. Depending on the method you are working with it can be different. for example if you are using PDO you can get it by PDO::lastInsertId
after getting the ID you need to forward it to the viewing or editing page. for example view.php?id=LastInsertId. This forward have some reasons:
Codes can be cleaner.
We prevent refresh and resend inserting. for example if you do inserting inside view.php and user hit F5 to refresh the page, The insertion happening again.
This is the whole idea. you can use this method for only one page:
page.php?do=new
page.php?do=insert
forward to the page.php?do=view&id=lastInsertID
why you trying to get just inputted data from database? you can do it using HTTP POST/GET method easily.just send data as parameters and show them in second page.
If you already have the data you are inserting, you don't need to run a query to get it back from the database again, you could just ensure that the query was successful and display the data directly. Anyways:
You can get the insert ID from the last insert using the MySQLi object. For example:
$sql = "<your insert statement>"
$conn->query($sql);
$last_id = $conn->insert_id; //Id of the row you just inserted
$sql = "SELECT * FROM Results WHERE id=$last_id";
This is assuming you do the insert in the same page that you display the result.
I am trying to develop an application where a guest user can see search results only 10 times after which he should be directed to payment page. I can use sessions on the search results page, but how can i put a counter on that. Can any please help me on that.
Every time a search request is created you just do
$_SESSION['counter']++
Altough he can just get rid of the limit by deleting cookies. An other approach would be, to store the number of search requests in a database table including the IP address, but this can also be bypassed, while it takes more work to do so.
If you should put search limit on current running session than you can use $_SESSION['count']++.
And if you should put search limit per day than you can use 'UPDATE users SET search_count = search_count+1'
It depends whether you would allow him to search again when he comes to your website or just those 10 times even after he visits after a year.
For temporary bases, you can use cookies (see setcookie function) but if you want to restrict him once and for all, you will have to ssave that information in database.
You would code something like:
<?php
session_start();
$_SESSION['counter'] += 1;
// more logic/code
Now you will have to save the value of $_SESSION['counter'].
If your users can search only while logged in, then I see no problem - you definitely have db table with users, so just add another column to it, say 'search_count' and increase it by one each time user attemps a search.
For example:
UPDATE `users` SET search_count = search_count+1
You can also use a counter in the table user of your db and call a function everytime the user looks for the result, that increments the value by one, so a simple UPDATE.
I think maintaining database will be much better then maintaining SESSION because may be due to some reason session removed or erased.
add a field within users table name for example visit with default value 0 and update this field on every visit of search result page..
"update usertablename set visitfield = visitfield + 1 where user_id = ".$current_user_id
thanks
I have a form where I insert a record and the ID (Primary key is Auto increment).
How I can show the ID in the next page after submit. I tried to echo mysql_insert_id(); in next page but didnt work:
<?php echo "Your refrence number is = ". mysql_insert_id(); ?>
Friend, Let's see your issue this way. You have two pages A, and B. You insert data to the database in page A and get the last insert id with mysql_insert_id(); Now the issue is you want to use this in page B. I can propose you 4 ways. but I will explain you session base way since I think it is the best solution for you.
POST
GET
Cookie
Session
Ok In your page A top of your page you write session_start(); remember it should be the first line. Once you get the last insert ID in page a you create a session variable.
$_SESSION["lastID"] = mysql_insert_id();
And in page B also you initiate the session as you did in page A. Then you can simply get the last ID using.
$lastID = $_SESSION["lastID"];
Hope you understand.
Use PHP Session variable. Put mysql_insert_id() in session after insert and access on next page.
$_SESSION["yourID"] = mysql_insert_id();
and access it on next page.
You should get the last insert id in the the page where you insert the record, and then pass the the id to the next page (by url parameter).
I'm trying to insert variables from an SQL result into a HTML form in a way that the user can open a form and auto populate the values based on selected data.
I am displaying a table with query results, I'd like the user to be able to open a form that populates certain fields with data from a link in the table row.
This is what I have so far:
<?php
$result = mysql_query("SELECT * FROM numbers WHERE username = '$user_name' order by id DESC");
while($row = mysql_fetch_array($result))
{
echo $row['group_name'] . " " . $group_numbers = $row['group_numbers'] = 'Click here to insert numbers' . '<br/><br/>';
?>
I was then going to try and pick up the POST variable on account_group.php and then insert the numbers into form fields when opened.
Basically, I need a way to list all of the groups which contain numbers, and then have the ability to select which group of numbers you want to use (so to add them to the form).
While your question isn't really clear, I think I understand what you're trying to do, which is make links in a table that open a page with a form, having some fields preset by the values that you chose when clicking the link.
To do this is simple, just encode the values from your query in the links after they are displayed. E.g:
Click here to insert numbers
Then have form.php pick up the GET variables:
<input type="text" value="<?php echo isset($_GET['field1']) ? $_GET['field1'] : '';?>">
I would recommend iterating over GET variables that form.php expects to check them for sanity and set friendly variable names. E.g. $_GET['foo'] once confirmed set and checked for special characters and stuff could just become $foo.
You could also store them in the session for easy retrieval later. Note also that you'll want to make sure your links are properly encoded (via urlencode() when displaying the links, and urldecode() when consuming the variables in the form page).
This could be done with POST if you wanted, or lots of other ways. I suggested GET because it would be the simplest to implement.