2015-10-20 57 views
0

嗨,我的問題是更新數據庫與查詢後,表單中的值將不會更改,即使相同的值(例如team1),在代碼的底部給了我更新的價值。要更新我必須刷新頁面的表單中的值。更新表單值,更新後在php查詢sql

<form action="" method="post" id="form-partite"> 
    Team Home: <input type="text" name="team1" value="<?php echo $team1 ?>"> 
    Team Away: <input type="text" name="team2" value="<?php echo $team2 ?>"> 
    <input type="submit" name="submit" id="submit"> 
</form> 
<?php 
    if (isset($_POST['submit'])) { 
     if ($_POST['team1'] == "" || $_POST['team2'] == ""){ 
      echo "Devi inserire entrambe le squadre"; 
     } else { 
      $team1 = $_POST['team1']; 
      $team2 = $_POST['team2']; 
      $data = array(
       'team1' => $team1, 
       'team2' => $team2 
      ); 
      global $wpdb; 
      $wpdb -> update(wp_partite, $data, array(id => '1')); 
      global $wpdb; 
      $partita = $wpdb -> get_row("SELECT team1, team2 FROM wp_partite"); 
      $team1 = $partita -> team1; 
      $team2 = $partita -> team2; 
      echo "Partita inserita: " . $team1 . " vs " . $team2 . "!"; 
      unset($_POST); 
     } 
     } 

回答

0

正如指出的路易斯,總是利用其varaibles之前調用PHP。 也不要取消設置$ _POST,因爲它是一個全局變量,不能被忽略。 取消全局變量是一種不好的做法。躲開它。

此外,在檢查$ _POST ['submit']之前定義$ team1和$ team2,以便它們始終可用並且不會給出未定義的變量錯誤。

使用修剪,以便您不希望用戶填寫表單中的空格。

雖然您可以在$ _POST內取消設置team1和team2的偏移量。

下面的代碼:

<?php 

    $team1 = $team2 = ''; // ADD THIS LINE, else form will show undefined variable error. 

    if (isset($_POST['submit'])) { 
     if (trim($_POST['team1']) == "" || trim($_POST['team2']) == ""){ // Modified 
      echo "Devi inserire entrambe le squadre"; 
     } else { 
      $data = array(
       'team1' => $_POST['team1'], // Modified 
       'team2' => $_POST['team2'] // Modified 
      ); 
      global $wpdb; 
      $wpdb -> update(wp_partite, $data, array(id => '1')); 
      global $wpdb; 
      $partita = $wpdb -> get_row("SELECT team1, team2 FROM wp_partite"); 
      $team1 = $partita -> team1; 
      $team2 = $partita -> team2; 
      echo "Partita inserita: " . $team1 . " vs " . $team2 . "!"; 
      unset($_POST['team1']); // Modified 
      unset($_POST['team2']); // Modified 
     } 
    } 
?> 
<form action="" method="post" id="form-partite"> 
    Team Home: <input type="text" name="team1" value="<?php echo $team1 ?>"> 
    Team Away: <input type="text" name="team2" value="<?php echo $team2 ?>"> 
    <input type="submit" name="submit" id="submit"> 
</form> 
+0

thanx以獲得有用的解釋! – stockw

+0

歡迎您!如果解決了您的問題,請接受答案。謝謝 –

0

相反呼應PHP之前的形式,做PHP後:

<?php 
    if (isset($_POST['submit'])) { 
     if ($_POST['team1'] == "" || $_POST['team2'] == ""){ 
      echo "Devi inserire entrambe le squadre"; 
     } else { 
      $team1 = $_POST['team1']; 
      $team2 = $_POST['team2']; 
      $data = array(
       'team1' => $team1, 
       'team2' => $team2 
      ); 
      global $wpdb; 
      $wpdb -> update(wp_partite, $data, array(id => '1')); 
      global $wpdb; 
      $partita = $wpdb -> get_row("SELECT team1, team2 FROM wp_partite"); 
      $team1 = $partita -> team1; 
      $team2 = $partita -> team2; 
      echo "Partita inserita: " . $team1 . " vs " . $team2 . "!"; 
      unset($_POST); 
     } 
    } 
?> 
<form action="" method="post" id="form-partite"> 
    Team Home: <input type="text" name="team1" value="<?php echo $team1 ?>"> 
    Team Away: <input type="text" name="team2" value="<?php echo $team2 ?>"> 
    <input type="submit" name="submit" id="submit"> 
</form> 
+0

感謝名單了很多路易斯 – stockw

+0

不客氣!請將問題設置爲回答@stefanotombari –