2017-02-22 12 views
-1

のいずれかが削除されますので、どのように私は、テーブル要素の一つだけを削除することができますが、私のコード、私はそれをクリックした場合、それはここでのエントリー

<?php 
require '../db/dbcon.php'; 
$sql="SELECT ClientId,Email,FirstName,LastName,Password,PhoneNumber,AdressID from client;"; 
$sth = $dbh->prepare($sql); 
$sth->execute(); 
foreach($sth as $row){ 
    echo "<tr>"; 
    echo "<td>".$row['FirstName']."</td>"; 
    echo "<td>".$row['LastName']."</td>"; 
    echo "<td>".$row['Password']."</td>"; 
    echo "<td>".$row['Email']."</td>"; 
    echo "<td>".$row['PhoneNumber']."</td>"; 
    echo "<td></td>"; 
    echo "<td></td>"; 
    echo "<td></td>"; 
    echo "<td></td>"; 
    $clientid=$row['ClientId']; 
    echo "<form action=\"viewclients.php\" method=\"post\"> "; 
    echo "<td><input type=\"submit\" name=\"fshij\" value=\"Fshij\"></td>"; 
    echo "</form>"; 
    if(isset($_POST['fshij'])){ 
     $sql = "DELETE FROM client WHERE ClientId=".$clientid.";"; 
     $sth = $dbh->prepare($sql); 
     $sth->execute(); 
     header('location:viewclients.php'); 
    } 
    echo "</tr>"; 
} 
?> 

ですこれらのエントリのいずれかを削除すると、すべてのエントリが削除されます。

+0

あなたがループ – Naincy

+0

内で削除クエリを実行しているとしては、それはすべての単一の繰り返しで、削除を実行します。 –

+0

"をクリックするとどういう意味ですか? –

答えて

1

まず、ClientIdの値をボタンの値に配置します。

次に、削除コードをスクリプトの上部までループ外に移動します。 `ISSET($ _ POST [ 'fshijが'])` trueに検証している場合

<?php 
require '../db/dbcon.php'; 

if(isset($_POST['fshij'])){ 

    // use a parameterised query here as you are passing user data 
    // to this query 
    $sql = "DELETE FROM client WHERE ClientId=?"; 
    $sth = $dbh->prepare($sql); 

    // if MYSQLI 
    $sth->bind_param("i", $_POST['fshij']); 
    $sth->execute(); 

    // if PDO   
    $sth->execute(array($_POST['fshij'])); 

    header('location:viewclients.php'); 
} 

$sql="SELECT ClientId,Email,FirstName,LastName, 
      Password,PhoneNumber,AdressID 
      from client"; 
// you could have done this query using ->query() as there are no parameters to prepare 
$sth = $dbh->prepare($sql); 
$sth->execute(); 
foreach($sth as $row){ 
    echo "<tr>"; 
    echo "<td>".$row['FirstName']."</td>"; 
    echo "<td>".$row['LastName']."</td>"; 
    echo "<td>".$row['Password']."</td>"; 
    echo "<td>".$row['Email']."</td>"; 
    echo "<td>".$row['PhoneNumber']."</td>"; 
    echo "<td></td>"; 
    echo "<td></td>"; 
    echo "<td></td>"; 
    echo "<td></td>"; 

    echo "<td>"; 
    echo "<form action=\"viewclients.php\" method=\"post\"> "; 
     echo "<input type='submit' name='fshij' value='{$row['ClientId']}'>"; 

    echo "</form>"; 
    echo "</td>"; 
    echo "</tr>"; 
} 
?> 
+0

コメントを残すことはできますが、回答を編集してakyouを追加することはできません – RiggsFolly

関連する問題