Announcement

Collapse
No announcement yet.

PHP Question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP Question

    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:

    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>";
    }
    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.
    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>";
    }
    }
    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
    We have enough youth - What we need is a fountain of smart!


    i7-920, 6GB DDR3-1600, HD4870X2, Dell 27" LCD

  • #2
    It's been a bit since I've done PHP but I believe you should be making EditVariable an array:

    Code:
    <input name='EditVariable[]' value=\"$myrow1[$i]\">
    Then you can access EditVariable like an array with a for/for each loop.
    Gigabyte GA-K8N Ultra 9, Opteron 170 Denmark 2x2Ghz, 2 GB Corsair XMS, Gigabyte 6600, Gentoo Linux
    Motion Computing M1400 -- Tablet PC, Ubuntu Linux

    "if I said you had a beautiful body would you take your pants off and dance around a bit?" --Zapp Brannigan

    Comment


    • #3
      works beautifully.. thanks
      We have enough youth - What we need is a fountain of smart!


      i7-920, 6GB DDR3-1600, HD4870X2, Dell 27" LCD

      Comment

      Working...
      X