【问题标题】:How to associate query with changing variable in PHP如何将查询与 PHP 中的更改变量相关联
【发布时间】:2016-03-27 09:17:29
【问题描述】:

我需要一点帮助。我试图找出如何将特定查询(删除记录)与记录的 id 相关联,而不是与另一个查询(记录的选择)回显的记录相关联。

这行代码在指定 id 时完全有效,但我再次需要它用于被调用的记录,如果我删除记录,id 可以跳过数字。

    $querytwo = "DELETE FROM `paginas` WHERE id = 5"; 

我的 phpmyadmin 数据库中有一个表,其中包含“id”、“pagetitle”、“toevoeging”(荷兰语中的添加)、“message”列。第一个是 INT,其余的是 varchars/text。

这可能是一个愚蠢的问题,我很抱歉。我对 PHP 和一般编程仍然很陌生。

这里是代码。我已经评论了行代码以澄清。谢谢!。

<?php 
        if (isset($_SESSION['email'])) //if the admin is active, forms can be written out.
        {
        echo '</nav>
          <br><br>  <div class="inlogscript">
         <form action="verstuurd.php" method="post">

            <input type="text" placeholder="Titel" method="POST" name="pagetitle" /><br><br>
            <input type="text" placeholder="Toevoeging" method="POST" name="toevoeging" /><br><br>
            <textarea class="pure-input-1-2" placeholder="Wat is er nieuws?" name="message"></textarea><br>
            <input type="submit" value="Bevestigen" />
        </form></div>'; 
        }       
        ?>  
    <div class="mainContent">

<?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">
            <input name="delete" type="submit" value="Delete Now!">
            </form>' . '</div>' ;
        } //This long echo will call variables $pagetitle, $toevoeging and &message along with divs so they automatically CSS styled, 
         //along with a Delete button per echo that has the 3 variables

    $querytwo = "DELETE FROM `paginas` WHERE id = 5";  
    if (isset($_POST['delete'])) //Deletes the query if 'delete' button is clicked

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

        ?>
            </div>
        </div>

这里还有记录的插入 INTO 查询。再次感谢!

$sql = "INSERT INTO paginas (pagetitle,toevoeging, message)
VALUES ('$_POST[pagetitle]','$_POST[toevoeging]','$_POST[message]')";
//the insertion into the table of the database

if ($MySQLi_CON->query($sql) === TRUE) {
    echo "";
} else {
    echo "Error: ". $sql . "" . $MySQLi_CON->error;
}

【问题讨论】:

  • 又应该与什么相关联?
  • 需要编写此代码,以便知道选择了哪个查询。 "从paginas 中删除 id = 5";喜欢:“从 'paginas' 中删除 id = $idofthepost”;但是 $idofthepost 不起作用。我需要用它做点别的事情,但我就是不知道是什么。

标签: php mysql database post


【解决方案1】:

这还不够,但是,从你的 echo 开始:

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">';
         // you ll want to use $_POST["id"] array to delete :
   echo '<input type="hidden" name="id" value="'.$row['id'].'">
         <input name="delete" type="submit" value="Delete Now!">
        </form>
       </div>' ;

【讨论】:

  • 这带来了一个未定义的变量错误,我需要如何声明$row?
  • 对不起,我忘记了 html name 属性,我刚刚编辑了它。现在$_POST['id'] 将不再是未定义的
  • 好的,谢谢。我还没有到那里,但我离得更近了,谢谢。
猜你喜欢
  • 2020-11-02
  • 2020-03-15
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多