Dear All,

I am new here, and I hope that my message goes in the right place; let me thank any one who will take the time to answer me, and wish much success to this board!

Now, let me introduce the point:

  • I am making a website to sell discs, using PHP/MYSQL
  • This issue is regarding the 'admin panel', where I created a page called 'insert_category.php' for the admin to insert a new music category.

Here is the code of this page :

<?php

// include function files for this application
require_once('disc_sc_fns.php');
session_start();

do_html_header("Adding a category");
if (check_admin_user()) {
  if (filled_out($_POST))   {
    $catname = $_POST['catname'];

if(insert_category($catname)) {
  echo "<p>Category \"".$catname."\" was added to the database.</p>";

} else {
      echo "<p>Category \"".$catname."\" could not be added to the database.</p>";
    }
  } else {
    echo "<p>You have not filled out the form.  Please try again.</p>";
  }
  do_html_url('admin.php', 'Back to administration menu');
} else {
  echo "<p>You are not authorised to view this page.</p>";
}

do_html_footer();

?>

Other files are included to make this page run, of course :

The first one is the connection to the database:

<?php

function db_connect() {
   $result = new mysqli('localhost', 'batman', '1234', 'disc_sc');
   if (!$result) {
      return false;
   }
   $result->autocommit(TRUE);
   return $result;
}

function db_result_to_array($result) {  
$res_array = array(); for ($count=0; $row = $result->fetch_assoc(); $count++) { $res_array[$count] = $row; } return $res_array; } ?>

Here is the function used in insert_category.php, for inserting the category into the database :

function insert_category($catname) {
// inserts a new category into the database

   $conn = db_connect();

   // check category does not already exist
   $query = "select * from categories where catname='".$catname."'";
   $result = $conn->query($query);
   if ((!$result) || ($result->num_rows!=0)) {
     return false;
   }

   // insert new category
   $query = "insert into categories values ('', '".$catname."')";

   $result = $conn->query($query);
   if (!$result) {
     return false;
   } else {
     return true;
   }
}

This is it for the background..

So basically, a category is inserted in my database, and I would like to get the ID after inserting a category; I tried the function mysql_insert_id(), I tried last_insert_id() too, but.. I did not work 🙁

I have been searching the solution on many websites, but I was not able to succeed 😕

I found out that I had to link the mysql_insert_id() function with the 'link_identifier' : is this the solution?
How would I do this (meaning what would be my link_identifier in this case?!)

That is it, let me thank again all the people that will take the time to help me!

Cheers to the whole board...

Batman

    mysql_insert_id() won't work since you are using the mysqli extension. You can instead use the insert_id parameter in your insert_category() function:

    function insert_category($catname) {
    // . . . code removed to save space . . . //  
    $result = $conn->query($query); if (!$result) { return false; } else { return $conn->insert_id; // function will now return the ID instead of true. } }

    Then you could use the return value (if not false) to get the inserted ID.

      NogDog, you rock 🙂

      I will have a try and let you know how successful it is 🙂

        Well, it seems that I did something wrong 😕

        I just followed your advice, then on the page executing the query, I added it this way :

          if (filled_out($_POST))   {
        
        $catname = $_POST['catname'];
        
        if(insert_category($catname)) {
        
        echo "<p>Category \"".$catname."\" was added to the database.</p>";
        
        echo "The ID is: ".$mysqli->insert_id;
        
        } else {
              echo "<p>Category \"".$catname."\" could not be added to the database.</p>";
            }
        

        But I got nothing :o

        Do you have any idea?

        Anyway, BIG THANKS, I am close to the desired result!

        Batman

          Assuming you made the change I suggested to the insert function, then when you call it, you'd want to assign its result to a variable, which will then be the insert ID if the query was successful, else a boolean false.

          $id = insert_category($catname);
          if($id !== false)
          {
             echo "The ID was '$id'";
          }
          else
          {
             echo "Uh-oh! the insert query failed.";
          }
          

            Let me thank you very much, it now works perfectly...

            I have much to understand, please forgive my newbie-ness.

            Long live this board 😃

              Don't forget to mark this thread "resolved" (if it is) via the "thread tools" link near the top.

                Write a Reply...