Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The function <a href="http://php.net/manual/en/mysqli.insert-id.php" rel="nofollow"><code>mysqli_insert_id()</code></a> returns the ID of the last inserted row.</p> <pre><code>&lt;?php $link = // Connect to the database using the mysqli driver // Create author (if not exists already) mysqli_query($link, 'INSERT INTO authors (name, ...) VALUES ("author name", ...)'); $author_id = mysqli_insert_id($link, $link); // Escape ID $author_id = mysqli_real_escape_string($link, $author_id); // Create the book mysqli_query($link, 'INSERT INTO books (name, author_id ...) VALUES ("book name", ' . $author_id . ' ...)'); $book_id = mysqli_insert_id($link); // Escape ID $book_id = mysqli_real_escape_string($link, $book_id); // Add the genres mysqli_query($link, 'INSERT INTO genres (book_id, genre_id) VALUES (' . $book_id . ', ...)'); </code></pre> <p>This code uses the procedural <code>mysqli_*</code> functions. This isn't the cleanest way to proceed, and the code above should <strong>only be seens as indications on how to proceed</strong>. For the sake of clean code (well, in PHP standards), look into PDO for a cleaner database access model. </p> <ul> <li><a href="http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/" rel="nofollow">A tutorial on PDO</a> (found on Google, haven't fully checked it out)</li> <li><a href="http://www.php.net/manual/en/class.pdo.php" rel="nofollow">PHP manual page on PDO</a></li> <li><a href="http://www.php.net/manual/en/mysqlinfo.api.choosing.php" rel="nofollow">The “Choosing and API” guide</a> from the PHP docs</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload