Page 1 of 1

Can't find index from form [Solved]

Posted: Mon Apr 23, 2007 4:46 am
by knallbernd
Hello!
I really need help concerning the following problem:
I get an array form a DB.

Code: Select all

$x = mysql_real_escape_string($_POST["kurs"]); 

$abfrage = "SELECT $x FROM lehrende"; 
$ergebnis = mysql_query($abfrage) or die("MySQL-Fehler: " . mysql_error()); 
while($row = mysql_fetch_array($ergebnis)) 
{   
   $rname = $row[0];
   $rFrage = $rname."Frage";
   echo "$rname
  <table>
   <tr><td>Frage</td> 
   <td><input type=\"radio\" name=\"$rFrage\" value=\"1\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"2\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"3\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"4\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"5\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"6\"></td>
   </tr></table>";
   $rnameall = $rnameall."+".$rname;
}
echo "<input type=\"hidden\" name=\"namesall\" value=\"$rnameall\">";
Can't find the variable $rnameall???
Notice: Undefined variable: rnameall in C:\Programme\xampp\htdocs\test\include.php on line 37

And the second problem is:

Code: Select all

$namesall = $_POST['namesall'];
$allenamenarray = explode("+", $namesall);
foreach($allenamenarray as $name){
 echo "Name: ".$name;
 $fieldname = $name."Frage"; 
 $wert = $_POST[$fieldname];
 echo "Wert: ".$wert;
}
Notice: Undefined index: Frage in C:\Programme\xampp\htdocs\test\formular.php on line 20

That's the main problem because I need the value from "Frage" to write it in a DB. I don't manage to define "Frage" the right way...
It would be great if you could help me!

Posted: Mon Apr 23, 2007 7:03 am
by CoderGoblin
Initially you have no value for $rnameall so it cannot find it. Simple solution is to stick $rnameall=""; before the loop.

The easiest way to debug your second problem is to do a print_r or var_dump of the $_POST variable to check what you are actually getting. This should highlight what you are actually getting so you can check it is what you are expecting.

Posted: Mon Apr 23, 2007 7:59 am
by knallbernd
array(4) {
["Name1Frage"]=>
string(1) "3"
["Name2Frage"]=>
string(1) "3"
["Name3Frage"]=>
string(1) "6"
["namesall"]=>
string(55) "+Name1+Name2+Name3"
}

I'm getting this by echo '<pre>';
var_dump($_POST);.

But I don't manage to get the right workflow to put this values into the database...

Posted: Mon Apr 23, 2007 8:08 am
by CoderGoblin
It would appear that the first element of your $allenamearray is empty as you have ''+'var'+'var'+'var' etc... Try

Code: Select all

$namesall = $_POST['namesall'];
$allenamenarray = explode("+", trim($namesall,'+'));
foreach($allenamenarray as $name) {
 echo "Name: ".$name;
 $fieldname = $name."Frage";
 $wert = $_POST[$fieldname];
 echo "Wert: ".$wert;
}
and see if that solves the problem

Posted: Mon Apr 23, 2007 8:16 am
by knallbernd
Not really, now it says:

Code: Select all

array(4) {
  ["Name1Frage"]=>
  string(1) "3"
  ["Name2Frage"]=>
  string(1) "1"
  ["Name3Frage"]=>
  string(1) "2"
  ["namesall"]=>
  string(55) "+Name1+Name2+Name3"
}
Name: Name1

Notice:  Undefined index:  Name1Frage in C:\Programme\xampp\htdocs\test\formular.php on line 16

Wert: Name: Name2

Notice:  Undefined index:  Name2Frage in C:\Programme\xampp\htdocs\test\formular.php on line 16

Wert: Name: Name3

Notice:  Undefined index:  Name3Frage in C:\Programme\xampp\htdocs\test\formular.php on line 16

Wert:

Posted: Mon Apr 23, 2007 8:26 am
by CoderGoblin
Errm... Try this.. Won't solve the problem but I am trying to track it down a bit more by checking for additional spaces or anything in the index (shouldn't be but don't know why it isn't finding the index bearing in mind the dump of the $_POST provided...).

Code: Select all

$namesall = $_POST['namesall'];
$allenamenarray = explode("+", trim($namesall,'+'));
foreach($allenamenarray as $name) {
  echo "Name: ".$name;
  $fieldname = $name."Frage";
  if (empty($_POST[$fieldname])) {
    echo "Cannot find [{$fieldname}]<br />";
  } else {
    $wert = $_POST[$fieldname];
    echo "Wert: ".$wert;
  }
}

Posted: Mon Apr 23, 2007 8:39 am
by knallbernd
As expected:

Name: Name1Cannot find [Name1Frage]

And there is still the problem that $rnameall can't be found.

Posted: Mon Apr 23, 2007 8:47 am
by CoderGoblin

Code: Select all

$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];
     $rFrage = $rname."Frage";
     echo "$rname
  <table>
   <tr><td>Frage</td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"1\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"2\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"3\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"4\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"5\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"6\"></td>
   </tr></table>";
     $rnameall = $rnameall."+".$rname;
  }
  echo "<input type=\"hidden\" name=\"namesall\" value=\"$rnameall\">"; 
} else {
  echo "No result for {$_POST['kurs']}<br />";
}
OK try this first, should highlight/solve your no $rnameall problem

Posted: Mon Apr 23, 2007 8:58 am
by knallbernd
Unfortunately it's still the same problem.
I get the right values from 'kurs' (and they are shown the right way), but I don't manage to define $rnameall.

Posted: Mon Apr 23, 2007 9:06 am
by CoderGoblin
Ok I'm also going to crazy looking at this here... Can you repost the code you have now.

Posted: Mon Apr 23, 2007 9:13 am
by knallbernd
Sorry...

include_test.php (the whole code)

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];
     $rFrage = $rname."Frage";
     $rnameall = $rnameall."+".$rname;
     echo "$rname
  <table>
   <tr><td>Frage</td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"1\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"2\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"3\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"4\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"5\"></td>
   <td><input type=\"radio\" name=\"$rFrage\" value=\"6\"></td>
   </tr></table>";
     
  }
  echo "<input type=\"hidden\" name=\"namesall\" value=\"$rnameall\">";
} else {
  echo "No result for {$_POST['kurs']}<br />";
}

?>
<input value=" Beurteilung absenden" type="submit">
</form>
</html>
formular.php:

Code: Select all

<?php
 echo '<pre>';
  var_dump($_POST);

  $namesall = $_POST['namesall'];
$allenamenarray = explode("+", trim($namesall,'+'));
foreach($allenamenarray as $name) {
  echo "Name: ".$name;
  $fieldname = $name."Frage";
  if (empty($_POST[$fieldname])) {
    echo "Cannot find [{$fieldname}]<br />";
  } else {
    $wert = $_POST[$fieldname];
    echo "Wert: ".$wert;
  }
}
?>

Posted: Mon Apr 23, 2007 9:35 am
by CoderGoblin
OK I'm going to rewrite using a different method for the form.. Can you see if this works. (SQL user/password need to be changed from XXXX)

Code: Select all

<html>
<head>
</head>
<form method="post" action="formular.php">
<?php
error_reporting(E_ALL);

mysql_connect("localhost","xxxx","xxxx") 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>Frage</td>
   <td><input type=\"radio\" name=\"Frage[{$rname}]\" value=\"1\"></td>
   <td><input type=\"radio\" name=\"Frage[{$rname}]\" value=\"2\"></td>
   <td><input type=\"radio\" name=\"Frage[{$rname}]\" value=\"3\"></td>
   <td><input type=\"radio\" name=\"Frage[{$rname}]\" value=\"4\"></td>
   <td><input type=\"radio\" name=\"Frage[{$rname}]\" value=\"5\"></td>
   <td><input type=\"radio\" name=\"Frage[{$rname}]\" value=\"6\"></td>
   </tr></table>";
     
  }
} else {
  echo "No result for {$_POST['kurs']}<br />";
}
?>

Code: Select all

<?php
echo '<pre>';
var_dump($_POST);
echo '</pre>';
if (!empty($_POST['Frage'])) {
  if ((is_array($_POST['Frage'])) && (count($_POST['Frage']))) {
    foreach ($_POST['Frage'] as $name=>$value) {
      echo "Name: {$name} <br />";
      echo "Wert: ".$value;
    }
  } else {
    echo "Passed value not valid";
  }
} else {
  echo "No responses";
}
?>

Posted: Mon Apr 23, 2007 9:41 am
by knallbernd
Ich glaub's nicht! IT WORKS!

Vielen vielen Dank!

Posted: Mon Apr 23, 2007 9:48 am
by CoderGoblin
You are welcome... Hopefully you understand what I changed...

It's always easier handling a single array rather than an array with another variable containing indexes etc.

You should also note the addition of some additional checking which you need to change to german and decide how you want to handle the cases. Bear in mind that you should NEVER TRUST USER INPUT. Even the formular.php could be passed strange values by someone hacking hence the check to see if 'Frage' is an array.