Jump to content

PHP/SQL - Unable to get the ID after INSERT


batman12345

Recommended Posts

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 :confused:

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.