Hello everyone.
I have the following problem
get the last id from the database
but you can not insert a text field, the last id
has, solution?
pls pls

<?
$localhost = "localhost";
$username = "username";
$password = "password";
$dbname = "nameMyDB";
$conn = mysqli_connect($localhost, $username, $password, $dbname);
// Check connection
if (!$conn) {    die("Connection failed: " . mysqli_connect_error());   }


$sql = "INSERT INTO Guests (firstname, lastname, code) VALUES ('Sam', 'Smith', '1')";

if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);
    echo "created successfully. Code is: COD" . $last_id;
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

?>

<form action="Test.php" method="post">
<input name="firstname" type="text">
<input name="lastname" type="text">
<input name="code" type="text" value="COD<? echo $last_id;?>">
<input name="Send" type="submit">
</form>

Result;

Names: Sam Smith
Code: COD1

Names: Tim Thompsom
Code: COD2

etc..

Recommended Answers

All 5 Replies

Not sure if I understood ypur question but I'll have a go at it.
The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute (sentence copied from the PHP manual). Now, in your insert query the ID is not treated as an autoincrement type, or if it is, you should not supply value for it - it will be created automatically by the DB server. Autoincrement fields can only be of an integer type so you will have to append string characters in the PHP code (as in your example above).

If the answer is not what you wanted please elaborate what you want to do and what errors do you get.

something like that???

$sql = "INSERT INTO Guests (firstname, lastname, code) VALUES ('Sam', 'Smith', 'mysqli_insert_id($conn)')";

VALUES ('Sam', 'Smith', 'mysqli_insert_id($conn)')"; // ????

Show info insert ID :

 $last_id = mysqli_insert_id($conn);
    echo "My Code is: COD" . $last_id;

I want to save this ID in a text field
<input name="code" type="text" value="COD<? echo $last_id;?>">

The correct query would be:

$sql = "INSERT INTO Guests (firstname, lastname) VALUES ('Sam', 'Smith')";

The code will get created automatically. When you want to create the input element using the last ID, first read the last ID from the database:

$sql = "SELECT MAX(code) AS last_id FROM guests";
if($result = mysqli_query($link, $sql)) {
    $row = mysqli_fetch_assoc($result);
    $last_id = $row['last_id']);
}

and then as you did it above:

<input name="code" type="text" value="COD<?php echo $last_id;?>">

Mysql MAX() function will always return the last ID since it is the highest number if you use autoincrement.

Your script does NOT take the input from the page, so it will keep inserting the SAME data into your table because of your hard code in line 11.

What you need to do is to retrieve value from the page form using $_POST. For example, you can retrieve value from<input name="firstname" type="text"> using $firstname = $_POST['firstname'] . If you don't get any value, do NOT do anything with the database part; otherwise, you could get an error and the page will stop working.

Once you retrieve all values from the page, reconstruct the query to use those variables instead of your hard code. By the way, you MUST validate the input before you insert into your database.

I believe that's what your question is asking for. It has NOTHING to do with getting the last ID...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.