JCowdrick Posted July 18, 2010 Share Posted July 18, 2010 Hello All, I'm fairly new to PHP, and I am working on a program to somewhat track health stats. I started with a tutorial, and have been modifying it for my needs. The part I am struggling with is the updating of the user's stats. The update query doesn't seem to be working. I get the message that the stats have been updated, but when I check the database, they haven't changed. I have included the code for my whole page so you can see what I'm referring to. <?php session_start(); // If the session vars aren't set, try to set them with a cookie if (!isset($_SESSION['user_id'])) { if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) { $_SESSION['user_id'] = $_COOKIE['user_id']; $_SESSION['username'] = $_COOKIE['username']; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Essential Fitness</title> <link href="css/base.css" rel="stylesheet" type="text/css" /> <script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script> <link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="container"> <div id="content"> <div id="header"> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="950" height="184" id="FlashID" title="Header animated flash"> <param name="movie" value="flash/header.swf" /> <param name="quality" value="high" /> <param name="wmode" value="transparent" /> <param name="swfversion" value="6.0.65.0" /> <!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you don’t want users to see the prompt. --> <param name="expressinstall" value="Scripts/expressInstall.swf" /> <!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. --> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="flash/header.swf" width="950" height="184"> <!--<![endif]--> <param name="quality" value="high" /> <param name="wmode" value="transparent" /> <param name="swfversion" value="6.0.65.0" /> <param name="expressinstall" value="Scripts/expressInstall.swf" /> <!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. --> <div> <h4>Content on this page requires a newer version of Adobe Flash Player.</h4> <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" width="112" height="33" /></a></p> </div> <!--[if !IE]>--> </object> <!--<![endif]--> </object> </div> <div id="nav"> <div id="TabbedPanels1" class="TabbedPanels"> <ul class="TabbedPanelsTabGroup"> <li class="TabbedPanelsTab" tabindex="0"><a href="index.html">Home</a></li> <li class="TabbedPanelsTab" tabindex="0" onmouseover="TabbedPanels1.showPanel(this);"><a href="resistance.html">Resistance</a></li> <li class="TabbedPanelsTab" tabindex="0" onmouseover="TabbedPanels1.showPanel(this);"><a href="athome.html">At Home</a></li> <li class="TabbedPanelsTab" tabindex="0" onmouseover="TabbedPanels1.showPanel(this);"><a href="cardio.html">Cardio</a></li> <li class="TabbedPanelsTab" tabindex="0"><a href="family.html">Family</a></li> <li class="TabbedPanelsTab" tabindex="0"><a href="flexibility.html">Flexibility</a></li> <li class="TabbedPanelsTab" tabindex="0"><a href="tracker.php">Self Tracker</a></li> </ul> <div class="TabbedPanelsContentGroup"> <div class="TabbedPanelsContent"> </div> <div class="TabbedPanelsContent" id="tc1"><a href="resbegin.html">Beginner</a> | <a href="resint.html">Intermediate</a></div> <div class="TabbedPanelsContent" id="tc2"><a href="athomebegin.html">Beginner</a> | <a href="athomeint.html">Intermediate</a></div> <div class="TabbedPanelsContent" id="tc3"><a href="cardiobegin.html">Beginner</a> | <a href="cardioint.html">Intermediate</a></div> <div class="TabbedPanelsContent"> </div> <div class="TabbedPanelsContent"> </div> <div class="TabbedPanelsContent"> </div> </div> </div> </div> <div id="stage"> <h1>Enter today's Self Tracker stats</h1><img src="images/featimg.jpg" alt="Filler Image" name="featimg" width="300" height="184" align="right" id="featimg" /> <?php require_once('appvars.php'); require_once('connectvars.php'); // Make sure the user is logged in before going any further. if (!isset($_SESSION['user_id'])) { echo '<p class="login">Please <a href="login.php">log in</a> to access this page.</p>'; exit(); } else { echo('<p class="login">You are logged in as ' . $_SESSION['username'] . '. <a href="logout.php">Log out</a>.</p>'); } // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (isset($_POST['submit'])) { // Grab the profile data from the POST $currentdate = mysqli_real_escape_string($dbc, trim($_POST['currentdate'])); $bodyweight = mysqli_real_escape_string($dbc, trim($_POST['bodyweight'])); $bmi = mysqli_real_escape_string($dbc, trim($_POST['bmi'])); $bloodpressure = mysqli_real_escape_string($dbc, trim($_POST['bloodpressure'])); $hdl = mysqli_real_escape_string($dbc, trim($_POST['hdl'])); $ldl = mysqli_real_escape_string($dbc, trim($_POST['ldl'])); $totalcholesterol = mysqli_real_escape_string($dbc, trim($_POST['totalcholesterol'])); $triglycerides = mysqli_real_escape_string($dbc, trim($_POST['triglycerides'])); $glucoselevels = mysqli_real_escape_string($dbc, trim($_POST['glucoselevels'])); $bodycomposition = mysqli_real_escape_string($dbc, trim($_POST['bodycomposition'])); $waist = mysqli_real_escape_string($dbc, trim($_POST['waist'])); $hip = mysqli_real_escape_string($dbc, trim($_POST['hip'])); $error = false; // Update the profile data in the database if (!$error) { if (!empty($currentdate) && !empty($bodyweight) && !empty($bmi) && !empty($bloodpressure) && !empty($hdl) && !empty($ldl) && !empty($totalcholesterol) && !empty($triglycerides) && !empty($glucoselevels) && !empty($bodycomposition) && !empty($waist) && !empty($hip)) { $query = "UPDATE userDB SET currentdate = '$currentdate', bodyweight = '$bodyweight', bmi = '$bmi', bloodpressure = '$bloodpressure', hdl = '$hdl', ldl = '$ldl' "." totalcholesterol = '$totalcholesterol', triglycerides = '$triglycerides', glucoselevels = '$glucoselevels', bodycomposition = '$bodycomposition', waist = '$waist', hip = '$hip' WHERE user_id = '" . $_SESSION['user_id'] . "'"; mysqli_query($dbc, $query); // Confirm success with the user echo '<p>Your stats have been successfully updated. View your new <a href="login.php">stats</a>?</p></div> <div id="foot"> <p id="left">Copyright © 2010 Essential Fitness, LLC</p> <p id="right">Privacy Policy | Terms and Conditions</p> </div> </div> </div> <script type="text/javascript"> <!-- var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1"); //--> </script> </body> </html> '; mysqli_close($dbc); exit(); } else { echo '<p class="error">You must enter all of the profile data.</p>'; } } } // End of check for form submission else { // Grab the profile data from the database $query = "SELECT * FROM userDB WHERE user_id = '" . $_SESSION['user_id'] . "'"; $data = mysqli_query($dbc, $query); $row = mysqli_fetch_array($data); if ($row != NULL) { $currentdate = $row['currentdate']; $bodyweight = $row['bodyweight']; $bmi = $row['bmi']; $bloodpressure = $row['bloodpressure']; $hdl = $row['hdl']; $ldl = $row['ldl']; $totalcholesterol = $row['totalcholesterol']; $triglycerides = $row['triglycerides']; $glucoselevels = $row['glucoselevels']; $bodycomposition = $row['bodycomposition']; $waist = $row['waist']; $hip = $row['hip']; } else { echo '<p class="error">There was a problem accessing your profile.</p>'; } } mysqli_close($dbc); ?> <form id="trackerforms" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <fieldset> <legend>Update Your Stats</legend> <label for="currentdate">Date:</label> <input type="text" id="currentdate" name="currentdate" value="<?php if (!empty($currentdate)) echo $currentdate; ?>" /> <br /> <label for="bodyweight">Body Weight:</label> <input type="text" id="bodyweight" name="bodyweight" value="<?php if (!empty($bodyweight)) echo $bodyweight; ?>" /> <br /> <label for="bmi">BMI (Body Mass Index):</label> <input type="text" id="bmi" name="bmi" value="<?php if (!empty($bmi)) echo $bmi; ?>" /> <br /> <label for="bloodpressure">Blood Pressure:</label> <input type="text" id="bloodpressure" name="bloodpressure" value="<?php if (!empty($bloodpressure)) echo $bloodpressure; ?>" /> <br /> <label for="hdl">HDL Cholesterol:</label> <input type="text" id="hdl" name="hdl" value="<?php if (!empty($hdl)) echo $hdl; ?>" /> <br /> <label for="ldl">LDL Cholesterol:</label> <input type="text" id="ldl" name="ldl" value="<?php if (!empty($ldl)) echo $ldl; ?>" /> <br /> <label for="totalcholesterol">Total Cholesterol:</label> <input type="text" id="totalcholesterol" name="totalcholesterol" value="<?php if (!empty($totalcholesterol)) echo $totalcholesterol; ?>" /> <br /> <label for="triglycerides">Triglycerides:</label> <input type="text" id="triglycerides" name="triglycerides" value="<?php if (!empty($triglycerides)) echo $triglycerides; ?>" /> <br /> <label for="glucoselevels">Fasting Blood Glucose Levels:</label> <input type="text" id="glucoselevels" name="glucoselevels" value="<?php if (!empty($glucoselevels)) echo $glucoselevels; ?>" /> <br /> <label for="bodycomposition">Body Composition:</label> <input type="text" id="bodycomposition" name="bodycomposition" value="<?php if (!empty($bodycomposition)) echo $bodycomposition; ?>" /> <br /> <label for="waist">Waist Size:</label> <input type="text" id="waist" name="waist" value="<?php if (!empty($waist)) echo $waist; ?>" /> <br /> <label for="hip">Hip Size:</label> <input type="text" id="hip" name="hip" value="<?php if (!empty($hip)) echo $hip; ?>" /> <br /> </fieldset> <input type="submit" value="Submit Stats" name="submit" /> </form> </div> <div id="foot"> <p id="left">Copyright © 2010 Essential Fitness, LLC</p> <p id="right">Privacy Policy | Terms and Conditions</p> </div> </div> </div> <script type="text/javascript"> <!-- var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1"); //--> </script> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/208080-new-to-php-problem-updating-database/ Share on other sites More sharing options...
jcbones Posted July 18, 2010 Share Posted July 18, 2010 I think if you changed the line: mysqli_query($dbc, $query); to mysqli_query($dbc, $query) or die(mysqli_error($dbc)); You would find that there is an error in: $query = "UPDATE userDB SET currentdate = '$currentdate', bodyweight = '$bodyweight', bmi = '$bmi', bloodpressure = '$bloodpressure', hdl = '$hdl', ldl = '$ldl' "." totalcholesterol = '$totalcholesterol', triglycerides = '$triglycerides', glucoselevels = '$glucoselevels', bodycomposition = '$bodycomposition', waist = '$waist', hip = '$hip' WHERE user_id = '" . $_SESSION['user_id'] . "'"; HERE: ldl = '$ldl' "." totalcholesterol = '$totalcholesterol' Should be changed to: ldl = '$ldl', totalcholesterol = '$totalcholesterol' Quote Link to comment https://forums.phpfreaks.com/topic/208080-new-to-php-problem-updating-database/#findComment-1087682 Share on other sites More sharing options...
JCowdrick Posted July 18, 2010 Author Share Posted July 18, 2010 Thank you very much. That worked wonderfully. I will keep your suggestion of using or die(mysqli_error($dbc)); to check for errors. Thanks again, you've been a big help. Quote Link to comment https://forums.phpfreaks.com/topic/208080-new-to-php-problem-updating-database/#findComment-1087684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.