dousuiben8395 2016-03-27 12:20
浏览 49
已采纳

从'table'中删除id ='selected record'?

I'm selecting records from a table with a Delete button in the echo line. I want to be able to delete the record when I click on the corresponding Delete button. This is my delete query to delete the selected record from the other query, but it won't work!

$querytwo = 'DELETE FROM paginas WHERE id= $_POST["id"]';   

I'm doing something wrong. What is it? Here is the full code.

    <?php

        include_once("config.php"); //this is the database connection
        $query = "SELECT * FROM paginas "; //selects from the table called paginas
        $result = mysqli_query($mysqli, $query);

            while($row = mysqli_fetch_assoc($result)) 
            {
            $pagetitle = $row['pagetitle'];
            $toevoeging = $row['toevoeging'];
            $message = $row['message'];

                echo '<article class="topcontent">
       <div class="mct">
        <h2>' . $pagetitle .'</h2>
       </div><br>
       <p class="post-info">'. $toevoeging . '</p>
       <p class="post-text"><br>'.$message.'</p>
      </article>
       <div class="deleteknop">
        <form method="post">';

   echo '<input type="hidden" name="id" value="'.$row['id'].'">
         <input name="delete" type="submit" value="Delete Now!">
        </form>
       </div>' ;
            }

        $querytwo = 'DELETE FROM paginas WHERE id= $_POST["id"]';

        if (isset($_POST['delete'])) //Deletes the query if 'delete' button is clicked

            {
                $resulttwo = $mysqli->query($querytwo);  
            }

            ?>

Got it working. I changed the line to $querytwo = "DELETE FROM paginas WHERE id= $_POST[id]"; It's functional. Although there was a warning notice saying “Notice: Undefined index” but I got rid of it thanks to error_reporting(0);

  • 写回答

3条回答 默认 最新

  • doushanmo7024 2016-03-27 15:56
    关注

    You need to use double quotes for variable interpolation to work. With single quotes, the exact string you see is what is getting sent as the database query, which is not a valid query. Something like this would actually substitute the value of the POST variable in the string:

    $querytwo = "DELETE FROM paginas WHERE id = $_POST[id]"
    

    However, interpolating variables directly into a query string is a bad idea as it can result in a SQL injection vulnerability. I would advise looking up parameterized queries.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改