3

This is my delete page :

<?php 
require('includes/config.php'); 

    $id = $_GET['ID'];
    $pdoConnect = new PDO($db);
    $query='DELETE * FROM studentraspored WHERE ID = "' . $id . '" ';
    $pdoResult = $db->prepare($query);

    $pdoExec = $pdoResult->execute($query);

    header('location:index.php');
?>

This is generated table in my “memberpage.php”:

if (count($rows)){
    foreach ($rows as $row) {       
        $_SESSION['row'] = $rows;
        $id = floatval($row['ID']);
        echo "<tr>" .
            '<form action="delete_raspored.php" method="post">'.
            "<td>" . $row["ID"] . "</td>" .
            "<td>" . $row["den"] . "</td>" .
            "<td>" . $row["chas"] . "</td>" .
            "<td>" . $row["predmet"] . "</td>" .
            "<td>" . $row["profesor"] . "</td>" .               
            "<td>" . $row["prostorija"] . "</td>" .
            "<td>" . $row["tip"] . "</td>" .
            '<td><input type="submit" id="' . $id . '" value="Delete" ></td>'.
            "</form>".
            "</tr>"

This not working properly. I don't understand why maybe something i missed with floatval

9
  • 1
    Maybe. You should be using an integer for your ID, rather than a float. You may not be getting a 1/1 match when your run your query. Commented Feb 2, 2016 at 14:44
  • Hmm might have to update answer, what is $db and where is it defined? Commented Feb 2, 2016 at 15:02
  • @chris85 $db is database conn defined in config.php Commented Feb 2, 2016 at 15:09
  • Okay, that's what I though, so you don't need $pdoConnect = new PDO($db);, I'll update answer. Commented Feb 2, 2016 at 15:11
  • @ValentinGjorgoski Does the answer below work? Commented Feb 2, 2016 at 16:41

1 Answer 1

1

Start by trying this:

<?php 
require('includes/config.php'); 
$id = $_GET['ID'];
$query='DELETE FROM studentraspored WHERE ID = ?';
$pdoResult = $db->prepare($query);
$pdoResult->execute(array($id));
header('location:index.php');
exit();

Note the placeholder in place of the actual value, this will prevent SQL injections. The value is passed in in the execute, or you could bind it (http://php.net/manual/en/pdostatement.bindparam.php). http://php.net/manual/en/pdo.prepared-statements.php

The delete syntax was also off, delete deletes a whole row not specific columns, http://dev.mysql.com/doc/refman/5.7/en/delete.html.

In your form I also don't see an element named ID so that could be another issue and your form is submitting via POST, not GET.

Sign up to request clarification or add additional context in comments.

Comments

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.