Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

php - How to delete data from sql with pdo?

Hello I have search the web all day trying to find example of deleting data when I use pdo but all i found its MySQL and MySQLi and I am stuck i can't see what I am missing when I run it it give me /search.php?del=Array(id). This its my code please help

<?php 
//load database connection
    $host = "localhost";
    $user = "abcd";
    $password = "******";
    $database_name = "abcd";
    $pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ));
// Search from MySQL database table




$search=$_POST['search'];
$query = $pdo->prepare("select * from wfuk where post_code LIKE '%$search%' OR telephone LIKE '%$search%'  LIMIT 0 , 10");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
?>
<html>
<head>
<title> How To Create A Database Search With MySQL & PHP Script | Tutorial.World.Edu </title>
</head>
<body>
<form action="search.php" method="post">
Search: <input type="text" name="search" placeholder=" Search here ... "/>
<input type="submit" value="Submit" />
</form>
<?php
         if (!$query->rowCount() == 0) {
 echo "Search found :<br/>";
echo "<table style="font-family:arial;color:#333333;">";
                echo "<tr>
<td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">First Name</td>
<td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">Last Name</td>
<td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">Trade</td>
<td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">Post Code</td>
<td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">Telephone</td>
<td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">Comments</td>
<td style="border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;">To be use</td></tr>";
            while ($results = $query->fetch()) {

echo "<tr><td style="border-style:solid;border-width:1px;border-color:#98bf21;">";
                echo $results['first_name'];

echo "</td><td style="border-style:solid;border-width:1px;border-color:#98bf21;">";
                echo $results['last_name'];

echo "</td><td style="border-style:solid;border-width:1px;border-color:#98bf21;">";
                echo $results['trade'];

echo "</td><td style="border-style:solid;border-width:1px;border-color:#98bf21;">";
                echo $results['post_code'];

echo "</td><td style="border-style:solid;border-width:1px;border-color:#98bf21;">";
                echo $results['telephone'];

echo "</td><td style="border-style:solid;border-width:1px;border-color:#98bf21;">";
                echo $results['comments'];

echo "</td><td style="border-style:solid;border-width:1px;border-color:#98bf21;">";
echo $results['id'];
                //this its my button for delete
echo("<button onclick="location.href='search.php?del=$results(id)'">delete user</button>"); 
                
echo "</td></tr>";
            }
echo "</table>";

        } else {
            echo 'Nothing found';
        }
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would start with something like this:

    $con =  new PDO( "mysql:host=".$dbHost.";"."dbname=".$dbName, $dbUsername, $dbUserPassword); 
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "DELETE FROM `table` WHERE id = ?";        
    $q = $con->prepare($sql);

    $response = $q->execute(array($id));                

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...