Jump to content

mysql duplicates latest entry on refresh, need help!


petewall

Recommended Posts

So i have a little script for my bands site, you're supposed to log in and submit a form that inserts the data into mysql and then displays it on the "tour" page. the problem i'm having is that everytime i hit F5 the latest entry gets duplicated. is there anyone who can help me? i'm pretty new to .php and mysql and this problem has been bugging me for quite some time now.

well, here's the code:

 

 

 

add_dates.php:

<?php

               require_once 'db.php';


$database = mysql_connect($dbhost, $dbuser, $dbpass);
	if (!$database) die("Unable to connect to mysql: " . mysql_error());
mysql_select_db($dbname);





if (isset($_POST['date']) &&
isset($_POST['location']) &&
isset($_POST['time']) &&
isset($_POST['cost']))
{
$date = ($_POST['date']);
$location = ($_POST['location']);
$time = ($_POST['time']);
$cost = ($_POST['cost']);




	$query = mysql_query("INSERT INTO tourdates (date, location, time, cost)
	VALUES	('$date', '$location', '$time', '$cost')",$database);

	}

	$query = "SELECT date, location, time, cost FROM tourdates WHERE date >= NOW() ORDER BY date ASC";
	$result = mysql_query($query);


		echo "<table border='1' cellpadding='5'>";
	echo '<td colspan="4"><center><b>Upcoming Gigs:</b></td><br /></center>';
echo "<tr> <th>Date</th> <th>Location</th> <th>Time</th> <th>Cost</th> </tr>";

while($row = mysql_fetch_array( $result )) {

echo "<tr><td>"; 
echo $row['date'];
echo "</td><td>"; 
echo $row['location'];
echo "</td><td>";
echo $row['time'];
echo "</td><td>"; 
echo $row['cost'];
echo "</td></tr>"; 
} 

echo "</table>";




	$query = "SELECT date, location, time, cost FROM tourdates WHERE date < NOW() ORDER BY date DESC";
	$result = mysql_query($query);






	echo "<table border='1' cellpadding='5'>";
	echo '<td colspan="4"><center><b>Past Gigs:</b></td><br /></center>';
echo "<tr> <th>Date</th> <th>Location</th> <th>Time</th> <th>Cost</th> </tr>";

while($row = mysql_fetch_array( $result )) {

echo "<tr><td>"; 
echo $row['date'];
echo "</td><td>"; 
echo $row['location'];
echo "</td><td>";
echo $row['time'];
echo "</td><td>"; 
echo $row['cost'];
echo "</td></tr>"; 
} 

echo "</table>";
?>

dates_form.php

<form method="post" action="add_dates.php">
datum:   <input type="text" name="date">
plats:   <input type="text" name="location">
tid:     <input type="text" name="time">
kostnad: <input type="text" name="cost">
<input type="submit" name="submit" value="lägg till">

</form>

Link to comment
Share on other sites

There is an easy solution to this. It seems you have one page which checks if the user has submitted data and, if it has, inserts the new record. Then after it inserts the record (or if there is no record to insert) it then displays the records.

 

You shoudl split up that logic. When a user submits a form to add a record it should point to the processign page that enters the new record. Then at the end of that script use a header() command to redirect to the display page. By using the header() function all POST data is destroyed. So, if the user refreshes the display page it will only refresh the display - it won't refresh (reexecute) the processing page.

Link to comment
Share on other sites

Here is some example code based on what you posted:

 

add_dates.php: (removed display portion and added redirect. You should also add validation. Previously it only checked if the values were set - which they always will be. Instead you should check if any are empty(). Plus you need to validate that the date is valid format)

<?php
   
require_once 'db.php';
$database = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$database) die("Unable to connect to mysql: " . mysql_error());
mysql_select_db($dbname);
    
if (isset($_POST['date']) && isset($_POST['location']) && isset($_POST['time']) && isset($_POST['cost']))
{
    $date     = trim($_POST['date']);
    $location = trim($_POST['location']);
    $time     = trim($_POST['time']);
    $cost     = trim($_POST['cost']);
        
    $query = "INSERT INTO tourdates (date, location, time, cost)
              VALUES ('{$date}', '{$location}', {'$time}', '{$cost}')";
    $result = mysql_query($query, $database);
}
    
header ("Location: http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}/display_dates.php");
?>

 

display_dates.php (note, modified code to only need one query and process to create two outputs

<?php
//Create output for upcoming and past gigs
$query = "SELECT date, location, time, cost, (date>=NOW, 1, 0) as future
          FROM tourdates
          ORDER BY futurepast DESC, date ASC";
$result = mysql_query($query);
        
$futureOrPast = false;
echo "<table border=\"1\" cellpadding=\"5\">\n";
while($row = mysql_fetch_array( $result ))
{
    if($future!==$result['future'])
    {
        $future=$result['future'];
        $title = ($future==1) ? 'Upcoming Gigs:' : 'Past Gigs:' ;
        echo "<tr><th colspan=\"4\"><center><b>{$title}</b></center></th></tr>\n";
        echo "<tr><th>Date</th><th>Location</th><th>Time</th><th>Cost</th></tr>\n";
    }
    echo "<tr>\n";
    echo "  <td>{$row['date']}</td>\n";
    echo "  <td>{$row['location']}</td>\n";
    echo "  <td>{$row['time']}</td>\n";
    echo "  <td>{$row['cost']}</td>\n";
    echo "</tr>\n"; 
} 
echo "</table>\n";
    
?>

Link to comment
Share on other sites

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.