I want to auto generate the id number. I write this code. But it's not work. I don't make primary key for id in database.

function getData()
{
  $data = array();

  $data[1] = $_POST['name'];
  $data[2] = $_POST['fname'];
  $data[3] = $_POST['address'];
  $data[4] = $_POST['phone'];
  $data[5] = $_POST['class'];
  $data[6] = $_POST['qualification'];
  $data[7] = $_POST['branch'];
  $data[8] = $_POST['rollno'];
  $data[9] = $_POST['gender'];
  $data[10] = $_POST['birth'];
  return $data;
}

//insert
if (isset($_POST['insert'])) {
  $info = getData();
  $insert_query = "INSERT INTO `smash`(`name`, `fname`, `address`, `phone`,`class`,`qualification`,`branch`,`rollno`,`gender`,`birth`) VALUES ('$info[1]','$info[2]','$info[3]','$info[4]','$info[5]','$info[6]','$info[7]','$info[8]','$info[9]','$info[10]')";
  try {
    $insert_result = mysqli_query($conn, $insert_query);
    if ($insert_result) {
      if (mysqli_affected_rows($conn) > 0) {
        echo ("data inserted successfully");
      } else {
        echo ("data are not inserted");
      }
    }
  } catch (Exception $ex) {
    echo ("error inserted" . $ex->getMessage());
  }
}

Dani AI

Generated

Quick summary and what helped: the insert kept failing because the table wasn’t letting MySQL generate the row id. solved it by recreating the table so the database could manage the id, which matches ’s advice to let the DB own that value. ’s suggestion to log and inspect values is also useful whenever an insert silently fails.

Practical checklist

  • Don’t accept an auto id from $_POST. Let the DB assign it and omit the id column from your INSERT.
  • Use prepared statements to avoid SQL injection and binding mistakes.
  • Validate and log the actual data you’re inserting (use error_log(print_r($data, true)) or var_export during debugging).
  • After a successful insert get the new id from the connection object (PDO or mysqli) rather than relying on manual SELECTs.

Example (PDO) — different approach from the thread:

$stmt = $pdo->prepare(
  'INSERT INTO smash (name,fname,address,phone,class,qualification,branch,rollno,gender,birth)
   VALUES (?,?,?,?,?,?,?,?,?,?)'
);
$stmt->execute([$name,$fname,$address,$phone,$class,$qualification,$branch,$rollno,$gender,$birth]);
$newId = $pdo->lastInsertId();

Schema and safety notes

  • Avoid using a user-supplied id and avoid SELECT MAX(id)+1 (race conditions). Let an integer AUTO_INCREMENT or a UUID handle uniqueness.
  • Prefer plain INT/BIGINT types (use UNSIGNED if only positive values). The display-width number (e.g., INT(255)) is not meaningful for capacity.
  • Enable database error reporting while debugging (for mysqli use mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) or catch PDOException) so you see the real error instead of silent failures.

If an insert still fails: dump the prepared SQL and bound values, check mysqli_error() / exception message, confirm $conn is the same connection used for the query, and verify column names match exactly.

Recommended Answers

All 5 Replies

Now I write:

function getData()
{
  $data = array();

  $data[0] = $_POST['id'];

and then

  $insert_query = "INSERT INTO `smash`(`id`, `name`, `fname`, `address`, `phone`,`class`,`qualification`,`branch`,`rollno`,`gender`,`birth`) VALUES ('$info[0]','$info[1]','$info[2]','$info[3]','$info[4]','$info[5]','$info[6]','$info[7]','$info[8]','$info[9]','$info[10]')";

but nothing change.

This is a fine example of poorly documented code as well as not much written in the design document. Yes, this is just a forum but as presented there's so much missing that any guess will likely be a bad guess.

I can't guess what the requirements are for 'id' so skipping over that, why not log what id is in your php code so you can inspect what is happening?

The id element is usually something controlled by the database not by the php code. You would need to have an id column in the database set as primary key with the "auto-increment" value set. Then, after running your insert query, you can use mysqli_insert_id() to get the id of the element you just inserted.

If you can not have an auto-increment primary key for id for some reason, then you need to first get the id of the highest row in the database using a select and then +1 to get the next id. This however is not the correct method for a number of reasons...

dear ajbest
I do as your direction and it's work well. just set as primary key. actually problem was different. I was made the database manually and the database didn't set the primary key. But now this time I make it with code and it's work well.
I write code to create table like this:

CREATE TABLE `smash` (
  `id` INT(255) NOT NULL AUTO_INCREMENT,
  `fname` VARCHAR(255) NOT NULL,
  `address` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4_general_ci;
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.