Reputation: 97
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!!
Upvotes: 1
Views: 57
Reputation: 463
There is most probably an issued in the structure of you database or the JavaScript you haven't shared.
Upvotes: 0
Reputation: 1150
First, you must make the quotation marks as follows.
$sql="INSERT INTO Users (first_name) VALUES ('".$first_name."')";
Or you can use prepared statements
Upvotes: 0
Reputation: 1639
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]);
}
Upvotes: 3