[SOLVED] overwriting data in db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

knallbernd
Forum Commoner
Posts: 30
Joined: Mon Apr 23, 2007 4:39 am

Post by knallbernd »

Let's say I have chosen one radiobutton from 5 questions. Then this one will be written in the db --> I don't want that.
I only want to write something (or do some other script) when EVERY button is set, no matter the name or what question.
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

You can validate in javascript then or:

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

}
you can put my validation into a function for better look
knallbernd
Forum Commoner
Posts: 30
Joined: Mon Apr 23, 2007 4:39 am

Post by knallbernd »

Still the same problem...

If only one radiobutton is selected and the others not, this one will be written in the db --> but there should be no
value be written in the db, except if ALL radiobuttons are selected. Javascript is too insecure for this purpose :-(.
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

let me see your code now - my validation should solve the problem
knallbernd
Forum Commoner
Posts: 30
Joined: Mon Apr 23, 2007 4:39 am

Post by knallbernd »

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;


}
}
}
Is this the way you meant it?
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

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
knallbernd
Forum Commoner
Posts: 30
Joined: Mon Apr 23, 2007 4:39 am

Post by knallbernd »

Thanks, I will try that. If I don't manage to fix it I'm hopefully allowed to post again ;-).
Have a nice day!
knallbernd
Forum Commoner
Posts: 30
Joined: Mon Apr 23, 2007 4:39 am

Post by knallbernd »

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

Post by knallbernd »

Sorry, me again. I'm now sitting since yesterday and I don't manage do validate my radiobuttons via php. It would be really great if someone could help me out...
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Try

Code: Select all

<input type=\"radio\" name=\"Frage1[]\" value=\"1\">
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\" checked>
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

use what you have already:

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;
}
make sure you send $kurs in hidden field
knallbernd
Forum Commoner
Posts: 30
Joined: Mon Apr 23, 2007 4:39 am

Post by knallbernd »

Now I'm even more confused...
Do you put that after the echo "rname? And why $kurs in a hidden field - that shouldn't matter to the validation.
Sorry...
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

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
knallbernd
Forum Commoner
Posts: 30
Joined: Mon Apr 23, 2007 4:39 am

Post by knallbernd »

You mean that I put this function into "formular.php" (the site the form is send to)? But why du you SELECT everything again?
Maybe you could paste the into the existing code because I don't understand it right now :(
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

You select everything again because you want to compare if values that have been sent match values that were in the form.
This is the best way to check if everything was selected.
Post Reply