Jump to content

how do i stop this from updating every time there's a refresh


Recommended Posts

The following code works. The user checks off what foods they want to be added. Then the foods and their nutrient values get added into the database. However, if the user refreshes, it updates the database with the values they selected before - thus doubling the amount of foods stored in the database. How do I stop this from happening?

 

Right now i'm taking values from the POST array and running an INSERT SQL query to put them into the database. (look for the following comment below: //Look here PHPFREAKS.COM)

 

<?php include 'top.php'; ?>
<?php

if (!loggedin())
{//1 if start
header('Location: need-to-log-in-mmp.php');
}//1 if end
?>
    <title>My Meal Planner - Vitamin K Tracker</title>
</head>

<div id="container">

<?php include 'header.php'; ?>

<?php include 'nav.php'; ?>



<div id="content-container">

<div id="content_for_site">
	<h2>My Meal Planner</h2>	<br />	
                      
	     


	      
	        	
                        My Meal Que:
		<br />
		<ul>
			<form action="my-meal-planner.php" method="POST">
		<?php
//do sql query to return the foods and nutrients that a person added to their que

$query = "SELECT DISTINCT
foods.id, `name`, `source`,`vit_k`, `cal`,`protein`, `fiber`, `carbs`, `sugar`, `sodium`, `chol`, `total_servings`
FROM `foods`
LEFT JOIN users_foods
ON foods.id=users_foods.food_id

WHERE users_foods.user_id =". $_SESSION['user_id']."
ORDER BY users_foods.id";



$query_run = mysql_query($query, $link);
//echo mysql_errno($link) . ": " . mysql_error($link). "\n";
while ($row = mysql_fetch_array($query_run)){
?>	<input type="checkbox" name="<?php
echo $row["id"];
?>"<?php
    echo ' value="'.$row["id"].'" />';	
	echo ' Food Name: '. $row["name"]." | Vitamin K: ".
    $row["vit_k"]." | Fiber: ".
    $row["fiber"]." | Calories: ".
    $row["cal"]." | Protein: ".
    $row["protein"]." | Carbs:".
    $row["carbs"]." | Sugar: ".
    $row["sugar"]." | Sodium: ".
    $row["sodium"]." | Cholesterol: ".
    $row["chol"]." | Source: ".
    $row["source"]." | Total Servings: ".
    $row["total_servings"]. "<br />How many servings will you have? "?><input type="text" name="<?php
echo $row["id"]."serving";
?>"<?php
    echo ' />';
   echo '<br /><br />';	
}
     

?>
</ul>
			Select a date: <input id="date" type="text" name="add_to_calendar"><br />
					<input type="submit" value="add to calendar" ><br />





			<input type="button" value="delete" name="delete">

	</form>

		<?php
		//updates the date of the food 
		if (isset($_POST['add_to_calendar']) && !empty($_POST['add_to_calendar'])){

		$add_to_calendar = $_POST['add_to_calendar'];
		$date_array = explode("/", $add_to_calendar);
                   

//Look here PHPFREAKS.COM		
					for ($i=1; $i<1000; $i++){
if (!empty($_POST[$i])){
$id = $_POST[$i];
$serv = $id. "serving";
$serving = $_POST[$serv];
$month = $date_array[0];
$day = $date_array[1];
$year = $date_array[2];
//echo 'month: '. $month . ' day: ' . $day . ' year: ' . $year . "<br />"; 

if (substr($month,0,1) == '0'){
	//takes out a zero in front of month if it exists
	$month = substr($month,1,1);}

if (substr($day,0,1) == '0'){
	//takes out a zero in front of day if it exists
	$day = substr($day,1,1);}

//echo 'POST ARRAY READS AS: <br />';
//print_r($_POST);
//echo '<br />';
$query = "INSERT INTO `users_calendar` VALUES ('','".$_SESSION['user_id']."','".$id."','".$month."','".$day."','".$year."','".$serving."')";
//echo '<br /> The query reads as: ' . $query;
$query_run = mysql_query($query) or die(mysql_error());
//columns in users_calendar:
//DONEid    DONEuser__id   DONEfood_id    DONEcalendar_month   DONElendar_day    DONEcalendar_year    servings
  }
}




		}
		?>

		<br />


	</div>



<script type="text/javascript" src="js/datepicker.js"></script>
<div id="clear"></div>
<?php //include 'footer.php'; ?>


</div>
</div>
</body>
</html>

Nicest option is to redirect to some page immediately after the operation. Thus refresh only refreshes that page.

 

Otherwise you can include nonce tokens: unique values that are only good for one use. Record that token somewhere, like the session, and only allow the operation if the token hasn't been used.

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.