Jump to content

simple php question


elementis0

Recommended Posts

Well, I just finished a php tutorial and decided to test my limited knowledge of php and well I've hit a roadblock. first of all heres the scripts:The Processing form script. workout.php

<html><head><title>Data Entered</title></head><body><?php$con = mysql_connect('localhost','root','april20');if(!$con){	die('Could not connect: '.mysql_error());}mysql_select_db("workout",$con);//enters workout dataif(mysql_query("INSERT INTO exercise (date, PushUps, Crunches, Cardio, Intake) " .		"VALUES('$_POST[date]','$_POST[pushups]','" .		"$_POST[crunches]','$_POST[cardio]','$_POST[intake]')")){	echo "Activity done during ".$_POST['date']."<br />";	echo "Push-Ups Done: ".$_POST['pushups']."<br />";	echo "Crunches Done: ".$_POST['crunches']."<br />";	echo "Cardio Time: ".$_POST['cardio']."<br />";	echo "Amount eaten today: ".$_POST['intake']."<br />";	echo "<a href='http://localhost/workform.php'>Go Back</a><br />";	echo "<a href='http://localhost/mydiet.html'>Home</a><br />";	echo "<a href='http://localhost/worksofar.php'>View Progress</a><br />";}else{	echo "ERROR! Data not entered correctly."."<br />";		echo "<a href='http://localhost/workform.php'>Go Back</a><br />";	echo "<a href='http://localhost/mydiet.html'>Home</a><br />";	echo "<a href='http://localhost/worksofar.php'>View Progress</a><br />";}mysql_close($con);?></body>

The form for that scriptworkoutform.php script

<html><head></head><body>  <form action="workout.php" method="post">  <fieldset>  <legend>Enter Workout data</legend>  Date(yyyy/mm/dd):  <br />  <input type="text" name="date" value="<?php echo date('Y-m-d'); ?>" />	<br />  <br /> Push-Ups: <input type="text" maxlength="3" size="3" name="pushups"/><br /> <br /> Crunches:  <input type="text" maxlength="3" size="3" name="crunches" /><br /> <br /> Cardio:	  <input type="text" name="cardio" /><br /> <br /> Intake:<br /> <textarea name="intake"></textarea> <br /> <input type="submit" value="Enter Data" /><br /> </fieldset>  </form>  <a href="http://localhost/mydiet.html">Go Home</a><br /><a href="http://localhost/worksofar.php">View Progress</a><br />  </body></html>

and the script where it displays what is entered(im putting this script down just in case you guys find something that needs tweaking)worksofar.php

<html><head><title>Work Done So far</title></head><body><?php$con = mysql_connect('localhost','root','april20');if(!$con){	echo "Could Not Connect to database: ".mysql_error();}echo "<p>Work Done So Far:</p>";mysql_select_db("workout",$con);$query = mysql_query("SELECT * FROM exercise");echo "<table border='1'>" ."<th>Date</th><th>Push-Ups</th><th>Crunches</th><th>Cardio</th><th>Intake</th>";while($row = mysql_fetch_array($query)){	echo "<tr><td>".$row['date']."</td>" .			"<td>".$row['PushUps']."</td>" .					"<td>".$row['Crunches']."</td>" .							"<td>".$row['Cardio']."</td>" .									"<td>".$row['Intake']."</td></tr>";																									}echo "</table>";$pushsum = mysql_query("SELECT SUM(PushUps) FROM exercise");$crunchsum = mysql_query("SELECT SUM(Crunches) FROM exercise");echo "Grand Totals: <br />";while(($pushups = mysql_fetch_array($pushsum))&&($crunches = mysql_fetch_array($crunchsum))){echo "Push Ups: ".$pushups['SUM(PushUps)']."<br />";echo "Crunches: ".$crunches['SUM(Crunches)']."<br />";}?><a href="http://localhost/mydiet.html">Go Home</a><br /><a href="http://localhost/workform.php">Enter Progress</a><br /></body></html>

Ok now to explain the script and its function. I made this because i started a diet and have been working out regularly and decided to keep a database to keep records of my progress, pretty simple. Basically what I want to know is how can I tweak the workout.php script to check if the current date is already entered in the database. If the current date is already entered in the database then I want it to update that information, but if the current date is not already entered in the database then it will Insert a new row for that day for the info to be entered. its pretty simple but I cant figure out how to make it so it checks if the date already exists in the database. My best guess is some type of use of the isset function? but im not totally sure, which is why I made this thread. Can anybody tell me how accomplish this?Also when telling me the code tweak, please explain what each tweak in the code does so I fully understand the logic behind it.

Link to comment
Share on other sites

You can replace

if(mysql_query("INSERT INTO exercise (date, PushUps, Crunches, Cardio, Intake) " .		"VALUES('$_POST[date]','$_POST[pushups]','" .		"$_POST[crunches]','$_POST[cardio]','$_POST[intake]')")){

with:

$result = mysql_query("SELECT COUNT(date) FROM exercise WHERE date='$_POST[date]'"); //Returns the number of records with the posted date$dateExist = ( implode(mysql_fetch_assoc($result)) == 1 ) ? true : false; //Sets a variable to true or false baed on previous queryif ($dateExist) { //The date entered is already in the table	$fields = array( //Set an array of all table fields and their related post fields		"date" => "date",		"PushUps" => "pushups",		"Crunches" => "crunches",		"Cardio" => "cardio",		"Intake" => "intake"	);	foreach($fields as $field => $key) { //For every field in $fields		if (mysql_query("UPDATE exercise SET $field='$_POST[$key]'")) { //Attempt to update to database			echo "$field updated to $_POST[$key] for date $_POST[date]<br />\n"; //Echo the update information		} else { //The query failed			echo "Error updating $field to \"$_POST[$key]\": " . mysql_error() . "<br />\n"; //Show it, and why		}	}} else { //The current date is not in the database	//You know what this code does...	if(mysql_query("INSERT INTO exercise (date, PushUps, Crunches, Cardio, Intake) VALUES	('$_POST[date]','$_POST[pushups]','$_POST[crunches]','$_POST[cardio]','$_POST[intake]')")) {		echo "Activity done during ".$_POST['date']."<br />";		echo "Push-Ups Done: ".$_POST['pushups']."<br />";		echo "Crunches Done: ".$_POST['crunches']."<br />";		echo "Cardio Time: ".$_POST['cardio']."<br />";		echo "Amount eaten today: ".$_POST['intake']."<br />";	} else {		echo "ERROR! Data not entered correctly."."<br />";	}}echo "<a href='http://localhost/workform.php'>Go Back</a><br />";echo "<a href='http://localhost/mydiet.html'>Home</a><br />";echo "<a href='http://localhost/worksofar.php'>View Progress</a><br />";

There, the 'tweak' fully commented for your perusal :) ask here if you have any questions.

Link to comment
Share on other sites

Thanks, I read the code over and over a few times and now I get it =D This has been a great help. Now for my next step of this project, make a login haha. Who knows I might post another thread with questions about that but hopefully I won't because i'd rather find it out for myself =P

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...