1

I'm setting up a small signup form that should be inserting the data to an existing table, return the unique ID and insert that value to a secondary table.

Currently, I presume that the the first query is not running because the unique ID returned is 0.

I've gone over this small portion of code, that works on all my other developments and can't seem to figure out where it's blocking.

mysqli_query ($db_conx, "INSERT INTO Users
     (first_name, last_name, dob, email, user_type, company, start_date, end_date, create_timestamp, last_modify_timestamp, create_matricule, pwd_hash)       
            VALUES
     ('$first_name','$last_name','$dob','$email','$user_type','$company','$start_date','$end_date','$create_timestamp','$create_timestamp','$create_matricule','$password_hash')");


    //Now select this user to retreive the matricule

    $matricule = mysqli_insert_id($db_conx);


    $sql2 ="INSERT INTO Permissions (matricule) VALUES ('$matricule')";
    $query2 = mysqli_query($db_conx, $sql2);

    if(!$query){
        echo json_encode(['message'=>"Error : ". mysqli_error($db_conx), 'code'=>500]);
    }

    echo json_encode(['message'=>"Matricule ".$matricule." created", 'code'=>200]);

}

The console returns the following :

Object { message: "Matricule 0 created", code: 200 }
javascript.js:125:13
success javascript.js:126:13
ReferenceError: matricule is not defined[Learn More]

So therefore the creation to the second table works just fine but not the first.

My database is configured as follows:

CREATE TABLE `Users` (
  `matricule` int(6) NOT NULL,
  `first_name` varchar(35) NOT NULL,
  `last_name` varchar(35) NOT NULL,
  `dob` date NOT NULL,
  `email` varchar(50) NOT NULL,
  `user_type` varchar(3) NOT NULL,
  `company` varchar(30) NOT NULL,
  `start_date` date NOT NULL,
  `end_date` date NOT NULL,
  `pwd_hash` varchar(500) NOT NULL,
  `pwd_reset_hash` varchar(100) NOT NULL,
  `otp_token` varchar(6) NOT NULL,
  `create_timestamp` datetime NOT NULL,
  `create_matricule` varchar(7) NOT NULL,
  `manager_matricule` varchar(7) NOT NULL,
  `sq_1` varchar(2) NOT NULL,
  `sa_1` varchar(50) NOT NULL,
  `sq_2` varchar(2) NOT NULL,
  `sa_2` varchar(50) NOT NULL,
  `last_modify_timestamp` datetime NOT NULL,
  `last_modify_matricule` varchar(7) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Thanks for any input!!

1
  • Note: do not concatenate strings to SQL queries. Use parameters. It will save you from a lot of headache Commented Jan 3, 2019 at 7:30

3 Answers 3

3

I think you mispelled some variable names.

You are fetching $query2 :

$query2 = mysqli_query($db_conx, $sql2);

then checks $query :

if(!$query){
    echo json_encode(['message'=>"Error : ". mysqli_error($db_conx), 'code'=>500]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

There is most probably an issued in the structure of you database or the JavaScript you haven't shared.

Comments

0

First, you must make the quotation marks as follows.

$sql="INSERT INTO Users (first_name) VALUES ('".$first_name."')";

Or you can use prepared statements

3 Comments

stackoverflow.com/questions/7137631/… The dots aren't necessary here though...
It also works the way he did it because the variable is inbetween the quotes.
It can work this way yes but it's not necessary... or not something that is causing an issue in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.