Jump to content
Larry Ullman's Book Forums

ScottM

Members
  • Posts

    4
  • Joined

  • Last visited

ScottM's Achievements

Newbie

Newbie (1/14)

0

Reputation

  1. ugh, I'm am idiot. I thought the error was related to mysqli_real_escape_string(), I forgot trim() was a function too Either way thanks Rob, you rock!
  2. Hi, I've been working my way through the book and I seem to have hit an error that I don't know how to resolve. I'm hoping someone could give me some suggestions as to how I can resolve it. I'm on chapter 10 and working on the edit_user.php lesson. I have the script written but when I go to test it by editing a users details I get this error message when I submit the edited form: Fatal error: Function name must be a string in /home/scottm/public_html/tuts/php/edit_user.php on line 36 That error is referring to this line of code: $fn = mysqli_real_escape_string($dbc, $trim($_POST['first_name'])); For the life of me I can't find the issue, I'm hoping a second set of eyes might be able to see where I'm going wrong. Below is the rest of the code I'm working with, any help would be greatly appreciated. Thanks in advance <?php # Script 10.3 - edit-user.php $page_title = 'Edit A User'; include('includes/header.html'); echo '<h1>Edit a User</h1>'; //check for a valid ID through GET or POST if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { //if the form uses $_GET method $id = $_GET['id']; //set the value of $id using GET } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { //or if the form uses $_POST method $id = $_POST['id']; //set the value of $id using POST } else { //if there was a problem echo '<p class="error>This page has been accessed in error.</p>'; include('includes/footer.html'); exit(); } //include the database connection script require_once('../../Connections/mysqli_connect.php'); //check to make sure the form has been submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') { //create an errors array to store all errors generated $errors = array(); //validate the first name if(empty($_POST['first_name'])) { $error[] = 'You forgot to enter your first name'; } else { $fn = mysqli_real_escape_string($dbc, $trim($_POST['first_name'])); // <----------------This is line 36 } //validate the last name if (empty($_POST['lst_name'])) { $error[] = 'You forgot to enter your last name'; } else { $ln = mysqli_real_escape_string($dbc, trim($_POST['last_name'])); } //validate the email address if (empty($_POST['email'])) { $error[] = 'you forgot to enter your Email address'; } else { $e = mysqli_real_escape_string($dbc, trim($_POST['email'])); } //if there where no errors in the error array, check that the email address is not already in use if (empty($errors)) { //test for unique email address $q = "SELECT user_id FROM users WHERE email=$e AND user_id != $id"; $r = @mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 0) { //if the email is unique //make query $q = "UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id='$id' LIMIT 1"; $r = @mysqliquery($dbc, $q); if(mysqli_affected_rows($dbc) == 1) { //if it ran OK //print message echo '<p>The user has been edited</p>'; } else { //if it did not run OK echo '<p class="error">The user could not be edited, dues to a system error. We apologize for the inconvienence</p>'; //public message echo '<p>' . mysqli_errors($dbc) . '<br />Query: ' . $q . '</p>'; //debugging message } } else { //already registered echo '<p class="error">The email address has already been registered</p>'; } } else { //Report the errors echo '<p class="error">The following error(s) occured:<br />'; foreach($errors as $msg) { echo "$msg<br />\n"; } echo '</p><p>Please Try Again</p>'; } //END OF if (empty($errors)) IF STATEMENT } //END OF if($_SERVER['REQUEST_METHOD']) SUBMIT CONDITIONAL //Always show the form //Retrieve the users information $q = "SELECT first_name, last_name, email FROM users WHERE user_id=$id"; $r = @mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { //if there is a valid user_id, show the form //get the users information $row = mysqli_fetch_array ($r, MYSQLI_NUM); //create the form echo '<form action="edit_user.php" method="post"> <p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="' . $row[0] . '" /></p> <p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '" /></p> <p>Email Address: <input type="text" name="email" size="20" maxlength="60" value="' . $row[2] . '" /></p> <p><input type="submit" name="submit" value="Submit" /></p> <input type="hidden" name="id" value="' . $id . '" /> </form>'; } else { //is not a valid user_id echo '<p class="error">This page has been accessed in error</p>'; } //close dtabase connection mysqli_close($dbc); include('includes/footer.html'); ?>
  3. That was it! I knew it was something simple, but for the life f me I was not able to see it. Thank you so much Jonathon, you have no idea how long I spent staring at that code. I'm currently working with Dreamweaver CS4, but I have been using Komodo Edit as well to have access to its syntax highlighting (DW is awful for php). Thanks again I really appreciate the second set of eyes on this script.
  4. Hi, I'm going through Chapter 3, working on calculator.php and I'm not able to get the page to work with the simple form validation. I can get the calculator to work only be commenting out the validation check. I've stared at this for too long and I can't see where the problem is, I'd appreciate a second set of eyes to just look over what I've got and maybe point me in the right direction. When I take the commenting off of the form validation I get this error: I know the problem is somewhere in my validation because with it commented out the calculator works, but when I look at the uncommented validation code I just don't see where the problem is. Thanks in advance for your time. <?php # Script 3.5 - calculator.php // Error handling //ini_set ('display_errors',1);//Let me learn from my mistakes! //error_reporting (E_ALL | E_STRICT);// show me all errors $page_title = 'Trip Cost Calculator'; include('includes/header.html'); //Write the conditional that checks for the form submission by checking the method (whether it's GET or POST) if ($_SERVER['REQUEST_METHOD'] =='POST') { //minimal form validation if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) &&is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) ) { //calculate the results $gallons = $_POST['distance'] / $_POST['efficiency']; $dollars = $gallons * $_POST['gallon_price']; $hours = $_POST['distance'] / 65; //print the results echo '<h1>Estimated Total Cost</h1> <p>The total cost of Driving' . $_POST['distance'] . ' miles, averaging ' . $_POST['efficiency'] . 'miles per gallon, and paying an average of $' . $_POST['gallon_price'] . ' per gallon, is $' . number_format($dollars, 2) . ' . If you drive at an average of 65 mph, the trip will take approximately' . number_format($hours, 2) . ' hours.</p>'; } else {//invalid submit values echo'<h1>Error!</h1> <p>Please submit valid distance, price per gallon and feul efficiency</p>'; } //end of main submission "if" statement //Leave the PHP section and create the html form ?> <h1>Trip Cost Calculator</h1> <form action="calculator.php" method="post"> <p>Distance (in miles):<input type="text" name="distance" /></p> <p>Average Price per Gallon: <span class="input"> <input type="radio" name="gallon_price" value="3.00" />3.00 <input type="radio" name="gallon_price" value="3.50" />3.50 <input type="radio" name="gallon_price" value="4.00" />4.00 </span></p> <p>Fuel Efficiency: <select name="efficiency"> <option value="10">Terrible</option> <option value="20">Decent</option> <option value="30">very Good</option> <option value="50">Outstanding</option> </select></p> <p><input type="submit" name="submit" value="Calculate!" /></p> </form> <?php include('includes/footer.html'); ?>
×
×
  • Create New...