2017-10-21 7 views
-6

PHPのレコードを削除するには、テーブルの各行の横にあるボタンを使用します。私はどこにでも良いチュートリアルを見つけることができませんが、私の問題に私を助けることができ、私は本当に立ち往生して何をすべきかわからない。PHPでレコードを削除するにはMySQLデータベース

<!DOCTYPE html> 
<html> 
<head> 
<title>PCSS Grad Gown</title> 
<link rel="stylesheet" type="text/css" href="Grad_Gown_Report.css"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> 
</script> 

<style> 

    body { 
     background-image: url("http://www.miguelmontalban.com/wp-content/uploads/2015/11/uSr9bZA-fancy-background-images.jpg"); 
    } 

    td { 
     color: white; 
    } 

    .Grad_Table { 
     color :white; 
    } 
</style> 
</head> 
<body> 

<?php 
    $servername = "Private"; 
    $username = "Private"; 
    $password = "Private"; 
    $dbname = "Private"; 


    $conn = new mysqli($servername, $username, $password, $dbname); 
    // Check connection 
    if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 

    $order; 
} 
    $order = $_GET['order']; 
    if (!$order) { 
     $order = "name"; 
    } 

    $sql = mysqli_query ($conn, "SELECT * FROM Information ORDER By $order"); 

    ?> 

    <div class="table-responsive" id="grad_table"> 

     <br> 
     <br> 

    <table class="Grad_Table" width="100%" border="12px"> 
     <thead> 
      <tr> 
       <td><a href="Grad_Gown_Report.php">Name:</a></td> 
       <td><a href="Grad_Gown_Report_2.php">Student #:</a></td> 
       <td>Homeform #:</td> 
       <td>Unit:</td> 
       <td>Height:</td> 
       <td>Size:</td> 
       <td></td> 



      </tr> 

     </thead> 

     <tbody> 
      <?php 

      while ($rows = mysqli_fetch_assoc($sql)) { 
      ?> 

      <tr> 
    <td><?php echo $rows['Name'];?></td> 
    <td><?php echo $rows['Studentnum'];?></td> 
    <td><?php echo $rows['Homeform'];?></td> 
    <td><?php echo $rows['Unit'];?></td> 
    <td><?php echo $rows['Height'];?></td> 
    <td><?php echo $rows['Size'];?></td> 









      </tr> 

      <?php 


      } 


      ?> 
     </tbody> 


    </table> 

    </div> 




</div> 
</body> 
</html> 

私はまさに私がそれをする方法を完全にデータベースからのデータと他のすべての機能を取得することができます:誰かが何をすべきか私に言うことができる場合

は、ここに私のコードです。私が必要とするのは、削除機能だけです。私はW3 Schoolsのチュートリアルを含む多くのことを試しましたが、オブジェクト指向と手続き的な方法を試しましたが、どちらも私に同じエラーを与えます。私は本当に別のdelete.phpファイルを好むでしょうが、うまく動作するものであれば問題ありません。

+1

ます。https://のstackoverflowを。 com/search?q = delete + record + PHP –

答えて

0

IDでエントリを削除するPHPスクリプトを作成し、テーブル行を作成するときに、指定したIDを持つスクリプトへのリンクを出力します。

// Using your code as a reference 
while($rows = mysqli_fetch_assoc($sql)) : ?> 
    <tr> 
    <td> 
     <a href="/somepath/delete_entry.php?entry_id=<?php echo $row['Id']; ?>">Delete Entry</a> 
    </td> 
    </tr> 
endwhile; 

のように、削除スクリプトが何かになります:あなたはしかし注意する必要があります

<?php 
    // Perform any user auth here ... 

    // Connect to mysql here ... 

    if(!isset($_GET['entry_id'])) { 
    die('You need to provide an ID'); 
    } 

    $id = $_GET['entry_id']; 

    // Ensure the ID is an integer or an int-like string 
    if(!ctype_digit("$id")) { 
    die('Invalid ID provided'); 
    } 

    // We've verified that if $id is a string, it is an int-like string; otherwise, it is actually an integer. 
    // Let's convert it to an actual integer 
    $id = (int) $id; 

    // At this point, if you have users and users own this resource, you might check to make sure they actually have permission to delete the requested resource. 

    $query = "DELETE FROM Information WHERE Id = $id;"; 
    $mysqli->query($query); 

    // If this is a rest API, you can just end with HTTP 200 (let the script die); 
    // otherwise, redirect them to the page they should see after performing this action 
    header('location: /view_something.php'); 

- 私はあなたがユーザーの入力を取り、あなたに直接入力していることをあなたのコードに気づきますSQLクエリ。これにより、アプリケーションをSQL injection attacksで簡単にターゲティングできます。特別に細工されたHTTPリクエストの1つで、データベース全体を削除する可能性があります。

簡単に言うと、ユーザーから受け取った文字列入力を検証してエスケープして、アプリケーションの権限で追加のタスクを実行する独自のSQLを追加しないようにする必要があります。

mysqliを使用しているので、mysqli_real_escape_string()をチェックしてください。

PDO/prepared statementを検索したり、これを自動的に行うフレームワークを採用したりすることがあります。


最後に、「バニラPHPの方法」を実行すると、コードの保守とテストが非常に難しくなります。私がチェックアウトすることをお勧め:

  • Laravel(または任意の他の近代的なPHPフレームワーク)
  • REST APIを
  • オブジェクト指向PHPの開発
  • PHP作曲
+0

と、IDでエントリを削除するスクリプトをどのように作成するのですか? –

+0

@AsadKhan - 私の答えを更新しました –

+0

私は理由はわかりませんがまだ動作しません。 –

関連する問題