添加到mysql并在同一页面上删除

时间:2017-03-03 23:50:11

标签: php mysql forms post

有我的脚本

require('dbcon2.php');


  if(isset($_GET['submit1'])) { 
        if(isset($_POST['post_autor']) && isset($_POST['post_tresc'])) {

            mysqli_query($connect,"INSERT INTO news (tresc, autor) VALUES ('$_POST[post_tresc]', '$_POST[post_autor]')");

        }

  } elseif(isset($_GET['submit2'])) {

        if(isset($_POST['post_id_news2'])) {
            $usun = $_POST['post_id_news2'];

            mysqli_query($connect,"DELETE FROM news WHERE id_news = ".$usun."");
        }   else {
            echo 'Proba usunieca postu o pustym id.';
        }

    }

我想在一个页面上执行此操作,然后添加删除(呵呵)它没关系,但我想删除选项并且没有任何作用,零错误我只是按输入而没有任何事情发生。

HTML:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
    <head>
        <meta charset="utf-8">

        <style type="text/css">
            .cztery {
                    background: pink;
                    height:705px; 
                    width:40%;
                    float:right;
                }
            .czteryipol {
                    background: red;
                    height:705px; 
                    width:40%;
                    float:right;
                }
            </style>
    </head>
    <body> 
        <div class="cztery">
            <h1 align="center">
                <a href="html/news_podg.php" target="_blink">
                    PODGLĄD
                </a>
            </h1>
            <table border="5"  bordercolor="#a64dff" align="center" style="max-width:20px;">
                <?php
                    $result = mysqli_query($connect,"SELECT autor,id_news FROM news GROUP BY id_news");
                    while($row = mysqli_fetch_array($result)) 
                    { 
                    echo '<tr><td>'.$row['id_news'].'</td><td>'.$row['autor'].'</td></tr>';
                    }
                ?>
            </table>
        </div>
        <div class="czteryipol">
            <h1 align="center">
                EDYCJA
            </h1>
             <form action="kwadrat.php?go=czesc" method="post" id="usrform" align="center">

                <h2>
                    Autor<br> <input type="text" size="20" name="post_autor"/>
                </h2>


                <h2>
                    Dodaj artykuł: 
                </h2>
                <textarea name="post_tresc" align="center" form="usrform"></textarea><br>

            <input name="submit1" type="submit" value="OK"/><br>
        </form>

         <form action="kwadrat.php?go=czesc" method="post" id="usrform" align="center">
            <h1 align="center">
                Usuń artykuł
            </h1>   
            <h2>
                    Numer artykułu<br> <input type="text" size="5" name="post_id_news2"/>
            </h2>   
            <input name="submit2" value="OK" type="submit"/><br>  
        </form>
        </div>
    </body>
</html>

如果有人能告诉我我的错误在哪里,我将非常感激

最终编辑:

感谢您的帮助,我解决了这个问题:

而不是

($_GET['submit1']) 

DO

($_POST['submit1'])

3 个答案:

答案 0 :(得分:0)

if(isset($_POST['post_id_news2'])) { //<- Here is your culprit I think $usun = $_POST['post_id_news'];

您正在检查post_id_news2(结尾处的注释2)参数,然后尝试使用post_id_news参数。

我不知道你实际使用哪一个,但在你的情况下它们应该是相同的。

我建议你读这个: Escaping parameters for MySQL queries

答案 1 :(得分:0)

以下是一些调试内容:1。检查以确保第二次提交实际上被称为#debug1。 2.接下来检查#debug2语句的MySQL错误delete

require('dbcon2.php');


if(isset($_GET['submit1'])) { 

    echo 'submit1: was called'; #debug1

    if(isset($_POST['post_autor']) && isset($_POST['post_tresc'])) {

        mysqli_query($connect,"INSERT INTO news (tresc, autor) VALUES ('$_POST[post_tresc]', '$_POST[post_autor]')");

    }
} 
elseif(isset($_GET['submit2'])) {

    echo 'submit2: was called'; #debug1

    if(isset($_POST['post_id_news2'])) {
        $usun = $_POST['post_id_news'];

        #debug2
        if(!mysqli_query($connect,"DELETE FROM news WHERE id_news = ".$usun."")) {
                echo 'MySQL error: ' . mysqli_error($connect);
        }
    } 
    else {
        echo 'Proba usunieca postu o pustym id.';
    }
}

答案 2 :(得分:0)

可能...... 而不是if(isset($_GET['submit1'])) 使用if(isset($_POST['submit1']))