A small Problem

php India
  • 14 years ago
    I want to insert some message into database and display the message.
    if insert the message it will be inserted successfully and displayed successfully.
    But the problem is after inserting if i pres refersh button it will be inserted again.
    The code i had writeen is.
    <html>
    <head>
    <script language="JavaScript" type="text/JavaScript">
    function validate()
    {
    if(document.form2.message.value=="")
    alert("Plase enter some message");
    }
    </script>
    </head>
    <body>
    <form  action=<? echo $PHP_SELF ?>  method="post" name="form2">

    <?php
    $_SESSION['userid']=$userid;
    echo '<input type="hidden" name="userid" value="'.$_SESSION['userid'].'">';
    $sqlServer = mysql_connect('localhost','root','') or die("Unable to connect to mysql ");
    $db = mysql_select_db("mssi",$sqlServer) or die("Unable to connect to Users dabase");
    if($Submit)
    {
    if(!($_POST[message]=='null'||$_POST[message]=='NULL'||$_POST[message]=='')&&!($userid=='null' || $userid=='NULL' || $userid==''))
    {
    $message=$_POST['message'];
    $query=mysql_query("insert into messages (username,message) values('$userid','$message')");
    }
    }
    $result=mysql_query("select * from messages order by date") or die('Query Failed:'.mysql_error());
    echo "<table align=center>\n";
    echo "<tr><th ><font> Name</font></th><th><font> Message  </font>   </th><th><font>Date</font></th></tr>";
    while($row=mysql_fetch_array($result))
    {
      printf("<tr><td>%s   </td><td>%s   </td><td>%s</td></tr>\n",$row["username"],$row["message"],$row["date"]);
    }
    echo "</table>";

    mysql_close($sqlServer);

    ?><br><br>
    <div align="center">
          <textarea name="message" rows="6" cols="45"></textarea><br><br>
              <input type="submit" value="Submit" name="Submit" onclick="validate()"></td>
            </div>

    </body>
    </html>

    I didn't find what is the problem is this code.
    Any one of u find please tell me.






















































  • 14 years ago
    Clear the values, both user id and message after u have been successfully inserted, i think so, but not sure with this answer.
  • 14 years ago
    Thank you for u r response.
    I am trying for that also.
    i am giving as unset($userid) and unset($message)
    but its not working.






  • 14 years ago
    Whenever a user clicks refresh in their browser the following happens:

    That last web address that was accessed is accessed again.
    Any information that was sent to the web server is sent again.

    There are, however, a couple of ways around this to stop the information going into the database just because the user clicked on refresh.

    Have 1 file that inserts the information into a database. If it completes successfully send a redirection header:
    header("Location: redirect.html");
    or
    header("Location: http://www.full.Path/toFile.html");
    . As long as you do this before any other input is sent out you can redirect the user to another page (so that when they click refresh they get that new page instead of the one that is inserting the information.

    Then, on that new page you can get the information that has just been inserted (however you want to do this is up to you), or just inform the user that everything went as planned. I'll have an example up in a bit so that you guys can follow it.










  • 14 years ago
    Here is the small example to illustrate what I was saying:

    <?php
    //insert information from form and redirect user

    //connect to database
    $conn=mysql_connect("localhost","root","password")
    or die(mysql_error());
    $db=mysql_use_db("databaseName",$conn)
    or die(mysql_error());//

    //get information from fields
    $name=$_POST['name'];
    $email=$_POST['email'];

    //validate information
    //always validate user input!
    //You don't know what they're trying to do to your site
    //
    //$name=validate_name($name);
    //$email=valudate_email($email);

    //insert information into database
    $sql="INSERT INTO tblNewsLetter (name,email)
    VALUES('".$name."','".$email."');";
    $result=mysql_query($sql);

    //redirect user
    if($result){
    header("Location: success.php?name=$name");
    }
    else{
    header("Location: failed.php");//back to the form?
    }
    ?>

































    This example is very basic so that idea behind redirecting the user is easier to understand. Let is know if there are any problems :)


Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The greatest performance improvement of all is when a system goes from not-working to working.” - John Ousterhout