Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make a text box display the last entry made into a database a mysql table?

I would like to display the last entry made in the database table inside of a text box. Currently all the entries are being displayed in text boxes but i want just one text box that contains the last entry i made into the database table. These is what the output looks like:

output

This is the code that I used to generate the output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php
 $conn = mysqli_connect("localhost", "root", "", "new");
if(!$conn){
die("connection error". mysqli_connect_error());
}
/*else{

    echo"connection successful";
}
*/
$connectDb=mysqli_select_db($conn,"new");
$result = mysqli_query($conn,"select name from new");
while($row = mysqli_fetch_array($result)){
?>
<input type="text" name="name" value="<?php  echo $row['name']; ?>" readonly>
<?php

}

>Solution :

Database tables have no inherent ordering. How do you define the "last" entry?

If, for example, you have an ID or date column in your table, you could use that to order your data by time of creation.

For instance if you have an auto-increment ID column in your table then you could write

SELECT name FROM new ORDER BY ID DESC LIMIT 1 

to get only the most recently-added record.

Live demo: https://dbfiddle.uk/jaXDboLq

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading