[SOLVED] overwriting data in db
Moderator: General Moderators
-
knallbernd
- Forum Commoner
- Posts: 30
- Joined: Mon Apr 23, 2007 4:39 am
You can validate in javascript then or:
you can put my validation into a function for better look
Code: Select all
$valid = true;
for ($i=1; $i<3; $i++ ) {
$zahl = "Frage".$i;
$array[$zahl] = $_POST[$zahl];
foreach ($array[$zahl] as $key => $wert) {
if (empty($wert)) {
$valid = false;
break;
}
}
}
if ($valid) {
// your loops with inserts and update
}-
knallbernd
- Forum Commoner
- Posts: 30
- Joined: Mon Apr 23, 2007 4:39 am
-
knallbernd
- Forum Commoner
- Posts: 30
- Joined: Mon Apr 23, 2007 4:39 am
Code: Select all
$valid = true;
for ($i=1; $i<3; $i++ ) {
$zahl = "Frage".$i;
$array[$zahl] = $_POST[$zahl];
foreach ($array[$zahl] as $key => $wert) {
if (empty($wert)) {
$valid = false;
break;
}
}
}
if ($valid) {
for ($i=1; $i<3; $i++ ) {
$zahl = "Frage".$i;
$array[$zahl] = $_POST[$zahl];
foreach ($array[$zahl] as $key => $wert) {
$Name = $key;
$Abteilung = $y;
$Wert = $wert;
if ($i==1){
$query = "INSERT INTO personen (ID, Name, Abteilung) VALUES (NULL, '$key', '$y')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
$aendern = "UPDATE personen SET $zahl = '$wert' WHERE Name = '$key' AND $zahl = ''";
$ergebnis = mysql_query($aendern) or die('Query failed: ' . mysql_error());
echo $ergebnis;
}
}
}argh ... my script is checking against $_POST which is wrong
This is obvious that everything that has been POSTed will be set
we should look for values that haven't been POSTed
you can do it by adding all names into the hidden field in the form and then do validation against that values (probably an array)
or you have to use sql query to take all names from the database and then check if they exists in POST
This is obvious that everything that has been POSTed will be set
we should look for values that haven't been POSTed
you can do it by adding all names into the hidden field in the form and then do validation against that values (probably an array)
or you have to use sql query to take all names from the database and then check if they exists in POST
-
knallbernd
- Forum Commoner
- Posts: 30
- Joined: Mon Apr 23, 2007 4:39 am
-
knallbernd
- Forum Commoner
- Posts: 30
- Joined: Mon Apr 23, 2007 4:39 am
Ok, i don't get it. I'm too silly to create a simple (in this case maybe a bit more difficult) form validation, I think I'm sitting too long behind this script. I just want to validate if every radiobutton ist set. Else the form shouldn't be send.
Code: Select all
<html>
<head>
</head>
<form method="post" action="formular.php">
<?php
error_reporting(E_ALL);
mysql_connect("localhost","root","") or die ("Keine Verbindung moeglich");
mysql_select_db("homepage") or die ("Die Datenbank existiert nicht");
$x = mysql_real_escape_string($_POST["kurs"]);
$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
<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>
<br>
</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>-
knallbernd
- Forum Commoner
- Posts: 30
- Joined: Mon Apr 23, 2007 4:39 am
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
Try with the other options the same. Added empty [] and test the return vals.
To retain values you need to add
Code: Select all
<input type=\"radio\" name=\"Frage1[]\" value=\"1\">To retain values you need to add
Code: Select all
<input type=\"radio\" name=\"Frage1[]\" value=\"1\" checked>use what you have already:
make sure you send $kurs in hidden field
Code: Select all
function validateForm() {
$x = mysql_real_escape_string($_POST["kurs"]);
$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];
for ($i=1; $i<3; $i++ ) {
$zahl = "Frage".$i;
$array[$zahl] = $_POST[$zahl];
if (empty($array[$zahl][$rname])) {
// not all values are set - return false
return false;
}
}
}
}
// form validated all values set
return true;
}-
knallbernd
- Forum Commoner
- Posts: 30
- Joined: Mon Apr 23, 2007 4:39 am
You put my function after you send the form to validate if everything was filled correctly
But you have to send "kurs" to the second page (the best way is to use a hidden field)
And if validate function returns true you can add values to the database
If it returns false you just display error message and "back" button to the form
But you have to send "kurs" to the second page (the best way is to use a hidden field)
And if validate function returns true you can add values to the database
If it returns false you just display error message and "back" button to the form
-
knallbernd
- Forum Commoner
- Posts: 30
- Joined: Mon Apr 23, 2007 4:39 am