It is auto_increment

.
To make it more understandable, I'll give you the code with comments.
First the form, which is dynamically produced from the db.
Code: Select all
<html>
<head>
</head>
<form method="post" action="formular.php">
<?php
error_reporting(E_ALL);
//produce the form dynamically from the db, depending on the chosen "kurs":
$x = mysql_real_escape_string($_POST["kurs"]); //saves the chosen "kurs" in the variable $x
$abfrage = "SELECT $x FROM lehrende";
$ergebnis = mysql_query($abfrage) or die("MySQL-Fehler: " . mysql_error());
if (mysql_num_rows($ergebnis)) {
while($row = mysql_fetch_array($ergebnis))
{
$rname = $row[0];
echo "$rname
//producing the radiobuttons
<table>
<tr><td>Unterrichtsvorbereitung</td>
<td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"1\"></td>
<td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"2\"></td>
<td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"3\"></td>
<td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"4\"></td>
<td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"5\"></td>
<td><input type=\"radio\" name=\"Frage1[{$rname}]\" value=\"6\"></td>
</tr>
<tr><td>Fachliche Sicherheit</td>
<td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"1\"></td>
<td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"2\"></td>
<td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"3\"></td>
<td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"4\"></td>
<td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"5\"></td>
<td><input type=\"radio\" name=\"Frage2[{$rname}]\" value=\"6\"></td>
</tr>
</table>";
}
} else {
echo "No result for {$_POST['kurs']}<br />";
}
?>
<input type="hidden" name="name" value="<?PHP print ("$x"); ?>">
<input value=" Beurteilung absenden" type="submit">
</form>
</html>
And now how I write the results in the db.
Code: Select all
<?php
error_reporting(E_ALL);
$verbindung = mysql_connect ('localhost', 'root', '')
or die ("keine Verbindung möglich.
Benutzername oder Passwort sind falsch");
mysql_select_db("homepage")
or die ("Die Datenbank existiert nicht.");
echo '<pre>';
var_dump($_POST);
echo '</pre>';
$y = $_POST['name']; //$y not important for my problem
echo $y;
//if-statement, the limit is equal to the amount of questions to be asked
for ($i=1; $i<3; $i++ ) {
$zahl = "Frage".$i; //variable, depends on $i and belongs to the questions in the form (Frage1, Frage2,...) --> named as the columns in the db.
$array[$zahl] = $_POST[$zahl]; // saves the values from the form into an array ($array[Frage1], $array[Frage2],...)
foreach ($array[$zahl] as $key => $wert) {
echo "In '".$key."' is question '".$zahl."' the value '".$wert."'<br />\n"; //for example: In Name1 is Frage1 the value 3.
$Name = $key;
$Abteilung = $y;
$Wert = $wert;
//write in db: only if $i==1, because I want the names only to be written once.
if ($i == 1){
$query = "INSERT INTO personen (ID, Name, Abteilung) VALUES (NULL, '$key', '$y')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
//updating the values - for example:
//Name/ Frage1 / Frage2
//Name1 / 1 / 5
//Name2 / 2 / 3
//here is the problem, because ALL names (personen) are going to be UPDATED when a new form ist completed --> doesn't work with LAST_INSERT_ID() because only the last name is going to be UPDATED.
$aendern = "UPDATE personen SET $zahl = '$wert' WHERE Name = '$key'";
$ergebnis = mysql_query($aendern) or die('Query failed: ' . mysql_error());
echo $ergebnis;
}
}
mysql_close();
?>
And these are all POST-values:
array(3) {
["Frage1"]=>
array(3) {
["Name1"]=>
string(1) "2"
["Name2"]=>
string(1) "2"
["Name3"]=>
string(1) "3"
}
["Frage2"]=>
array(3) {
["Name1"]=>
string(1) "1"
["Name2"]=>
string(1) "1"
["Name3"]=>
string(1) "4"
}
As I mentioned, it principally works, but only if only one person completes the form. The others would overwrite the results.