Jump to content
Larry Ullman's Book Forums

phpRob

Members
  • Posts

    58
  • Joined

  • Last visited

  • Days Won

    2

Posts posted by phpRob

  1. Didn't think it would be wise to give away credentials. It was just a bit of fun really Larry, didn't want people to expect anything mindblowing, I just wanted to go through Chapter 13 with a different look to the user interface. I will probably use this base to add in a user registration, keep different users' details stored in the database and create a new quotes field within the database so that I could seperate different kinds of quotes, e.g. films, sports etc. That I'm sure I will learn in the second of your books hopefully.

  2. Hi Guys and Gals,

     

    Not been posting much recently, my php/mysql learning was put on hold durning the Christmas Hols and I'm just going over a few things to refresh before I hit the PHP & MYSQL for dynamic websites book.

     

    To refresh I went back over the last chapter (13) and thought I'd give it my own stamp if you like. Have a look and please rip it apart as it's just for learning purposes. Main reason for this was to gain a better feeling of how HTML, CSS, PHP and MYSQL all interact with each other, as well as gaining some practice with hosting a live dynamic website as I've mostly been testing locally.

     

    As in the book the login credentials are:

     

    Edit: 'm not sure if this is safe to publically give away the login credentials?

     

    Like I said if there's anything I've missed or that can be improved upon (within the PHP for the web book scope) please, please say :)

     

    The url is:

     

    http://www.blueberry....uk/gibberjabba

     

    Thanks

    Rob

    • Upvote 1
  3. Hey all,

     

    I'm currently adopting Chapter 13 into my own quotes database mini-site. The first task I've come across is on the login page.

     

    After the user has logged in I want it to re-direct to the homepage (index.php) rather than displaying a simple message as the following script does.

     

    My question is, how can I re-write the following script as per the book to allow for header() to be called? Would I have to use output buffering, or is there a bette way?

     

     
    <?php // Script 13.5 - login.php
    /* This page lets people log into the site. */
    // Set two variables with default values:
    $loggedin = false;
    $error = false;
    // Check if the form has been submitted:
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Handle the form:
    if (!empty($_POST['email']) && !empty($_POST['password'])) {
    
     if ( (strtolower($_POST['email']) == [email=""]'me@example.com'[/email]) && ($_POST['password'] == 'testpass') ) { // Correct!
    
      // Create the cookie:
      setcookie('Samuel', 'Clemens', time()+3600);
    
      // Indicate they are logged in:
      $loggedin = true;
    
     } else { // Incorrect!
      $error = 'The submitted email address and password do not match those on file!';
     }
    } else { // Forgot a field.
     $error = 'Please make sure you enter both an email address and a password!';
    }
    }
    // Set the page title and include the header file:
    define('TITLE', 'Login');
    include('templates/header.html');
    // Print an error if one exists:
    if ($error) {
    print '<p class="error">' . $error . '</p>';
    }
    // Indicate the user is logged in, or show the form:
    if ($loggedin) {
    
    print '<p>You are now logged in!</p>';
    
    } else {
    print '<h2>Login Form</h2>
    <form action="login.php" method="post">
    <p><label>Email Address <input type="text" name="email" /></label></p>
    <p><label>Password <input type="password" name="password" /></label></p>
    <p><input type="submit" name="submit" value="Log In!" /></p>
    </form>';
    }
    include('templates/footer.html'); // Need the footer.
    ?>
    

     

    Thanks

  4. Hi guys, having finished Chapter 13 find myself at the Review section.

     

    I'm having trouble answering the following two review questions:

     

    How would the is_administrator() function be called to check for the same cookie - named Samuel - with a different value? A different cookie - not named Samuel - with a different value? I'm still unsure about this please help!

     

    AND

     

    What would be some other good ideas for user-defined functions wit this site? Hint: look for repeated code. Would printing an error message be viable for a function as this seems to be repeated a lot in the code throughout the project? What other ones could there be?

     

    Thanks

  5. PHP for the web covers the fundamentals of php at a much slower pace than that of the php & mysql book. It alsor includes an introduction chapter to sql and mysql which i found very helpful. I've nearly finished the php for the web book and will be working through the php & mysql book (4th edition) in the new year which I'm already looking forward to.

     

    Like Jonathon said, having both books by your side would be a great decision.

    • Upvote 1
  6. Hi Redscouse,

     

    I guess I'm at the exact same position you found yourself in all that time ago, I'm just starting out at php & mysql having nearly finished Larrys' PHP for the web book. I'm hoping after reading Larrys other books and completing a couple of my own projects I'll be in a position to laugh at my own forum posts oneday!!

  7. Do you have to quote the values in the $_POST variable?

     

    function validate_text_input($input_name, $err_msg) {
    if (empty($_POST['$input_name'])) {
     $errors[] = $err_msg;
      } else {
    	if (get_magic_quotes_gpc()) {
    	 $input_name = mysqli_real_escape_string($dbc, stripslashes(trim($_POST['$input_name']))); }
    	else {
    	 $input_name = mysqli_real_escape_string($dbc, trim($_POST['$input_name']));
    	 }
    	}
    }

     

    Excuse me if this is nonsense, I'm just a beginner!

    • Upvote 1
  8.  

    But I do see where you did get confused. Larry reccomends to go look at the php manual early on in the book. When I did that I found it to be very confusing. One tip though. http://tw2.php.net/m....prototypes.php Look here. It is on 'how to read a function definition' . I found this very helpful in understanding the php manual. Frankly, I think page should not be so deeply buried.

     

    I too got a bit overwhelmed using the php manual to begin with, especially reading the User Contributed Notes, as a beginner I don't recommend reading those as they'll probably confuse the heck outta ya! . This is a helpful page on reading functions though thanks :)

  9. Just started working through the final chapter. I'm struggling to understand the is_administrator function on page 384, in particular this line (in bold):

     

    function is_administrator($name = 'Samuel', $value = 'Clemens') {

     

    if (isset($_COOKIE['name']) && $_COOKIE['name'] == $value) {

     

    return true;

     

    } else {

     

    return false;

     

    }

     

    }

     

    Now I understand that it tests to see whether the cookie name 'Samuel' is set but not why it tests whether 'Samuel' is equel to 'Clemens'??

     

    Can someone please explain this? It's probably very simple!!

  10. Having finished Chapter 12 of the PHP for the web the first pursue objective is to start reading more about MySQL. To continue in my learning following your books Larry I managed to purchase a used copy of your MySQL (second edition) book. Would this be a good resource for furthering my education of MySQL? Also would I be best reading your PHP and MySQL for dynamic websites first (I have also ordered this book!) Bare in mind my reading of Chapter 12 is the first time I've ever touched Mysql!

     

    Thanks

  11. Ran a mysql_error and it turns out my query had an error.

     

    $query = 'UPDATE entries SET title = ' . $title . ', entry = ' . $entry . ' WHERE entry_id = ' . $_POST['id'] . ' LIMIT 1';

     

    I used this instead (from the book) and it worked:

     

    $query = "UPDATE entries SET title='$title', entry='$entry' WHERE entry_id='{$_POST['id']}' LIMIT 1";

     

    My question would now be how could I have got this to work using single quotes on my query?

     

    N.B The reason I didn't use the book code in the first place is I try to hand code it myself without reference for learning purposes.

  12. Hi all, I'm having a problem editing the database whilst running the edit_entry.php script p.373.

     

    I'll include my script, apologises if it code bloat!

     

    Just to add I can, add, view and delete records in my database fine.

     

    <?php
    
    include('mysql_connect.php');
    
    if ( isset($_GET['id']) && is_numeric($_GET['id']) ) {
    
    $query = 'SELECT title, entry FROM entries WHERE entry_id = ' . $_GET['id'] . '';
    
    if ( $result = mysql_query($query, $dbc)) {
    
    	$row = mysql_fetch_array($result);
    
    	print '
    	<form action="edit_entry.php" method="post">
    	Entry Title: <input type="text" name="title" value="' . $row['title'] . '" />
    	Entry Text: <textarea name="entry" rows="5" cols="40">' . $row['entry'] . '</textarea>
    	<input type="hidden" name="id" value="' . $_GET['id'] . '" />
    	<input type="submit" name="submit" value="Edit entry" />
    	</form>
    	';
    
    } else {
    
    	print 'The entry could not be accessed. The query being ran is ' . $query . '';
    
    }
    
    } elseif ( isset($_POST['id']) && is_numeric($_POST['id']) ) {
    
    $problem = FALSE;
    
    if ( !empty($_POST['title']) && !empty($_POST['entry']) ) {
    
    	$title = $_POST['title'];
    	$title = trim(strip_tags($title));
    	$title = mysql_real_escape_string($title, $dbc);
    
    	$entry = $_POST['entry'];
    	$entry = trim(strip_tags($entry));
    	$entry = mysql_real_escape_string($entry, $dbc);
    
    } else {
    
    	$problem = TRUE;
    	print '<p class="error">Please enter a title and entry</p>';
    
    	}
    
    if (!$problem) {
    
    	$query = 'UPDATE entries SET title = ' . $title . ', entry = ' . $entry . ' WHERE entry_id = ' . $_POST['id'] . ' LIMIT 1';
    
    	$result = mysql_query($query, $dbc);
    
    	if (mysql_affected_rows($dbc) == 1) {
    
    		print '<p>The entry has been updated</p>';
    
    	} else {
    
    		print '<p class="error">The entry could not be editied. The query being run is: ' . $query . '</p>';
    
    	}
    }
    
    } else {
    
    print '<p class="error">This page was accessed in error</p>';
    
    }
    
    mysql_close($dbc);
    
    ?>

     

    After running the above script I get the error

     

    
    
    The entry could not be editied. The query being run is: UPDATE entries SET title = Number 9, entry = Blaa Blaa WHERE entry_id = 11 LIMIT 1

  13.  

    That makes me think I do not have my table set up properly....

    Page 380 didn't give a whole lot of details as to how to set up but here is what my table looks like:

     

    <?php
    $dbc - mysql_connect('localhost', 'root', 'spring');
    if ($dbc) {
    $query = 'CREATE TABLE quotes ( quote_id INT UNSIGNED NOT NULL AUTO_INCREMENT, quote TEXT NOT NULL, source VARCHAR(100) NOT NULL, favorite TINYINT(1) UNSIGNED NOT NULL, date_entered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (quote_id))';
    }
    mysql_close($dbc);
    
    ?>
    

     

     

    You may want to start with subsituing the '-' with '=' on this line...

     

    $dbc = mysql_connect('localhost', 'root', 'spring');

    • Upvote 1
×
×
  • Create New...