Jump to content

davidjones1990

Members
  • Posts

    20
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

davidjones1990's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks for your input, I cant believe I didn't think of that, my method was so long winded
  2. Hello everybody, I have a script that allows a user to set themselves a target and a script that allows them to edit the target if they decide to. Now everything works as it should until the data from the form (on the edit target page) should be updated in the database. I require that the user must update at least one field. For example they may want to extend the completion date so I would only want to update the completion date field in the database and the fields the user left blank should not be updated in the database. The problem I am having is regardless of if the user enter a value into the field it updates the database so if the user leaves a field blank it updates the database will a blank value. Here is my code: //If the user clicks the update button on a WEIGHT LOSS target run the following code to update the database if(isset($_POST['target_weight_loss_update'])){ $target_weight_loss_update = $_POST['target_weight_loss_update']; $weight_loss_complete_update = $_POST['weight_loss_complete_update']; $weight_loss_comment_update = $_POST['weight_loss_comment_update']; $weight_loss_user_id_update = $_POST['weight_loss_user_id_update']; //Check that at least one value has been entered into the form if(empty($target_weight_loss_update) && empty($weight_loss_complete_update) && empty($weight_loss_comment_update)){ $error_msg = 'Please enter at least one value to update your '.$target_type.' target'; } //Check to see what variables have values in them so we know what fields to update in datebase //If the user has entered a value for their TARGET WEIGHT LOSS update that value in the database if(isset($target_weight_loss_update) || !empty($target_weight_loss_update)){ $target_weight_loss_update_sql = mysql_query("UPDATE user_targets SET target_weight='$target_weight_loss_update' WHERE id='$target_id' LIMIT 1") or die (mysql_error()); //Check that the update was successful if($target_weight_loss_update_sql){ $error_msg = 'Target '.$target_type.' updated successfully'; } else { $error_msg = 'Could not update target. Please try again.'; } } //If the user has entered a value for their TARTGET WEIGHT LOSS DATE update that value in the database if(isset($weight_loss_complete_update) || !empty($weight_loss_complete_update)){ $weight_loss_completion_date_sql = mysql_query("UPDATE user_targets SET target_date='$weight_loss_complete_update' WHERE id='$target_id' LIMIT 1") or die (mysql_error()); //Check that the update was successful if($weight_loss_completion_date_sql){ $error_msg = 'Target '.$target_type.' updated successfully'; } else { $error_msg = 'Could not update target. Please try again.'; } } //If the user has entered a value for their TARGET WEIGHT LOSS COMMENT update that value in the database if(isset($weight_loss_comment_update) || !empty($weight_loss_comment_update)){ $weight_loss_comment_update_sql = mysql_query("UPDATE user_targets SET extra_info='$weight_loss_comment_update' WHERE id='$target_id' LIMIT 1") or die (mysql_error()); //Check that the update was successful if($weight_loss_comment_update_sql){ $error_msg = 'Target '.$target_type.' updated successfully'; } else { $error_msg = 'Could not update target. Please try again.'; } } } So the question I ask is how can I stop entering blank values into the database and only update the information the user enters into the form? Also I was thinking of having one mysql query at the end and using the IF statements to build the query. Thoughts? Thanks for any advice.
  3. Thank you, your method has cleaned the code up and sorted my ordering problem, many thanks
  4. I know now, in the first query I need to get a distinct target type I'll let you know how i get on
  5. No duplicates, that was the first thing I looked for Not sure that would sort it because the user can have more than one target set Also I noticed something strange. I used the same if statement but changed the query and variables etc. to Lower BMI instead of weight loss and again works but displays duplicate data. The new thing is if there are two targets displayed it will repeat two times and if there are three targets it will repeat three times. So here is a mock up of what the page looks like now. WEIGHT LOSS 1 WEIGHT LOSS 2 WEIGHT LOSS 1 WEIGHT LOSS 2 LOWER BMI 1 LOWER BMI 2 LOWER BMI 3 LOWER BMI 1 LOWER BMI 2 LOWER BMI 3 LOWER BMI 1 LOWER BMI 2 LOWER BMI 3 See what im getting at? Do you think maybe I should count the number of rows being returned with each target type and loop that number of times? Thanks for your responses.
  6. Yes of course sorry for not be clear. So there are two targets that should appear, so it should look like this TARGET 1 TARGET 2 Instead it displays like this TARGET 1 TARGET 2 TARGET 1 TARGET 2 Hope this answers your question.
  7. Hi everybody, I have a problem and was wondering if I could pick your brains. My situation is that I am making a fitness website and one feature is that the user can set themselves targets. Now each target has some fields that are mandatory (for example the date to be completed) and some fields that are unique to each target (for example if the target is weight loss I will need to grab the current weight/target weight fields). Just to make it clear I have all fields in the same database table. So I have written a script that displays that users current targets they have set. It grabs all the data correctly but it duplicates the display and although I am pretty new to php even I can tell the code if not best practise because I have a query inside a loop and a loop inside another loop. Here is the code I have done so far: //Get target types from database for that particular user $get_user_targets = mysql_query("SELECT target_type FROM user_targets WHERE user_id='$id'") or die (mysql_error()); while ($row = mysql_fetch_array($get_user_targets)){ $target_type_display = $row['target_type']; //If the target type is equal to Weight Loss run this script to get all the information and make a display if($target_type_display == 'Weight Loss'){ //Search the database for all weight loass goals $weight_loss_display_sql = mysql_query("SELECT current_weight, target_weight, target_date, date_set, extra_info FROM user_targets WHERE user_id='$id' AND target_type='$target_type_display'") or die (mysql_error()); //Put the data from the query into page variables while($row1 = mysql_fetch_array($weight_loss_display_sql)){ $current_weight_loss_dis = $row1['current_weight']; $target_weight_loss_dis = $row1['target_weight']; $weight_loss_date_set_dis = $row1['date_set']; $weight_loss_target_date_dis = $row1['target_date']; $weight_loss_extra_info_dis = $row1['extra_info']; //Convert date_set into ago time $convertedTime = ($myObject -> convert_datetime($weight_loss_date_set_dis)); $weight_loss_set = ($myObject -> makeAgo($convertedTime)); //Construst a display out of the collected variables $weight_loss_display .= '<div class="blackText" style="width: 475px"> <table width="475px" class="blackText"> <tr><td class="profileUsername" colspan="2">'.$target_type_display.'</td></tr> <tr><td width="250px">Current Weight:</td><td width="225px">'.$current_weight_loss_dis.'</td></tr> <tr><td>Target Weight:</td><td>'.$target_weight_loss_dis.'</td></tr> <tr><td>Date for completion:</td><td>'.$weight_loss_target_date_dis.'</td></tr> <tr><td>'.$weight_loss_extra_info_dis.'</td><td><h6>Date set: '.$weight_loss_set.'</h6></td></tr> </table> </div>'; } } } So far it just checks for a weight loss goal and I was going to expand the code with more if statements for each of the other types of target. So my questions are: 1)Can anybody see why the data is displaying twice? 2) Is there a better way of doing this? (NOTE: im not too worried that the code isn't best practise, I just want to get it functioning properly) Thanks in advance for any help
  8. Hey everyone, So here is my problem. I have some code to display the amount of views that page has got. In this case it is the thread in my forums section. I have used the same code to show how many people have views a certain persons profile page and that works fine but when I use it on my forum thread page I get this error. Here is the section of code: <?php $thread_id = preg_replace('#[^0-9]#i', '', $_GET['id']); $getThreadViews = mysql_query("SELECT view_count FROM forum_posts WHERE id='$thread_id' LIMIT 1") or die (mysql_error()); $row = mysql_fetch_assoc($getThreadViews); $counter = $row['view_count']; if($counter == 0){ $counter = 1; $startCounter = mysql_query("INSERT INTO forum_posts (view_count) VALUES ('$counter') WHERE id='$thread_id' LIMIT 1") or die (mysql_error()); } $threadViews = $counter+1; $appendCounter = mysql_query("UPDATE forum_posts SET view_count='$view_count' WHERE id='$thread_id'") or die (mysql_error()); ?> I have checked that there are no spelling errors so just wanted to show it to a fresh pair of eyes because its really starting to annoy me. Thanks in advance for any help.
  9. Hi thanks for the feedback, I know my code is shockingly bad but im a beginner just trying some things out at the moment. If the time comes to put anything online i'll definitely review my code and make changes for best practise. Anyway the problem was I had the comment display too far down the page Thanks for the tip about joining that will come in handy on several of my pages.
  10. Hey, I have written a script for a very simple PHP wall and comment system. This works fine but the problem I have is displaying the comments. It seems to display the comments associated with the post as well as the comments on the posts above it. I have checked the database and the post ID's are correct. Here is my code: <?php $wallDisplay = ''; $commentDisplay = ''; $wallDisplaySql = mysql_query("SELECT * FROM wall WHERE to_id='$id' ORDER BY datetime DESC") or die (mysql_error()); while($row = mysql_fetch_array($wallDisplaySql)){ $wallPostId = $row["id"]; $to_id = $row["to_id"]; $from_id = $row["from_id"]; $message = $row["message"]; $dateTime = $row["datetime"]; $getFromData = mysql_query("SELECT username FROM members WHERE id='$from_id'") or die (mysql_error()); while($row2 = mysql_fetch_array($getFromData)){ $wallUsername = $row2['username']; } $displayComments = mysql_query("SELECT * FROM wallComments WHERE wallPostId='$wallPostId' ORDER BY datetime DESC"); while($row3 = mysql_fetch_array($displayComments)){ $wallComment = $row3['comment']; $commentFrom = $row3['from_id']; $commentDate = $row3['datetime']; $getUsername = mysql_query("SELECT username FROM members WHERE id='$commentFrom'"); while($row4 = mysql_fetch_array($getUsername)){ $commentUsername = $row4['username']; } $cheersCheck_pic = "members/$commentFrom/pic1.jpg"; $cheersDefault_pic = "members/0/defaultMemberPic.jpg"; if (file_exists($cheersCheck_pic)) { $cheers_pic = "<img src=\"$cheersCheck_pic?$cacheBuster\" width=\"40px\" />"; } else { $cheers_pic = "<img src=\"$cheersDefault_pic\" width=\"40px\" />"; } $commentDisplay .= '<table width="500px" align="right" cellpadding="4" bgcolor="#FFF"> <tr> <td width="10%" bgcolor="#FFFFFF"><a href="member_profile.php?id=' . $commentFrom . '">' . $cheers_pic . '</a><br /> </td> <td width="90%" bgcolor="#DBE4FD"><a href="member_profile.php?id=' . $commentFrom . '"><span class="blackText">' . $commentUsername . '</span></a> • <span class="blackTetx">' . $commentDate . '<br /><font size="1"></font></span><br /> <span class="blackText">' . $wallComment . '</span></td> </tr> </table>'; } $cheersCheck_pic = "members/$from_id/pic1.jpg"; $cheersDefault_pic = "members/0/defaultMemberPic.jpg"; if (file_exists($cheersCheck_pic)) { $cheers_pic = "<img src=\"$cheersCheck_pic?$cacheBuster\" width=\"40px\" />"; } else { $cheers_pic = "<img src=\"$cheersDefault_pic\" width=\"40px\" />"; } $wallDisplay .= '<table width="100%" align="center" cellpadding="4" bgcolor="#FFF"> <tr> <td width="7%" bgcolor="#FFFFFF"><a href="member_profile.php?id=' . $from_id . '">' . $cheers_pic . '</a><br /> </td> <td width="93%" bgcolor="#DBE4FD"><a href="member_profile.php?id=' . $from_id . '"><span class="blackText">' . $wallUsername . '</span></a> • <span class="blackTetx">' . $dateTime . '<br /><font size="1"></font></span><br /> <span class="blackText">' . $message . '</span></td> </tr> </table> <div id="commentList">' . $commentDisplay . '</div> <div id="comment" align="right"> <form id="comment" name="comment" method="post" action="member_profile.php?id=' .$id. '"> <textarea name="comment" id="comment" rows="1" cols="35"></textarea> <input type="hidden" name="wallPostId" id="wallPostId" value="'. $wallPostId .'" /> <input type="hidden" name="commentFrom" id="commentFrom" value="'. $_SESSION['id'] .'" /> <input type="submit" name="submitComment" id="submitComment" /> </form> </div><br /> '; } ?> I have been looking at it for ages but can think why this is happening. Thanks in advance for any help
  11. Works now, although I put this in the form instead of what you said, I know your way is better but i just want to get it working for now <input type="hidden" name ='id' value="<?php echo ("$id"); ?>"/> Thanks for your help
  12. Im getting the blog entry ID from the URL so I can use it to update that blog entry with the new posted information Is that right? or do I need to go back to basics
  13. Yep, the ID corresponds to the correct ID in the database
×
×
  • 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.