表格新闻重复

时间:2017-09-11 18:36:36

标签: php mysql

我不是专家开发人员,但我创建了一个发布我的新闻的编辑器。 但每次我更新页面(使用f5或其他)我在我的数据库中发现了一篇新文章。所以我的数据库充满了一篇文章。 有一个例子:https://image.prntscr.com/image/3z0d-9I5S9OUdGBCaytz2Q.png 这是我的代码,我错了吗?

 <?php

require_once('../db_conf.php');

$id = $key;

    $titolo = addslashes($_POST['titolo']);
    $immagine = addslashes($_POST['immagine']);
    $anteprima = addslashes($_POST['anteprima']);
    $testo = addslashes($_POST['testo']);
    $autore = addslashes($_POST['autore']);


mysql_query("INSERT INTO news (titolo,immagine,anteprima,testo,data,autore) VALUES ('".$titolo."','".$immagine."','".$anteprima."','".$testo."','".$date_full."','".$autore."')") or die(mysql_error());



?>


        <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main">
        <center>  <h1>Inserisci News</h1></center>


       <center> 
          <FORM METHOD="post" ACTION="<? echo $PHP_SELF; ?>">
<div class="form-group sp-wdg5"><label class="col-sm-2 control-label">Titolo:</label>
<div class="col-sm-10"><input type="text" name="titolo" value class="form-control"></div>
</div>
<div class="form-group sp-wdg5"><label class="col-sm-2 control-label">Immagine:</label>
<div class="col-sm-10"><input type="text" name="immagine" value class="form-control"></div>
</div>
<div class="form-group sp-wdg5"><label class="col-sm-2 control-label">Testo di Anteprima:</label>
<div class="col-sm-10"><input type="text" name="anteprima" value class="form-control"></div>
</div>
          <script type="text/javascript" src="/tinymce/js/tinymce/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
        selector: "textarea",
        language : "it",
        plugins: [
                "advlist autolink autosave link lists charmap print preview hr anchor pagebreak image imagetools",
                "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media",
                "table contextmenu directionality emoticons textcolor paste fullpage textcolor colorpicker textpattern"
        ],

        toolbar1: "bold italic underline strikethrough cut copy paste alignleft aligncenter alignright alignjustify | searchreplace | bullist numlist | outdent indent blockquote | styleselect formatselect fontselect fontsizeselect",
        toolbar2: "undo redo | link unlink anchor image media code |  preview | forecolor backcolor | table | hr removeformat",
        toolbar3: "charmap | print fullscreen | ltr rtl | spellchecker | visualchars visualblocks nonbreaking template pagebreak restoredraft",

        menubar: false,
        toolbar_items_size: 'small',

        style_formats: [
                {title: 'Bold text', inline: 'b'},
                {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
                {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
                {title: 'Example 1', inline: 'span', classes: 'example1'},
                {title: 'Example 2', inline: 'span', classes: 'example2'},
                {title: 'Table styles'},
                {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
        ],

        templates: [
                {title: 'Test template 1', content: 'Test 1'},
                {title: 'Test template 2', content: 'Test 2'}
        ],
  imagetools_toolbar: "rotateleft rotateright | flipv fliph | editimage imageoptions",
  imagetools_cors_hosts: ['/']
});</script>
<div class="row container-modifica">
<div class="col-lg-12">
<div class="ibox float-e-margins">

<div class="ibox-content">
<div class="col-container-modifica">

<div class="hr-line-dashed"></div>
<div class="form-group">
<div class="col-sm-10">
<div class="editor-special-news">
<textarea name='testo' cols='70' rows='25' width="20" wrap='soft' id='testo' class='multitext'></textarea>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group sp-wdg5"><label class="col-sm-2 control-label">Autore:</label>
<div class="col-sm-10"><input type="text" name="autore" value class="form-control"></div>
</div>
<button type="submit" accesskey="s" class="btn btn-w-m btn-info"> Aggiungi </button></FORM>
</center>  

1 个答案:

答案 0 :(得分:0)

您需要提交按钮。如果单击该按钮,您的代码将会运行

<?php

    require_once('../db_conf.php');
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $id = $key;

        $titolo = addslashes($_POST['titolo']);
        $immagine = addslashes($_POST['immagine']);
        $anteprima = addslashes($_POST['anteprima']);
        $testo = addslashes($_POST['testo']);
        $autore = addslashes($_POST['autore']);


    mysql_query("INSERT INTO news (titolo,immagine,anteprima,testo,data,autore) VALUES ('".$titolo."','".$immagine."','".$anteprima."','".$testo."','".$date_full."','".$autore."')") or die(mysql_error());


    }
    ?>


            <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main">
            <center>  <h1>Inserisci News</h1></center>


           <center> 
              <FORM METHOD="post" ACTION="<? echo $PHP_SELF; ?>">
    <div class="form-group sp-wdg5"><label class="col-sm-2 control-label">Titolo:</label>
    <div class="col-sm-10"><input type="text" name="titolo" value class="form-control"></div>
    </div>
    <div class="form-group sp-wdg5"><label class="col-sm-2 control-label">Immagine:</label>
    <div class="col-sm-10"><input type="text" name="immagine" value class="form-control"></div>
    </div>
    <div class="form-group sp-wdg5"><label class="col-sm-2 control-label">Testo di Anteprima:</label>
    <div class="col-sm-10"><input type="text" name="anteprima" value class="form-control"></div>
<div class="col-sm-10"><input class="form-control btn btn-primary"  type="submit" value="Send it!"></div>
    </div>