Jump to content
Larry Ullman's Book Forums

margaux

Members
  • Posts

    453
  • Joined

  • Last visited

  • Days Won

    52

Posts posted by margaux

  1. My response may sound a little harsh but if you post this sort of topic on other forums you will get a much harsher response.

     

    1. the book clearly states that it is not for beginners - if you are not able to resolve this problem, you will have difficulties with the rest of the book;

    2. your problem is due to not understanding file structure;

    3. to debug errors, you need to provide more information. What home page? Are you referring to the first or the second example in this book? Without more information about files, coding etc, there is not enough information to give you any kind of useful help.

     

    Read the forum guidelines and you may want to start off with this book which is very good.

    • Upvote 2
  2. I dont have this book so I'm guessing at what you're trying to do.

     

    By submitting the form without making any changes, the script is probably using the previous values that had been entered. The error is thrown because an update expects change and will return false if you try to do an update without actually changing any values. While in principle no error is made, technically there is an error.

     

    After the form has passed validation you might want to clear the $_POST array to prevent exactly this situation, which can be confusing to the user.

    • Upvote 1
  3. I believe it is possible to query a database via a remote host using ssh2_tunnel but you need to ensure you have the correct permissions.

     

    My opinion on the larger question you ask is that the company would want to change its web hosting arrangements to ensure it has full control of its assets (files, databases etc)  and is able to change the arrangements as its hosting requirements change without too much hassle. It may be that your client has to go through some short term pain to get to a position that will be more suitable for the longer term.

  4. Thanks for the reply Edward. I think you may have misunderstood my question or I'm not completely understanding your response.

     

    The User and Page classes were originally set up with Id and creatorId respectively. In order to display the creatorName which already exists as username in the user class I have added the creatorName attribute (and getCreatorName method) to the Page class. creatorName is not a field in the pages table.

     

    Is it redundant to have name in both classes (username and creatorName in User class and Page class)? Its just 2 attributes currently, but if I decided the Page class wanted to manipulate a lot more data about the creator there would be alot of overlap between the 2 classes? Does that matter in classes?

  5. I'd like to display the page author's name on the single post page. My approach:

    •  add a creatorName attribute and a corresponding getCreatorName method to the Page class
    • in page.php, change the the query to
      $q = "SELECT id, creatorId, title, content, DATE_FORMAT(dateAdded, '%e %M %Y') AS dateAdded, u.username AS creatorName FROM pages AS p INNER JOIN users AS u ON u.userId=p.creatorId WHERE p.id= $id";
      
    • in page.html - use $page->getCreatorName(); to display the name

    This works nicely and I like the way the code structure enables me to slot in a new attribute easily. I guess this is the advantage of using OOP.

     

    My question - I have the page creator's name as an attribute in both classes, is this approach the best way? I was considering changing the FETCH_MODE to be able to access the username without having to add it as an attribute to class.  Is one way better than the other or is it a matter of preference.

     

    I like the first way because I have instantiated the page object and have access to all its methods and attributes if I decide I want to do more processing with it.

  6. I'm not sure there is an easy way as the query is first sent and the variables are sent later. What exactly is the error you're getting? I often copy and paste the query into something like phpmyadmin and substitute real values for the variables. If that does not produce an error, echo out the variables before binding them to ensure they are valid. Its difficult to be more helpful without more information. Show us the code for the prepared statement and what error you're getting.

  7. If I understand correctly, one of the first things the pages do is to require the utilities file which autoloads the classes, starts the session and sets the $user variable depending on whether or not $_SESSION['user'] is set.  If it doesnt exist, $user is set to null.

     

    When a user logs in the method setFetchMode with PDO::FETCH_CLASS parameter is used to create the $user object and $_SESSION['user'] is created and set to the $user object which PHP automatically serialises so that the object can be stored in the session variable.

     

    There is an awful lot of processing going on in a few lines of code because some of the php functions do it all for you. Its good to understand what php is doing so you can use it productively.

     

    Good question - made me revisit some of these functions and hopefully my understanding of them has deepened.

    • Upvote 1
  8. research another host: (HostGator; iPage; DreamHost; Go Daddy, etc)

    If you do decide to go with another host, think twice about switching to Go Daddy. The support is not great and I found updating my site very slow (also the ads they ran during the super bowl were so appalling that it is reason enough to boycott them :)). I have since switched to csn-uk who are brilliant on support and not expensive. But if you're outside the uk they may not be a good option.

     

    I think stored procedures look way scarier than prepared statements

    Totally agree. Its worth learning prepared statements even if you get the stored procedures working. They're better for security and performance, particularly if you're running the same query multiple times.

     

    Prepared statements basically involve 3 steps

    1. prepare the statement by defining the placeholders to be used in your query. Placeholders indicate the places where variables will be provided at run time. A simple SELECT statement "SELECT * FROM users WHERE username='$un' && password='$pw'  becomes "SELECT * FROM users WHERE username = ? && password =?). Question marks are the placeholders for the variables you will bind in the next step. So first you prepare the statement with
      $statement = mysqli_prepare($dbconnection, $query );
      

       

    2. bind the parameters to the statement

      mysqli_stmt_bind_param($statement, 'ss', $un, $pw);
      

       

      The second paramenter lists in order the variable types of the parameters to be bound - in this instance I am binding 2 strings. For a more lengthy query you might get something like ssiisss - that would indicate you are binding 7 variables 2 strings then 2 integers then 3 strings. (Other variable types are supported.) The last parameters, again in order, specify the variables to be bound.
    3. execute the statement -
      mysqli_stmt_execute($statement); 
      

       

     

     

  9. Yes - worth the effort to learn how to use the PDO class. It takes an object oriented approach to connecting to most database systems - mysql, oracle, postgreSQL, SQLite. So if your project needs to change databases, you only need to change one line of code. I'm currently learning it myself  - Larry covers it in the 3rd edition of his book PHP Advanced and you can check out this tutorial.

  10. if (isset($_REQUEST['age'])) {
      
      echo '<p><b>You are ' . $_REQUEST['age'] . '!</b></p>';
      
    }
    

     works nicely. I was thinking that he could do a bit more

    $ages=array('0-29' => 'young', '30-60'=> 'middle aged', '60+'=>'old');
        if (isset($_REQUEST['age'])) {
            $age = $_REQUEST['age'];
            echo '<p><b>You are ' . $age .'!</b></p>';
            echo '<p><b>You are ' . $ages[$age] .'!</b></p>';
    	}
    

    and in the html

    <form action="#" method="post">
    <p>Age: 
    <select name="age">
    <?php
    foreach ($ages as $key=> $value) {
    echo "
    <option>{$key}</option>";}
    
    ?>
    </select>
    

     

  11. There's nothing wrong with your code but you might want to consider incorporating an associative array which would reduce your if elseif statement down to one line of code. Also if you are using a select field in your html form it would allow you to code all your options with just one line. It's a good way to test your understanding of arrays. Let us know if you have any questions :)

  12. To clarify my understanding, it would be good to get some feedback re some of the comments in this thread.

     

    Static methods do not get inherited by childeren that extends a parent.

    In my parent class(databaseObject) is a public static function find_by_all() which I'm accessing via child classes (Comment and Photograph)

    If one method is static I think they all have to be?

     I've not seen this before and my classes have both static and non static functions.

     

    new self() // will return the class that have the method definition (if u dont override the method)

    I understood the point of inheritance was that the child class could access the parent method through an instance of itself (the child) without the need for overriding.
     

  13. Getting closer - I knew that something was wrong with how the object was being instantiated.. Have changed the method to use get_called_class in the instantiate method.

    protected static function instantiate($record) {
       $class_name = get_called_class();
       $object = new $class_name;
       foreach ($record as $attribute=>$value) {
          if ($object->has_attribute($attribute)){
    	   $object->attribute = $value;
          }
       }
       return $object;
    }
    

    now the object looks like this

    array(4) {
      [0]=>
      object(Photograph)#5 (11) {
        ["db_fields"]=>
        array(5) {
          [0]=>
          string(2) "id"
          [1]=>
          string(8) "filename"
          [2]=>
          string(4) "type"
          [3]=>
          string(4) "size"
          [4]=>
          string(7) "caption"
        }
        ["id"]=>
        NULL
        ["filename"]=>
        NULL
        ["type"]=>
        NULL
        ["size"]=>
        NULL
        ["caption"]=>
        NULL
        ["temp_path":"Photograph":private]=>
        NULL
        ["upload_dir":protected]=>
        string(6) "images"
        ["errors"]=>
        array(0) {
        }
        ["upload_errors":protected]=>
        array(8) {
          [0]=>
          string(26) "File uploaded successfully"
          [1]=>
          string(41) "File size larger than upload_max_filesize"
          [2]=>
          string(40) "File size larger than form MAX_FILE_SIZE"
          [3]=>
          string(28) "File only partially uploaded"
          [4]=>
          string(19) "File does not exist"
          [6]=>
          string(34) "Temporary directory does not exist"
          [7]=>
          string(19) "Can't write to disk"
          [8]=>
          string(34) "File upload prevented by extension"
        }
        ["attribute"]=>
        string(10) "sunflowers"
      }
    

    Need to figure out why the values from the d/b aren't getting into the object.

×
×
  • Create New...