1

This is my code:

$uid=$_POST['uid'];
$fields = '';
if($uid==1){    
//having some error in this line
   foreach($query as $key => $value) {
    if ($i++ != 0) $fields .= ', '; 
    $key = mysql_real_escape_string($key);
      $value = mysql_real_escape_string($value);
      $fields .= "$key = $value";
      }
$query = mysql_query("UPDATE 2mcom SET $fields");
   }



  echo "<table>" ;
$result = mysql_query("select * from 2mcom where roll_no='138218600004'");
$i=1;

while (false != ($data = mysql_fetch_array($result, MYSQL_ASSOC)))
  foreach ($data as $key => $value)
  //echo "$key: $value <br />";
  {
  echo "<tr><td>";
    echo "$key: ";
  echo "</td>";
    echo '<form method="post" action="">';
    echo "<td>";
    echo '<input type="text" name="fld'.$i.'" value="'.$value.'"/>';
    echo '<input type="hidden" name="uid" value="1"/>';
     $i++;
     echo "</td></tr>";
  }
  echo "<tr><td>";
   echo'<input type="submit" name="submit" value="submit"/>';

  echo "</tr></td>";
    echo'</form></table>';

I want to update this table 2mcom from the input text-box. Here i am fetching the field-name along with their values in input field from table "2mcom" using array.. now i want to update the field after editing.it is for result correction .

11
  • Could you be a little more specific about what you're trying to do? Commented Sep 16, 2014 at 8:35
  • same as normal update.... because ur trying update text Commented Sep 16, 2014 at 8:38
  • @AmritaGupta try first. try to code simple update Commented Sep 16, 2014 at 8:47
  • The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database in newer versions of PHP (>5.5). Instead, use the MySQLi or PDO_MySQL extensions. Commented Sep 16, 2014 at 8:48
  • @Prashant: I have tried but its giving error for $fld= $_POST["fld'.$i.'"] Commented Sep 16, 2014 at 8:52

1 Answer 1

1

This should give your text field a name, then you can do separate UPDATE queries to update each field individually.

UPDATE, consolidated version of previous code:

$i = 0;
while($row = mysqli_fetch_assoc($result)) {
    foreach($row as $key => $value) {
        $name = 'field';
        $id = $row['id']; //rename this with your row id, this will give you identifiable input names
        echo '<tr><td>';
        echo $key . ': ';
        echo '</td>';
        echo '<form method="POST">';
        echo '<td>';
        echo '<input type="hidden" name="id[]" value="'. $row['id'] .'"/>';
        echo '<input type="hidden" name="column[]" value="'. $key .'"/>';
        echo '<input type="text" name="field[]" value="'. $value .'"/>';
        echo '</td></tr>';
    }
    $i++;
}

UPDATE QUERY:

function update_table($table, $col, $value, $id) {

    global $link; //mysqli object

    //escape values
    $value = mysqli_real_escape_string($link, $value);
    $col = mysqli_real_escape_string($link, $col);

    //update the database field
    $query = "UPDATE `$table` SET `$col` = '$value' WHERE `roll_id` = '$id'";
    mysqli_query($link, $query) or die(mysqli_error($link));

    return true;

}

CONTROLLER:

if(isset($_POST['submit'])) {

    $col_count = count($_POST['column']);
    $field_count = count($_POST['field']);

    if($field_count == $col_count) {
    } else { 
        trigger_error("Col - Field mismatch", E_USER_ERROR);
    }

    for($i=0; $i<count($_POST['column']); $i++) {
        if(!update_table($table, $_POST['column'][$i], $_POST['field'][$i], $_POST['id'][$i])) {
            trigger_error("Error updating field", E_USER_ERROR);
        }
    }

}

Try that.

Sign up to request clarification or add additional context in comments.

10 Comments

$row[$ref] should be $row[$ref[$i]]
:i need a update query in php this code is giving error Warning: Illegal offset type
Its printing only the 1st fieldname @Edward
$id = $row['id']; its showing error as Undefined index: id @Edward
@AmritaGupta I just spent my time writing this for you, you could at least read it! //rename this with your row id, this will give you identifiable input names
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.