Take the 2-minute tour ×

First of all: I'm new into MYSQL/PHP :). I'm working on a simple script to learn php/mysql, but I'm stuck with several problems. I'm really sorry if some of these are already asked on this website, but I DID search them, but they didn't work in combination with my intentions, so please don't just -1 this post!

I've got a database with some tables. One of the tables is named 'leerlingen' and I want to add data into that table. I'm from the Netherlands, "leerlingen" means "students". So, I'd like to add a student into the table. The table has 4 things: leerling_id, leerling_naam, leerling_nummer and klas_id. The last one is weird, right? Nope, because I've got another table with just one column: klas_id. That's for later, though.

'Klas' means 'group' (yep, I'm learning you guys Dutch haha :D), and I've got several groups. But, you can just add a new group by another form.

Let's get to business: I've created a HTML form:

<!-- start form -->

    <form action="insert_leerling.php" method="post">
<input type="text" name="leerling_naam" class="inp-form">
<input type="text" name="leerling_nummer" class="inp-form">


if (!$con)

mysql_select_db("sms", $con);

 $query = "SELECT * FROM klas ORDER BY klas_id";
 $result = mysql_query($query);

echo "<select class='inp-form' id='klas_id'>\n";

while ($row = mysql_fetch_assoc($result)) {
  if (1 == 1) {
    echo '<option name="'.htmlspecialchars($row['klas_id']).'">'.htmlspecialchars($row['klas_id']).'</option>';

echo "</select>";

            <input type="submit" value="Voeg toe" class="form-submit" />


<!-- end form  -->

So, there are 3 things inserted: leerling_naam (means student name), leerling_nummer (means student number = mobile phone number) and klas_id. I'm getting the klas_id from the little php script, so the user sees a html dropdown menu with all the klas (groups). When I run the form, it sends you to this piece of code (another page):

    if (!$con)

mysql_select_db("sms", $con);

$leerling_naam = $_POST['leerling_naam'];
$leerling_nummer = $_POST['leerling_nummer'];
$klas_id = $_POST['klas_id'];

$sql_insert = mysql_query("
                           INSERT INTO leerlingen 
                           (leerling_naam, leerling_nummer, klas_id) 
                           ('$leerling_naam', '$leerling_nummer', '$klas_id')


Now, here are the problems..

  1. The 'klas_id' isn't inserted into the table, it's just blanco.
  2. I want the first column (leerling_id, that's the normal id from a mysql table) to be +1 the whole time, I'm not using that piece of code. When I add new data, the leerling_id IS going +1, but when I'm removing the first row (let's say I kicked a student), the data isn't dating itself up (haha, the word 'updating')

If you've read all the way trough here: well done. My English is not that good (if you find some dramatic grammar mistakes, you're allowed to tell me :) ). I also understand that this was quite hard to follow, sorry for that..


share|improve this question
Please write less and simply make your question known. I am sure the problem does not need this much narration. –  bivoc Dec 23 '12 at 22:42
Although quite wordy, I thought the questions were quite known. –  Lock Dec 23 '12 at 22:51
@ TCPN - Ok, I'll work on that. @ Lock - Thought it was more difficult, sorry. –  Jellevdschoot Dec 23 '12 at 22:57
add comment

1 Answer

up vote 2 down vote accepted

Should read

echo "<select name='klas_id' class='inp-form' id='klas_id'>\n";

You didn't give your select form element a name. Id and Name are not interchangeable in this situation.

In answer to your second question, why do you want the data to be completely sequential? There are no easy ways to make the database records always sequential, especially if this is the primary key of the row.

Rather than delete records from the database, I would suggest that this is a good case for "soft record deletion", that is, add a new column to your table called 'active' with a data type of ENUM('Y','N'). If you want to delete them, simply mark this field as 'N', and when querying for students, only look for records with a 'Y' in the active column.

If you want to return your students with a sequential number in your query, look at a rownumber: @rownum:=@rownum+1 although this number will have absolutely no direct link to the id in the database.

share|improve this answer
Thanks! I'll work on my way of telling my problem, sorry for the long text.. Anyway, thanks man, Merry Christmas! –  Jellevdschoot Dec 23 '12 at 22:55
add comment

Your Answer


