so I am playing around in PHP. I am by no means an expert, but would consider myself a bit beyone the hello world type programs.
Right now I am trying to make a simple generic editor for Database entries, which has two parts, the part which brings up the edit form:
in this, each input field is given a name of EditVariable_$i,
now on the receiving end of this, I have no luck accessing the $EditVariable_$i variable.
e.g.
I can't say I am terribly surprised that $EditVariable_$i does not work as expected, but is there a way to do this, as in combine a variable with a string to create the name of the second variable, e.g if I wanted it to access the data in $EditVariable_1 ?
also, if this is a retarded way to do things, please point me in the right direction for how such database edits would be done, without hard coding the name of each database field
Thanks in advance
Tjalfe
Right now I am trying to make a simple generic editor for Database entries, which has two parts, the part which brings up the edit form:
function displayeditDB($Database, $ID) {
include 'db.php';
$SQLSTRING= "SELECT * FROM $Database WHERE ID='$ID'";
$result1 = mysql_query("$SQLSTRING")or die("error accessing Database");
$NumFields = mysql_num_fields($result1);
echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
echo "<TABLE border=1>";
echo "<TR><TD colspan=3 align=center bgcolor=#aaaaaa>Edit Record for $Database, ID[$ID]</TD></TR>";
while ($myrow1 = mysql_fetch_row($result1)) {
for($i=1;$i<$NumFields;$i++) {
$name=mysql_field_name($result1,$i);
echo "<TR><td>$name</td><td><input name='EditVariable_$i' value=\"$myrow1[$i]\"></td></TR>\n";
}
}// end while
//echo "<TR><TD> Date </TD><TD><input type=text name=Date value=\"$TodaysDate\" size=15></TD></TR>\n";
echo "<input type=hidden name=Action value=EditRecord>";
echo "<input type=hidden name=Database value=$Database>";
echo "<input type=hidden name=ID value=$ID>";
echo "<input type=hidden name=NumFields value=$NumFields>";
echo "<tr><td>Locate Production/Repair</td><td align=center><input type=submit value=Find name=B3></form></td></tr>";
echo "</TABLE>";
}
include 'db.php';
$SQLSTRING= "SELECT * FROM $Database WHERE ID='$ID'";
$result1 = mysql_query("$SQLSTRING")or die("error accessing Database");
$NumFields = mysql_num_fields($result1);
echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
echo "<TABLE border=1>";
echo "<TR><TD colspan=3 align=center bgcolor=#aaaaaa>Edit Record for $Database, ID[$ID]</TD></TR>";
while ($myrow1 = mysql_fetch_row($result1)) {
for($i=1;$i<$NumFields;$i++) {
$name=mysql_field_name($result1,$i);
echo "<TR><td>$name</td><td><input name='EditVariable_$i' value=\"$myrow1[$i]\"></td></TR>\n";
}
}// end while
//echo "<TR><TD> Date </TD><TD><input type=text name=Date value=\"$TodaysDate\" size=15></TD></TR>\n";
echo "<input type=hidden name=Action value=EditRecord>";
echo "<input type=hidden name=Database value=$Database>";
echo "<input type=hidden name=ID value=$ID>";
echo "<input type=hidden name=NumFields value=$NumFields>";
echo "<tr><td>Locate Production/Repair</td><td align=center><input type=submit value=Find name=B3></form></td></tr>";
echo "</TABLE>";
}
now on the receiving end of this, I have no luck accessing the $EditVariable_$i variable.
e.g.
if($Action=='EditRecord') {
echo "SELECT * FROM $Database WHERE ID='$ID'";
$SQLSTRING= "SELECT * FROM $Database WHERE ID='$ID'";
$result1 = mysql_query("$SQLSTRING")or die("error accessing Database");
$NumFields = mysql_num_fields($result1);
for($i=1;$i<$NumFields;$i++) {
$Fieldname=mysql_field_name($result1,$i);
$SQLSTRING ="UPDATE $Database set $Fieldname=$EditEntry_$i where ID = '$ID' " or die (" error updating $Fieldname Amount on $Database");
echo "$SQLSTRING <br>";
}
}
echo "SELECT * FROM $Database WHERE ID='$ID'";
$SQLSTRING= "SELECT * FROM $Database WHERE ID='$ID'";
$result1 = mysql_query("$SQLSTRING")or die("error accessing Database");
$NumFields = mysql_num_fields($result1);
for($i=1;$i<$NumFields;$i++) {
$Fieldname=mysql_field_name($result1,$i);
$SQLSTRING ="UPDATE $Database set $Fieldname=$EditEntry_$i where ID = '$ID' " or die (" error updating $Fieldname Amount on $Database");
echo "$SQLSTRING <br>";
}
}
also, if this is a retarded way to do things, please point me in the right direction for how such database edits would be done, without hard coding the name of each database field
Thanks in advance
Tjalfe
Comment