Reputation: 1
I'm new to PHP, MySQL and working on an project for my school. I need to make a form that inserts new students into a database.
I need to have an primary key named studentnummer
so I can use this later on, but this needs to be created in the database table not in the form. When I try to insert the data of the form in the table I'll get an error saying I need to insert data in table row 1 (this is the primary key which is AI and an INT)
I've got the following for PHP and MySQL:
require ("connection.php");
//gets the data from the form
$voornaam = $_REQUEST['voornaam'];
$tussenvoegsel = $_REQUEST['tussenvoegsel'];
$achternaam = $_REQUEST['achternaam'];
$geboortedatum = $_REQUEST['geboortedatum'];
$woonplaats = $_REQUEST['woonplaats'];
$straat = $_REQUEST['straat'];
$huisnummer = $_REQUEST['huisnummer'];
$postcode = $_REQUEST['postcode'];
$telefoonnummer = $_REQUEST['telefoonnummer'];
$mobielnummer = $_REQUEST['mobielnummer'];
$email = $_REQUEST['email'];
$voor = $_REQUEST['voor'];
$motivatie = $_REQUEST['motivatie'];
$alt = $_REQUEST['alt'];
$tbl_name ="studenten";//db table name
$sql_ins = mysql_query("INSERT INTO $tbl_name values('??studentnummer??','$voornaam', '$tussenvoegsel', '$achternaam', '$geboortedatum', '$woonplaats', '$straat', '$huisnummer', ' $postcode' , '$telefoonnummer', '$mobielnummer', '$email', '$voor', '$motivatie', '$alt')");
This is a printscreen from my table stucture. If anyone knows what I can do about this problem I would appreciate it very much!!!
Thanks.
Upvotes: 0
Views: 122
Reputation: 889
Please try this-
$sql_ins = mysql_query("INSERT INTO $tbl_name values('','$voornaam', '$tussenvoegsel', '$achternaam', '$geboortedatum', '$woonplaats', '$straat', '$huisnummer', ' $postcode' , '$telefoonnummer', '$mobielnummer', '$email', '$voor', '$motivatie', '$alt')");
Upvotes: 0
Reputation: 360702
If you insert a NULL
into an auto_increment primary key field, mysql will supply the value for you, so
INSERT INTO $tbl_name VALUES(NULL, blah blah blah)
and then you can retrieve the generated value with
$studentnummer = mysql_insert_id();
Note that you shouldn't be using the mysql_*() functions. They're deprecrated. Consider switching to mysqli or PDO
Upvotes: 2