4

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';
        }
?>
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Remus Afrem
  • 87
  • 1
  • 1
  • 7
  • 1
    This `'%$search%'` isn't a placeholder. http://php.net/manual/en/pdo.prepared-statements.php I don't know if that works or not if it does it is opening you to injections because it is not being parameterized. – chris85 Jul 16 '15 at 21:44
  • mr chris85 i am having problem whit delete data not search appreciate the time and its just a hobby i do it all for knowledge – Remus Afrem Jul 16 '15 at 21:49
  • The SQL for the delete query will be the same regardless of whether you are using pdo, mysqli, mysql, or whatever. But there does not appear to be a delete query in your code, only a select. This is part of why you are having trouble deleting. – Don't Panic Jul 16 '15 at 21:53
  • Well you're 3/4 of the way there why not finish off doing it properly? Where are you running a delete I don't see that anywhere? – chris85 Jul 16 '15 at 21:53
  • right so i don`t have a delete function on it thx all – Remus Afrem Jul 16 '15 at 22:01

2 Answers2

10

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));                
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Juan Serrats
  • 1,358
  • 5
  • 24
  • 30
  • 1
    just before i am 100% sure do i need to set this on a new file or can i use it on the same page and thx a loot – Remus Afrem Jul 16 '15 at 22:03
  • You can do it on the same page, you'll need to update all the $variables with your data: $dbHost, $dbName, $dbUsername, $dbUserPassword, $table and $id (if you want to delete based on the id) Good luck! :) – Juan Serrats Jul 16 '15 at 22:05
0
It's work for me.

$id = $_GET['id'];
$conn->exec("DELETE FROM mrbs_entry WHERE id = $id");
Omar Faruk
  • 298
  • 5
  • 19
  • 1
    Any explanation with this snippet dump? This looks topical: [Difference between PDO->query() and PDO->exec()](https://stackoverflow.com/q/16381365/2943403) – mickmackusa Nov 30 '22 at 06:46