Jump to content

designanddev

Members
  • Posts

    54
  • Joined

  • Last visited

Posts posted by designanddev

  1. You know, this is quite basic stuff. So it would seem that you might want to spend a little bit more time studying the basics of PHP, as well as planning (with pen an paper) how you want your code to work. Doing so will make things a lot cleared for you, and save you a lot of time in the long run.

     

    As for the example:

    // Only create uploader object if a file has been uploaded.
    if (!empty ($_FILES['photo']['name'])) {
    $uploader = new Photograph ();
    
    // Code to handle the upload.
    }

     

    Thanks a lot, will work on it

  2. Ok i have a class that uploads files, but i also would like it to submit without files being uploaded, the file only submits if a file has been uploaded, i want users to have the option not to upload if they didn't need to here is my code class

     

    class Photograph{
    
    public $id;
    public $filename;
    public $type;
    public $size;
    public $content;
    public $caption;
    public $category_id;
    public $admin_id;
    public $created;
    private $temp_path;
    protected $upload_dir = "images";
    public $errors = array();
    protected $upload_errors = array(
    UPLOAD_ERR_OK => "NO errors",
    UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize",
    UPLOAD_ERR_FROM_SIZE => "Larger than form MAX_FILE_SIZE",
    UPLOAD_ERR_PARTIAL => "Partal upload",
    UPLOAD_ERR_NO_FILE => "No file",
    UPLOAD_ERR_NO_TMP_DIR => "No temporary directory",
    UPLOAD_ERR_CANT_WRITE => "Can't write to disk",
    UPLOAD_ERR_EXTENSION => "File upload stopped by extension",
    );
    //passes in $_FILE(['uploaded_file']) as an argument!
     public function attach_file($file="") {
     // Perform error checking on the form parameters
     if(!$file || empty($file) || !is_array($file)) {
       // error: nothing uploaded or wrong argument usage
       $this->errors[] = "No file was uploaded.";
       return false;
     } elseif($file['error'] != 0) {
       // error: report what PHP says went wrong
       $this->errors[] = $this->upload_errors[$file['error']];
       return false;
     } else {
      // Set object attributes to the form parameters.
       $this->temp_path  = $file['tmp_name'];
       $this->filename   = basename($file['name']);
       $this->type	   = $file['type'];
       $this->size	   = $file['size'];
      // Don't worry about saving anything to the database yet.
      return true;
     }
    }
    
    
    public function getcontent(){
    strlen($this->content) > 10;
    }
    
     public function save() {
     // A new record won't have an id yet.
     if(isset($this->id)) {
      // Really just to update the caption
      $this->update();
     } else {
      // Make sure there are no errors
    
      // Can't save if there are pre-existing errors
       if(!empty($this->errors)) { return false; }
    
      // Make sure the caption is not too long for the DB
       if(strlen($this->caption) > 255) {
       $this->errors[] = "The caption can only be 255 characters long.";
       return false;
      }
    
       if(empty($this->caption)) {
       $this->errors[] = "please add a caption.";
       return false;
      }
    
       // Can't save without filename and temp location
       if(empty($this->filename) || empty($this->temp_path)) {
      $this->errors[] = "The file location was not available.";
      return false;
       }
    
      // Determine the target_path
       $target_path = "/home/designanddev.co.uk/public_html/blog/".$this->upload_dir."/".$this->filename;
    
       // Make sure a file doesn't already exist in the target location
       if(file_exists($target_path)) {
      $this->errors[] = "The file {$this->filename} already exists.";
      return false;
       }
    
      // Attempt to move the file
      if(move_uploaded_file($this->temp_path, $target_path)) {
     // Success
       // Save a corresponding entry to the database
       if($this->create()) {
     // We are done with temp_path, the file isn't there anymore
     unset($this->temp_path);
     return true;
       }
      } else {
       // File was not moved.
      $this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
      return false;
      }
     }
    }
    

  3. There is no way for us to tell you since we have no idea what those variables contain. Also. I assume you are getting an error, so what is it? In many cases an error is reported on a particular line, but the actual error is on a previous line. It's just not till the line in question that the PHP parser gets stuck.

     

    this variables contain numbers when i hard code them into the string they work fine

  4. Can please someone help me by telling me what is wrong with this string i have wrote, were is the error what do i need to do to correct it.

     

    Kind Regards

     

    $json_content = file_get_contents('https://graph.facebook.com/'.$facebook_page_id.'/feed?access_token='.$access_token);
    

  5. i keep getting error messages when running this code below but it looks exactly the same as the tutorial i copied it from, i was wondering if someone could take a look and see if the can find my bug

     

    Kind Regards

     

    $facebook_pade_id = 'id';
    $access_token = 'token';
    $number_of_posts = 5;
    $json_content = file_get_contents('http://graph.facebook.com/'.$facebook_page_id.'/feed?access_token='.$access_token);
    $raw_data = json_decode($json_content);
    $i=0;
    foreach($raw_data->data as $feed) {
    $feed_id = explode("_", $feed->id);
    if (!empty($feed->message)) {
    $i++;
    if($i<($number_of_posts+1)) {
    ?>
    <div class="fb_update">
    <?php if (!empty($feed->likes->count)) { ?>
    <div class="fb_likes">
    <?php echo $feed->likes->count; ?>
    </div>
    <?php } ?>
    <?php if (!empty($feed->created_time)) {?>
    <div class="fb_date">
    <?php echo date('F j, Y', strtotime($feed->created_time));?>
    </div>
    <?php } ?>
    <div class="fb_message">
    <?php echo $feed->message; ?>
    </div>
    </div>
    <?php }?>
    <?php }?>
    

  6. Hi all i have nearly accomplish this registration form, i just have a minor problem with matching the post values to whats already stored in the database to then return back a message to the user letting them know whether someone has taken there input entry username/email

     

    Heres my code i can compare both username and email fields and also echo the message letting the user know if what soever has already been taken but when it queries the check for the username and i insert a a string that currently in the database it comes up with the error message and also ll does the elseif statement and inserted the records into the database even though its already taken. but the email check stops and doesn't insert the query if there a duplicate.

     

    public function Reg() {
    global $database;
    if(empty($this->username)){
      $this->errors[1] = "You forgot to enter your
      Username."; 
    }
    if(empty($this->email)){
      $this->errors[2] = "You forgot to enter your
      email."; 
    }
    if(empty($this->last_name)){
      $this->errors[3] = "You forgot to enter your
      last name."; 
    }
    if(empty($this->first_name)){
      $this->errors[4] = "You forgot to enter your
      first name.";   
    }
    if(empty($this->password)){
      $this->errors[6] = "You forgot to enter your
      password.";   
    }
    if ($this->password != $this->password2) {
    $this->errors[5] = 'Your password did not
    match the confirmed password.';
    }
    
    elseif(empty($this->errors)){
    // Register the user in the database...
    $match_user = "SELECT * FROM users WHERE username ='{$this->username}' LIMIT 1";
    $result1 = mysql_query($match_user,$database->connection);
    $num1 = mysql_num_rows($result1);
    $match_email = "SELECT * FROM users WHERE email ='{$this->email}' LIMIT 1";
    $result2 = mysql_query($match_email,$database->connection);
    $num2 = mysql_num_rows($result2);
    if($num1 > 0){
    echo "username already exist";
    }
    if($num2 > 0){
    echo "email already exist";
    }
    else{
    $insert = "INSERT INTO users (username, password, first_name, last_name, email) VALUES ('{$this->username}','{$this->password}','{$this->first_name}','{$this->last_name}','{$this->email}')";
    $r = mysql_query ($insert,$database->connection);
    }
    if($r){
    echo "Thank you for registering";
    }
    }
    }//End of Reg
    

     

    Kind Regards

  7. Don't start new threads for the same problem. Merging this thread with your previous one...

     

    In your previous thread, someone specifically stated you needed single quotes around the value in the query and they also specifically stated why you needed them -

     

    What i dnt get is that i have been using double quoutes for my queries all along, can someone explain why?

     

    Kind Regards

  8. I fixed the problem i by changing my query syntax to this...

     

    $sql = "SELECT * FROM users WHERE first_name ='{$this->first_name}' LIMIT 1";
    

     

    But i am not sure why this works and the way i normally do my variables in queries did not work,

    would be nice to hear that explanation.

     

    Kind Regards

     

    Leo Scott

  9. Sorry ladies and gents i am still having trouble passing the posted form values into my method query, i can select all and it works fine but when i start to use WHERE clauses it goes all wrong here's my code please take a look.

     

    Kind Regards

    Submit

    if(isset($_POST['submit'])) {
    $new_user = new User();
    $first_name = $new_user->first_name = $_POST['first_name'];
    $last_name = $new_user->last_name = $_POST['last_name'];
    if($new_user->reg()){
    
    }
    else {
    $first_name = "";
    $last_name = "";
    }
    }
    

     

    Method

    public function Reg() {
    global $database;	
    if(empty($this->last_name)){
      $this->errors['one'] = "You forgot to enter your
    					last name.";		
    }
    if(empty($this->first_name)){
      $this->errors['two'] = "You forgot to enter your
    					first name.";				
    }
    
    elseif(empty($this->errors)){
    // Register the user in the database...
    
    $sql = "SELECT id FROM users WHERE first_name =".$this->first_name." LIMIT 1";
    $result = mysql_query($sql,$database->connection);
    while($row = mysql_fetch_assoc($result)){
    echo $row['first_name'];
    }
    
    }
    }//End of Reg
    

  10. +1

     

    In programming, EVERYTHING matters, because computers only do EXACTLY what your code tells them to do.

     

    $this->first_name contains a string and must be enclosed in single-quotes within the query statement so that it will be treated as a literal string instead of a mysql keyword or a column/table/database name. If you had some error checking logic in your code to get the mysql error message that is occurring, it would be complaining about an unknown column name that is the value of the entered first_name.

     

    ok could you show me an example, i can not structure this logic

  11. I manage to get the error message for when fields have not been filled out, now i am beginning to write the process of checking to see

    if a current user already exists, is my method query being past the post? i am not sure if it is that why it may have the error

     

    Kind Regards

     

    Heres my function...

     

    // Check for an email first name:
    public function Reg() {
    global $database;
    if(empty($this->last_name)){
      $this->errors['one'] = "You forgot to enter your
      last name."; 
    }
    if(empty($this->first_name)){
      $this->errors['two'] = "You forgot to enter your
      first name.";   
    }
    elseif(empty($this->errors)){
    // Register the user in the database...
    $sql = "SELECT id FROM users WHERE first_name =" . $first_name . " LIMIT 1";
    $result = mysql_query($sql,$database->connection);
    while($row = mysql_fetch_assoc($result)){
    echo $row['first_name'];
    }
    }
    }//End of Reg
    

     

     

    Heres the call...

    if(isset($_POST['submit'])) {
    $new_user = new User();
    $new_user->first_name = $_POST['first_name'];
    $new_user->last_name = $_POST['last_name'];
    if($new_user->reg()){
    
    
    }
    else {
    $first_name = "";
    $last_name = "";
    }
    }
    

  12. I keep getting this error ----> Warning: mysql_fetch_assoc() expects parameter 1 to be resource,

     

    Code below

     

    $sql = "SELECT id FROM users WHERE first_name =".$this->first_name;
    $result = mysql_query($sql,$database->connection);
    while($row = mysql_fetch_assoc($result)){
    echo $row['firstname'];
    }
    

  13. ok il post something real simple... and all i want to do is access these errors on the other side, outside of the class

     

    public function Reg() {
    if(empty($first_name)){
      $this->errors[] = 'You forgot to enter your
      username.';
      return false;
    }
    if(empty($last_name)){
      $this->errors[] = 'You forgot to enter your
      lastname.';
      return false;
    }
    
    elseif(empty($this->errors)){
    echo "hello";
    }
    }
    

  14. Now ive got this error..

     

    Fatal error: Access to undeclared static property: User::$errors

     

    if(empty($first_name)){
      User::$errors[] = 'You forgot to enter your
      username.';
      return false;
    
    }
    if(empty($last_name)){
      User::$errors[] = 'You forgot to enter your
      lastname.';
      return false;
    }
    

  15. try using

    self::errors[] = "you forgot...";

    incase it's something funky about the static method call, failing that you can always try

    User::errors[] = "you forgot...";

     

    Now ive got this error..

     

    Fatal error: Access to undeclared static property: User::$errors

×
×
  • 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.