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';
        }
?>

5
  • 1
    This '%$search%' isn't a placeholder. 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. Commented Jul 16, 2015 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 Commented Jul 16, 2015 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. Commented Jul 16, 2015 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? Commented Jul 16, 2015 at 21:53
  • right so i don`t have a delete function on it thx all Commented Jul 16, 2015 at 22:01

2 Answers 2

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));                
Sign up to request clarification or add additional context in comments.

2 Comments

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
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! :)
0
It's work for me.

$id = $_GET['id'];
$conn->exec("DELETE FROM mrbs_entry WHERE id = $id");

1 Comment

Any explanation with this snippet dump? This looks topical: Difference between PDO->query() and PDO->exec()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.