如何在PHP

时间:2016-03-27 09:17:30

标签: php mysql database post

我需要一些帮助。我试图找出如何将特定查询(删除记录)与不是记录的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>

此处还有Insert 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;
}

1 个答案:

答案 0 :(得分:0)

这不够,但是,从你的回声开始:

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