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());
  }
}

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.