How can the issue of assigning a new ID to each new entry in a MySQL database be addressed in PHP?
When inserting a new entry into a MySQL database, it is common practice to assign a unique ID to each entry. This can be achieved by setting the ID column in the database table to auto-increment. This way, MySQL will automatically assign a new ID for each new entry, ensuring uniqueness.
// Assuming we have a table named 'entries' with an auto-increment ID column
// Connect to the database
$connection = new mysqli('localhost', 'username', 'password', 'database');
// Insert a new entry into the database
$query = "INSERT INTO entries (column1, column2) VALUES ('value1', 'value2')";
$connection->query($query);
// Check if the query was successful
if ($connection->affected_rows > 0) {
echo "New entry added successfully with ID: " . $connection->insert_id;
} else {
echo "Error adding new entry";
}
// Close the database connection
$connection->close();