Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert data in mysql by html form (and more..)
    text
    copied!<p>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!</p> <p>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.</p> <p>'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.</p> <p>Let's get to business: I've created a HTML form:</p> <pre><code>&lt;!-- start form --&gt; &lt;form action="insert_leerling.php" method="post"&gt; &lt;input type="text" name="leerling_naam" class="inp-form"&gt; &lt;input type="text" name="leerling_nummer" class="inp-form"&gt; &lt;?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 "&lt;select class='inp-form' id='klas_id'&gt;\n"; while ($row = mysql_fetch_assoc($result)) { if (1 == 1) { echo '&lt;option name="'.htmlspecialchars($row['klas_id']).'"&gt;'.htmlspecialchars($row['klas_id']).'&lt;/option&gt;'; } } echo "&lt;/select&gt;"; ?&gt; &lt;input type="submit" value="Voeg toe" class="form-submit" /&gt; &lt;/form&gt; &lt;!-- end form --&gt; </code></pre> <p>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):</p> <pre><code> &lt;?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') "); ?&gt; </code></pre> <p>Now, here are the problems..</p> <ol> <li>The 'klas_id' isn't inserted into the table, it's just blanco.</li> <li>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')</li> </ol> <p>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.. </p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload