2016-03-30 14 views
-1

私のデータベースのusersテーブルのrankカラムを更新する際に、データをPHPフォームで表示し、ボタンを使って送信しています。しかし、一旦私のPHPフォームのデータを編集してsubmitを押すと、データベースのデータは変更されません。私はウェブページの写真へのリンクを追加しています。コードは以下に掲載されています。 Webpage imagePHPフォームでmysqliテーブルを更新する

<!DOCTYPE HTML> 
<html> 
<head> 
    <title>View Records</title> 
</head> 
<body> 
<?php 
/* 
    Displays all data from 'users' table 
*/ 
    // connect to the database 
    include('../db/connect.php'); 

    // get results from database 
    $result = $MySQLi_CON->query("SELECT * FROM users") 
     or die(mysql_error()); 

    // display data in table  
    echo "<table border='1' cellpadding='10'>"; 
    echo "<tr> <th>ID</th> <th>Username</th> <th>Email</th> <th>Rank</th> <th></th></tr>"; 

    // loop through results of database query, displaying them in the table 
    while($row = $result->fetch_array()) { 
     // echo out the contents of each row into a table 
    echo "<tr>"; 
    echo '<td>' . $row['user_id'] . '</td>'; 
    echo '<td>' . $row['username'] . '</td>'; 
    echo '<td>' . $row['email'] . '</td>'; 
    echo '<td><input type="hidden" name="user_id[]" id="newrank" width="20px" min="0" max="100" value="' . $row['user_id'] . '"></td>'; 
    echo '<td><form method="POST" action=""><input type="number" name="newrank[]" id="newrank" width="20px" min="0" max="100" value="' . $row['rank'] . '"></form></td>'; 
    echo '<td><a href="delete.php?id=' . $row['user_id'] . '">Delete</a></td>'; 
    echo "</tr>"; 
} 
// close table> 
echo "</table>"; 
if(isset($_POST['btn-update'])) { 
    for($i = 0; count($_POST["user_id"]); $i++) { 
     $_POST['newrank'][$i] = $MySQLi_CON->real_escape_string($_POST['newrank'][$i]); # If this function exists either, if not comment or remove this line! 
    $_POST['user_id'][$i] = $MySQLi_CON->real_escape_string($_POST['user_id'][$i]); # If this function exists either, if not comment or remove this line! 

    $MySQLi_CON->query('UPDATE users SET rank=' . $_POST['newrank'][$i] . ' WHERE user_id=' . $row['user_id'][$i] . ''); 

} 
echo "Updated the rows."; 
} 
?> 
<br> 

<button type="submit" class="btn btn-default" name="btn-update" id="btn-update">Update</button></a> 
<p><a href="ny.php">Add a new record</a></p> 

</body> 
</html> 
+0

あなたのコードは、SQL Injections' 'に完全に脆弱です。つまり、ハッカーは簡単にログインせずにデータベースからデータを取得できます。避ける方法を見てください:http://php.net/manual/en/mysqli.real-escape-string.php。第2の部分: 'while()'ループの外側で '$ row'変数にアクセスすることができないので、更新クエリをループする必要があります。 – Jer

+0

私はこれを調べます、ありがとうございます。 – Onret

+0

[何を試しましたか?](http://mattgemmell.com/what-have-you-tried/)と[ask]それらをplsを読んでください。 – Pred

答えて

0

あなたquery声明

にエラーこの変更があるようです:あなたは$ _POSTに変更したいIDを解析する必要がif ($MySQLi_CON->query($sql) === TRUE) {

+0

ご返信ありがとうございます。私はこれを変更しましたが、それでも動作しません。 – Onret

+0

@Onretあなたのフォームは提出していないと思います。 '

+0

私はそれを行いましたが、まだ動作していません。 – Onret

0

if ($$MySQLi_CON->query($sql) === TRUE) {

は、 。また、あなたのコードで<form action="" method="POST">を使用する必要があります。

はそれをテストしていませんでしたが、次のように動作するはずです:

<!DOCTYPE HTML> 
<html> 
<head> 
<title>View Records</title> 
</head> 
<body> 
<table border='1' cellpadding='10'> 
<thead> <th>ID</th> <th>Username</th> <th>Email</th> <th>Rank</th> <th colspan="3"></th></thead> 
<tbody> 

<?php 
//Displays all data from 'users' table 
// connect to the database 
include('../db/connect.php'); 

// get results from database 
$result = $MySQLi_CON->query("SELECT * FROM users") 
    or die(mysql_error()); 

// loop through results of database query, displaying them in the table 
while($row = $result->fetch_assoc()) { 
    // echo out the contents of each row into a table 
    ?> 
    <form action="" method="POST"> 
     <tr> 
      <td><?php echo $row['user_id']; ?></td> 
      <td><?php echo $row['username']; ?></td> 
      <td><?php echo $row['email'];?></td> 
      <td><input type="number" name="newrank" id="newrank" value="<?php echo $row['rank']; ?>"></td>   
      <td><input type="hidden" name="id" value="<?php echo $row['user_id']; ?>"><button type="submit" class="btn btn-default" name="btn-update" id="btn-update">Update</button></td> 
      <td><a href="delete.php?id=<?php echo $row['user_id']; ?>">Delete</a></td> 
     </tr> 
    </form> 
    <?php 
} 
?> 

</tbody> 
</table> 
<?php 

if(isset($_POST['btn-update'])) 
{ 
$sql = 'UPDATE users SET rank=' . $_POST['newrank'] . ' WHERE user_id=' . $_GET['id'] . ''; 

if ($MySQLi_CON->query($sql) === TRUE) { 
    echo "Record updated successfully"; 
} else { 
    echo "Error updating record: " . $MySQLi_CON->error; 
} 
} 
?> 

<p><a href="ny.php">Add a new record</a></p> 

</body> 
</html> 
+0

私が更新しようとすると、次のような結果が返ってきます: '未定義インデックス:44行目のid'' – Onret

+0

@Onret私たちがすべて答えを提案する前に、URLはどのように見えますか? 'edit.php?id = 1'のようなものか、URLにパラメータがありますか?一度にすべての行を更新しますか? – Jer

+0

すべての行を一度に更新することは、私が心に留めていたことです。 URLは '?id = 1'のない' edit.php'のように見えます。 – Onret

関連する問題