I have this problem,on the same form,i want to get the id from saved value.
i already make some example on JS Fiddle
JS Fiddle :
https://jsfiddle.net/8a79wch7/
Detailed Process :
1.Input Value on Form 1-3.
2.Save Value to database and get the auto incremented ID
3.Click Next,and use the ID from previous saved value (this is the question i want to ask).
Any Tips for doing this ?
This will give you the last insert ID
$this->db->insert_id();
Just redirect the user with correct URL by getting the LAST_INSERT_ID
redirect('controller/method/LAST_INSERT_ID', 'refresh');
The URL might Look something like this
http://example.com/controller/method/1
Related
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.
Hi everyone,
When I delete first record, my table will live update but the order number remained '2',
is it possible to make it change to '1' without refresh page,
currently I am using ajax to do it, and found json_encode & json_decode maybe able to solve my problem?
$.ajax({
type:"POST",
url:"delete_item.php",
data:{id:del_id},
success:
function(){
}
});
$(this).parents(".record").animate('fast').animate({opacity:'hide'},'slow');
This is for display my data
<tbody id='item_list'>
<?php eitem_listItem($page, $record,$search); ?>
</tbody>
Sorry for my poor english.
The easiest way of addressing this is to hide the actual id of the data row (presumably you are using id as the unique identifier for your query) - so have it as a data attribute or an id for the delete button (to give you the del_id) or other method of keeping it in the data but not displayed.
Then when you are iterating through your data to display it - have a javascript count set up so that on the display of the data - each <tr> row gets its displayed index from this count - rather than the id of the actual data.
This means that you will display a 1 for the first displayed row - but the actual id that you use for the delete button may be 2 for example.
So the number on the side of the table becomes a displayed index that will always match the actual sequence of the data rows, but not necessarily will match the id of the row.
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);
my userlisting provides link to edit users and update profile as when one clicks the screen name of the user then it goes to profile update view with the id of corresponding user:
//url:
localhost/damcombd/admin/userprofile/userupdate/3
The form loads nice and address bar and on mouse over status bar both shows the link as above
but in reality in the controller if i pull something with this id say from user model it just goes to the first record or the id 1.. and doing it all the way.. try as i might!
What could be the reason? Checked for any hardcoded ids too.. but that's not the case..
//with this one too I create a row and get it back like:
$this->commonmodel->create_and_get($table,$data){
$this->db->insert($table,$data);
$lastid=$this->db->insert_id();
$getback=$this->db->get($table,array('id'=>$lastid))->result_array();
return $getback[0];
But what it return every time the first row.. I dont know like the userprofile updating above this one also shitting around.. Is it a bug or something..I dont get it at all..try as I might:( :( Please do something..
This may be because:
a) In your routes you're specifying an id it's taking all the time:
b) Your controller is taking a wrong id
c) Your model is executing a wrong query.
What I'd do: Check you have in the routes applocation/config/routes a sentence with
$routes['admin/userprofile/userupdate/(:num)'] = 'admin/userprofile/userupdate/$1'
Maybe you forgot the $ before the 1, ;P.
Then, check the value you're sending, for being sure is the right id, after receiving the POST/GET value, print it and check if this value is the expected one (you're editing user 2, so you get id 2 and so on).
If the value is correct, check what are you retrieving from the database. In your model, you can write:
echo $this->db->last_query();
and check what SQL query is executing. Just do as I wrote you above, and tell us what happens, :D
The second problem I mentioned is solved when I used
$this->db->get_where($table,array('id'=>$lastid));
//actually for get the above line would have been false syntax..
I think I will I figure next one out too...
My First problem is solved too... That exact way actually..
to describe it a bit so that some one might be helped..
//If u use
$obj=$this->db->get($table,array('id'=>$lastid))->result_array(); //here then
// it will actually do a simple get all operation and returning:
return $obj[0] //will obviously return the very first row..try as u might.
//so for querying with id always use:
$this->db->get_where();
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).