Jellevdschoot
Jellevdschoot

Reputation: 27

Insert data in mysql by html form (and more..)

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">

<?php

$con = mysql_connect("localhost","root","root");
if (!$con)
  {
   die('Could not connect: ' . mysql_error());
  }

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" />

    </form>

<!-- 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):

 <?php
    $con = mysql_connect("localhost","root","root");
    if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

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) 
                           VALUES 
                           ('$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..

Thanks!

Upvotes: 0

Views: 933

Answers (1)

Lock
Lock

Reputation: 5522

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

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.

Upvotes: 2

Related Questions