Jump to content

Recommended Posts

im trying to insert a value in to mysql using an experiment everything works fine after hitting the insert button but the problem arises wen i refresh the page as a empty value gets inserted in to the mysql table wen i refresh it.(pardon my english)

 

this is the index page where i fetch an result from mysql table:

<div id="container">
<div class="Container-left">
</div>
    <div class="container-right"> <form action="1.2.php" method="post">
            <textarea height="190px" width="190px" name="text" placeholder="share wats on your mind"></textarea>
            <br>
            <input type="submit" name="button" value="Type Here" />
        </form> 
        <hr>
    <?php
include '1.2.php';
while ($fetch = mysqli_fetch_array($query1)) {
    echo 'comment:'.$fetch['post'].'<br/>';
    echo '<small>date:'.$fetch['date'].'</small><br/><hr>';
    
}
    ?>
    </div>
</div>

this is the page where my second code rests:


<?php 
include 'config.php';
error_reporting(E_ALL ^ E_NOTICE);
$h= htmlspecialchars($_POST['text']);
$p=  mysqli_real_escape_string($conn,$h);
$sql = "INSERT INTO article (post) VALUES ('$h') ";
$sql1="SELECT * FROM article";
$query=mysqli_query($conn, $sql);
$query1=  mysqli_query($conn, $sql1);
if ($_POST['button']){

    echo 'inserted';
}  
else {
    echo 'not inserted'. mysqli_error($conn);    
}


what should i do to stop value getting inserted in to my table while refreshing the page. pl help and thanks in advance.

Edited by shan

comment out the actual code that inserts anything into your db - echo the query instead so you can see what's happening. You must be hitting the db somewhere. Get rid of the location header until you can find the problem.

Use an include for that page instead. 

Edited by hansford
<form action="1.2.php" method="post">

This form goes to the page set in the action. 1.2.php needs to do its work and then redirect back to this form - no refresh on a form page as it just resubmits the form and thus the blank db insert.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.