mysql - Query to display record just added - php

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.

Related

Automatically edit a php table

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.

Passing wrong data for random elements with PHP+MYSQL+JSON+FBGRAPH

Hello there;
I'm making basic script - save values, then compare it, if it exists - show different class, everything works fine with 99% of entries. I click on button, jquery posts data to mysql, and after that - data has been saved - BUT - there are some of the elements that gets written in with elementid minus one value.
How my logic works:
JSON gets values from Facebook Graph - it got userID like - 19999;
It is now splitted in array;
Array is getting arranged by names;
And all userids are my button id to pass each userid to mysql;
If user gets clicked it gets written in db;
Then it shows up - if user is there, or not...
Problem
So i click on 99% everyting is working just perfect, can't see no problem;
But then there are some entries which get written in database with wrong userid.
JSON gives value = 1999 -> same for button id = 1999;
BUT It writes value into database = 1998 [?]
WHY?
All other entries are working just fine, but in all list, there are few expectations.
So i've been googling this problem, can't find any solution or reasoning.
SQL queries
SELECT * FROM checkin WHERE (eid = '".$eid."' AND suid = '".$suid."' )
Problem solved.
It was in Jquery, i was using
var id,
and variable was called with (+id), now with (id) everything works fine.

PHP POST WITH DATATABLE PAGINATION

Sorry if I am repeating the question, I have a HTML FORM page which displays the employee_id & attendance check box (IN, OUT) using datatable.
I made pagination with initial load value in the screen as 10, the data are loaded from mysql DB and all the data loads perfectly with pagination. I will update attendance status of each employee which checkbox and submit the form.
Once i submit the form, i called the PHP _POST (if(isset($_POST['save']))) , inside this i am trying to get the value of all employee_id & checkbox value but, i can able to get only first 10 rows, remaining in page 2,3,4,5 are not available, is there any option to get them, i tried with jquery for each too but it also alerts on first 10 rows.
Please help me out.
I need to post the data to database from data table regardless of pagination either using PHP post method (or) Jquery.
If you using pagination by LIMIT in database query there is, of course, no way you would get more than 10 rows, so, your <form> submit will not give you more than those 10 data arrays you can save to database.
Can you explain why you need to update all rows in database? Because limiting by pagination guarantees that other rows will not be affected (if query is right).

SQL/PHP: Calling unique ID

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);

AJAX response from database creates duplicate entries

I am trying to build a simple forum where the questions wil be refreshed automatically . Although it is done ,I have two problems.
1 . Since I retrieve the last update from database,last entry is dulicated.
2 . The div where I put the AJAX response always refrshes .
How can I remove the duplicate entries and make it smooth forum ,just like in facebook comment page ?
If I understand your first problem, you're saying you are doing something like:
Take the user's input (i.e. the new entry)
Write this input to your database
Select all entries, including the one you just added
Because you are also separately adding the new reply to the div client-side or something the last entry always shows up twice (but doesn't get written to the database twice).
If this is the case you could solve the problem by either omitting the last entry from your MySQL query (e.g. SELECT * FROM entries WHERE id != (SELECT MAX(id) FROM entries)) or by simply refraining from processing the entry on the client's side.
Concerning your second problem:
Instead of fetching all entries and replacing all content inside the div with the new list of entries, try only appending the new entry with .innerHTML += ... or jQuery's .append() for example. This way you won't experience any 'flickering' or jumping content with the rest of the replies.

Categories