2016-03-30 14 views
0

wie ich bin Neuling mich freundlich verzeihen PHP, wenn ich dumm aussehen,PHP Formular Update ohne Logout

ich ein Formular in PHP erstellt, während ich das Update Teil des Formulars das Update spiegelt in db während im Form es zeigt immer noch den gleichen alten Wert. Ich habe versucht, aktualisieren und erzwingen aktualisieren, aber nichts ändert sich.

Während ich mich abmelde und erneut anmelde, zeigt das Formular den aktualisierten Wert an.

Ich versuchte mit die(); nach mysql_close($link);, aber es meldet sich die Sitzung und muss neu anmelden.

Bitte helfen Sie mir bei der Anzeige der Änderungen, während ich noch im Login bin.

Mein Code ist wie folgt:

<?php 
     if(isset($_POST['update'])) { 

      $name_a = $_POST['name']; 
      $email_a = $_POST['email']; 
      $pass_a = $_POST['password']; 

      $sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' "; 
      $retval = mysql_query($sql,$link); 

      if(! $retval) { 
       die('Could not update data: ' . mysql_error()); 
      } 
      echo "Updated data successfully\n"; 

      mysql_close($link); 

     }else { 
      ?> 
<!-- Widget: user widget style 1 --> 
      <div class="box box-widget widget-user-2"> 
      <!-- Add the bg color to the header using any of the bg-* classes --> 
      <div class="widget-user-header bg-yellow"> 
       <div class="widget-user-image"> 
       <?php echo '<img src="' . $img . '" class="img-circle" alt="User Image">'; ?> 
       </div> 
       <!-- /.widget-user-image --> 
       <h3 class="widget-user-username"><?php echo "$name"; ?></h3> 
       <h5 class="widget-user-desc"><?php echo "$role"; ?></h5> 
      </div> 
      <div class="box-footer no-padding"> 
       <form role="form" method = "post" action = "<?php $_PHP_SELF ?>"> 
       <div class="box-body"> 
       <div class="form-group"> 
        <label for="exampleInputName1">Name</label> 
        <input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo "$name"; ?>"> 
       </div> 
       <div class="form-group"> 
        <label for="exampleInputEmail1">Email address</label> 
        <input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo "$email"; ?>"> 
       </div> 
       <div class="form-group"> 
        <label for="exampleInputPassword1">Password</label> 
        <input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo "$password"; ?>"> 
       </div> 


       </div> 
       <!-- /.box-body --> 

       <div class="box-footer"> 
       <button type="submit" name="update" id="update" class="btn btn-primary">Submit</button> 
       </div> 
      </form> 
      </div> 
      </div> 
      <!-- /.widget-user --> 
     <?php 
     } 
     ?> 
+0

einige Code des Programms bitte. –

+0

Code anzeigen, wird es helfen, das Problem zu finden –

+0

@Fraynekonok Code hinzugefügt –

Antwort

0

Wie @DivyeshSavaliya im Kommentar erwähnt ist die Frage,

I didn‘ t Verwendet Wählen Sie die Abfrage nach dem Update aus. Ist das erledigt, dass das Problem

Der neue Arbeits Code gelöst ist

<?php 
     if(isset($_POST['update'])) { 

      $name_a = $_POST['name']; 
      $email_a = $_POST['email']; 
      $pass_a = $_POST['password']; 

      $sql = "UPDATE admin SET a_name = '$name_a', a_email = '$email_a', password = '$pass_a' where aid='$update_id' "; 
      $retval = mysql_query($sql,$link); 

      if(! $retval) { 
       die('Could not update data: ' . mysql_error()); 
      } 



     } 


    $result = mysql_query("SELECT * FROM admin where aid='$update_id' ",$link); 
    while($row = mysql_fetch_array($result)){ 

     $name = $row['a_name']; 
     $email = $row['a_email']; 
     $password = $row['password']; 

    } 
    mysql_close($link); 
      ?> 

Dank @DivyeshSavaliya

1

Try this:

<?php 
$name = ''; 
$email = ''; 
$password = ''; 
$update_id = ''; 
//$img = ''; 
//$role = ''; 
//$link = null; 

if(
isset($_POST['update']) && 
isset($_POST['id']) && 
isset($_POST['name']) && 
isset($_POST['email']) && 
isset($_POST['password']) 
) { 
    $update_id = mysql_real_escape_string($_POST['id']); 
    $name = mysql_real_escape_string($_POST['name']); 
    $email = mysql_real_escape_string($_POST['email']); 
    $password = mysql_real_escape_string($_POST['password']); 

    $sql = 'UPDATE admin SET a_name = \'' . $name . '\', a_email = \'' . $email . '\', password = \'' . $password . '\' WHERE aid = \'' . $update_id . '\''; 
    $result = @mysql_query($sql, $link); 

    if(!$result) 
     die('Could not update data: ' . mysql_error($link)); 

    echo 'Updated data successfully', "\n"; 
} 
elseif(isset($_GET['id'][0])) { 
    $update_id = mysql_real_escape_string($_GET['id']); 
    $sql = 'SELECT a_name,a_email,a_password FROM admin WHERE aid = \'' . $update_id . '\''; 

    $result = @mysql_query($sql, $link); 
    if($result) { 
     $result = mysql_fetch_row($result); 
     $name = $result[0]; 
     $email = $result[1]; 
     $password = $result[2]; 
    } 
    else { 
     echo 'Could not find the id.' . "\n"; 
     $update_id = ''; 
    } 
} 

unset($result); 

if(isset($update_id[0])) { 
    mysql_close($link); 
?> 
<!-- Widget: user widget style 1 --> 
<div class="box box-widget widget-user-2"> 
    <!-- Add the bg color to the header using any of the bg-* classes --> 
    <div class="widget-user-header bg-yellow"> 
     <div class="widget-user-image"> 
      <img src="<?php echo htmlspecialchars($img); ?>" class="img-circle" alt="User Image"> 
     </div> 
     <!-- /.widget-user-image --> 
     <h3 class="widget-user-username"><?php echo htmlspecialchars($name); ?></h3> 
     <h5 class="widget-user-desc"><?php echo htmlspecialchars($role); ?></h5> 
    </div> 
    <div class="box-footer no-padding"> 
     <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST"> 
      <input type="hidden" name="id" value="<?php echo htmlspecialchars($update_id); ?>"> 
      <div class="box-body"> 
       <div class="form-group"> 
        <label for="exampleInputName1">Name</label> 
        <input type="text" class="form-control" id="exampleInputName1" name="name" value="<?php echo htmlspecialchars($name); ?>"> 
       </div> 
       <div class="form-group"> 
        <label for="exampleInputEmail1">Email address</label> 
        <input type="email" class="form-control" id="exampleInputEmail1" name="email" value="<?php echo htmlspecialchars($email); ?>"> 
       </div> 
       <div class="form-group"> 
        <label for="exampleInputPassword1">Password</label> 
        <input type="password" class="form-control" id="exampleInputPassword1" name="password" value="<?php echo htmlspecialchars($password); ?>"> 
       </div> 
      </div> 
      <!-- /.box-body --> 

      <div class="box-footer"> 
       <button type="submit" name="update" id="update" class="btn btn-primary">Submit</button> 
      </div> 
     </form> 
    </div> 
</div> 
<!-- /.widget-user --> 
<?php } 
else { 
    $sql = 'SELECT aid,a_name FROM admin'; 
    $result = @mysql_query($sql, $link); 

    if($result) { 
     while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
      echo '<a href="?id=' . $row['aid'] . '">' . $row['a_name'] . '</a><br />' . "\n"; 
     } 
    } 
    mysql_close($link); 
} 
?> 
+0

es zeigt keine Diff – Affan

1
SOLUTION 
    1) use the updated value like $name_a instead of $name because $name_a contain updated value and $name contain old value 
    2) reload page after update and get new value from database on page load and store that value in $name , $email etc variable (if new data update successfully in database then only you get new value) 
    3) if You store your data in session or cookie then update session and cookie value also when you update in database