2016-05-12 28 views
0

我厭倦了這段代碼。真。我昨天一直在努力解決這個問題,但這只是不起作用。我會appriciate如果有人會給他們的投入。用一個提交點擊更改數據庫值PHP

我發現了一個很棒的功能老CMS。但是,該CMS是從2008年開始的,現在仍在MySQL上運行,並且不再進行更新。所以我拿出了幾個腳本。一個是能夠調整多個數據庫行onoff只需點擊一下提交按鈕。

數據庫看起來像這樣。

值表示開啓或關閉。在荷蘭它是aanuit。我正在製作一個荷蘭CMS,所以我大部分都是用荷蘭語創建的,所以我的客戶也可以理解它。

來自2008 CMS的代碼如下所示:http://pastebin.com/EkmB8rFr(我將它粘貼到Pastebin中,因爲它是凌亂的代碼,因此對於此代碼太小)。

所以我的工作是將這個2008代碼轉換爲2016代碼,所有的SQL注入都是固定的。這是我的結果:

<?php 
    if(isset($_POST['opslaan'])) { 
     $stmt = $dbConnection->prepare('SELECT * FROM settings'); 
     $stmt->execute(); 
     $result = $stmt->get_result(); 
     while ($row = $result->fetch_assoc()) { 
      $id = $_POST[$row["id"]]; 
      $row = $row["id"]; 
      $stmt1 = $dbConnection->prepare('UPDATE settings SET value = ? WHERE id = ?'); 
      $stmt1->bind_param('ss', $id, $id); 
      $stmt1->execute(); 
     } 

     //header('Location: index.php?page=instellingen'); 
    } else { 
     $stmt = $dbConnection->prepare('SELECT * FROM settings ORDER BY id ASC'); 
     $stmt->execute(); 

     $result = $stmt->get_result(); 
    ?> 
    <form method="POST" action=""> 
     <table> 
     <tr> 
      <th>Naam</th> 
      <th>Laatst gewijzigd</th> 
      <th>Waarde</th> 
     </tr> 
     <?php 
    while ($row = $result->fetch_assoc()) { 
    ?> 
     <tr> 
      <td><?php echo $row["code"]; ?></td> 
      <td><?php echo $row["updated"]; ?></td> 
      <td> 
       <select name="<?php echo $row["id"]; ?>"> 
        <option value="aan"<?php if($row["value"] == "aan") {echo ' selected';} ?>>Aan</option> 
        <option value="uit"<?php if($row["value"] == "uit") {echo ' selected';} ?>>Uit</option> 
       </select> 
      </td> 
     </tr> 
    <?php } ?> 
     </table> 
    <input type="submit" value="Opslaan" name="opslaan"> 
    </form> 
    <?php 
} 

此代碼只是不像2008年的代碼,我只是不知道發生了什麼事情。 錯誤在於它將ID放入數據庫中,而不是<select>的值。


輸出應該是這樣的:

+0

這個問題isthe'$ stmt1-> bind_param( 'SS',$ ID,$ ID);' – 2016-05-12 22:13:55

+0

@Dagon得到它固定的,感謝您的輸入袞! –

回答

1

看起來有些事情是在你更新混合起來。我認爲這應該更好的工作:

while ($row = $result->fetch_assoc()) { 

    $id = $row["id"];      // get the id from the row 
    $value = $_POST[$id];     // find the corresponding POST value 

    $stmt1 = $dbConnection->prepare('UPDATE settings SET value = ? WHERE id = ?'); 

    $stmt1->bind_param('ss', $value, $id); // Bind the correct variables here 
    $stmt1->execute(); 
} 
+1

不要告訴我不要恐慌,如果我想要ahrhrherhrharharhjeahrakwrkescrjmeuicyhtuiawmtlch,恐慌, – 2016-05-12 22:19:00

+0

啊,看起來像是固定的問題。我想我昨天不能回去,但你只是在我的晚上。謝謝,不要驚慌! –

+0

@Dagon這只是一個建議,而不是需求。 :) –