Jump to content
Larry Ullman's Book Forums

bahaa

Members
  • Posts

    147
  • Joined

  • Last visited

Posts posted by bahaa

  1. Hi all,

     

    I need some help validation questions and answers in a survey.

     

    I have a survey that can take x number of questions and each questions can have x number of answers.

     

    questions 1

    answer 1

    answer 2

    answer3

     

    question 2

    answer 1

    answer 2

    answer3

     

     

    The questions and their associated answers are coming from the database so I don't know the the values fof each questions and the values of each answer.

    How do I test to make sure the user selected one answer from each question and get the values?

     

    Bahaa,

  2. My web application is going along smoothly thanks to both of you.

     

     

     

    Bahaa, When updating the table containing CreationDate and CreatedBy these values will remain the same. How do you make a query update if they will have the same values. CreationDate and CreatedBy will not change during an query update, therefore are these fields skipped?

     

    Thanks,

    Mark

     

    When you do an update query, you only include the one you want to update.... You don't incclude all fields in your table every table you do a select or update query.... choose only the one you need to do select on or update.

  3. Thanks bahaa, exactly what I was looking for.

     

    Larry mentions above that there are proper headers while retrieving an image, which one did you use?

     

     

    You use the header to retrive the image if you put the image directory out of the root, but if you don't but it out of the root you don't need to use header to retrive the image, you just do like I show you above.

  4. got it. you are using a combination of the two. are you getting errors with the file name extensions being included when inserting into the database?

     

    When you call your images, do you call them from the database or the directory?

     

    I don't get an errors when I insert the image name into the database.

    when you call your image you need to put the path to your image directory and add the variable name that represent the image name in the database.

     

    something like this <img src="../upload/<?php echo $image; ?>" width="" height="" alt ="">

  5. I am only inserting the name of the image to the database not the image its self and I upload the image to a folder on the server.

    I did not show the insert query because it works the same as other insert quiers.

    You insert the name of the image same any other data you insert to the database.

     

    If you need more clarification let me know.

  6. here is the function to validat the file

     

    function ValidateUpload($File)

    {

    global $mysqli;

    if(!empty($File))

    {

    $errors = array();

    // get the file from user

    $fileName = basename($File['name']);

    $tempFilename = $File['tmp_name'];

    $fileSize = $File['size'];

    $fileType = strtolower($File['type']);

    $fileExtension = strtolower(substr(strchr($fileName,'.'), 1));

    $imageInfo = getimagesize($tempFilename);

    $imageInfo = $imageInfo['mime'];

    $width= $imageInfo[0];

    $height = $imageInfo[1];

    //Convert all applicable characters to HTML entities

    $fileName = htmlentities($fileName);

    $fileType = htmlentities($fileType);

    //Validate the type.Should be JPEG , PNG Or GIF

    $allowed = array('image/pjepg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG',

    'image/png', 'image/x-png', 'image/gif', 'image/GIF');

    if(!in_array($fileType, $allowed))

    {

    $errors[]="<p>إمتداد الملف الذي قمت بتحميله غير مصرح به.</p>";

    }// end of if(in_array($_FILES['upload']['type'], $allowed)){

    if(!in_array($imageInfo, $allowed))

    {

    $errors[]="<p>- نوع الملف الذي تحاوله رفعه غير معروف.</p>";

    }

    if(!is_numeric($fileSize))

    {

    $errors[]="<p>- الملف الذي تحاول رفعه غير مصرح به.</p>";

    }

    }//End if(!empty($_FILES['upload']['name']))

    return $errors;

    }

     

     

     

     

    and this is the function to upload the file

     

    function UploadFile($File,$tempFilename, $fileName,$fileExtension)

    {

    global $mysqli;

     

    if(!empty($File))

    {

    $targetPath = "../uploads/";

    if(move_uploaded_file($tempFilename, $targetPath.$fileName))

    {

    if(file_exists($targetPath.$fileName))

    {

    //Generate uniqe file name

    $Image = uniqid(md5(time())).".".$fileExtension;

    //Rename the file to an uniqid version

    rename($targetPath . $fileName, $targetPath . $Image);

    }//end of if(file_exists($target_path/$filename)){

    }//end of if(move_uploaded_file($temp_filename, $target_path.$file_name)){

    } //END if(!empty($_FILES['Image']['name'])){

    return $Image;

    }

     

     

    and here is an example of how I use them in my code

     

    if(!empty($_FILES['Image']['name']))

    {

    $File = $_FILES['Image'];

    $errors = ValidateUpload($File);

    }//End if(!empty($_FILES['upload']['name']))

    else

    {

    $Image = NULL;

    }

     

     

     

     

    if(!empty($_FILES['Image']['name']))

    {

    $File = $_FILES['Image']['name'];

    $fileName = basename($_FILES['Image']['name']);

    $tempFilename = $_FILES['Image']['tmp_name'];

    $fileExtension = strtolower(substr(strchr($fileName,'.'), 1));

    $Image = UploadFile($File,$tempFilename, $fileName,$fileExtension);

    }//END if(!empty($_FILES['Image']['name']))

  7. You can easily upload a file to a directory and assign the image any name you want, weather a user id or random name.

    I read the book a long time ago and I don't remember exactly the exaple but I think there was a problem with the uploading script... I guess the image is uploaded without the extenstion and thats why you why try to access it , it doesn't work.

     

    I will provide you with a code I use to upload image to a directory.

    In my case use random name for my image and I save it in the database with the associated record.

    You don't have to name the image with the user ID and rather than that you can save its name in the database and refer to it by a fk

  8. Like I said, it's not the type of changes that I'm debating, it's the quantity. With your method (as far as I can tell, but maybe I'm misunderstanding something), you only get a record of the creation of the record and the last modification. But let's say that a particular record has been 10 times and by 10 different people, you'd lose all the other modifications information. If you want to retain all that history information efficiently, then you'd need at least one other table, as opposed to including the mods history in the clients table.

     

    That's all I was saying.

    I know you can add as many as fields to track in a seprate table but I was just curious about the things you want to track.

    In such site I would not track such things, If i was handling a sensitiv data like banking information and confidential infromation for patient, then I would track who created the record ,when, who edited it, when was edited, who viewed a record and such things.

  9. Well, it seems like you guys have more or less resolved all the kinks, so I'll leave it at that, but just to clarify:

     

    bahaa's method is perfectly acceptable, but if you go that route, you will not get an entire history of modifications. You will only have the person who added a record and the person who last edited that record. If that's enough, then going with bahaa's method is best.

     

    However, if you want a full list of modifications (which is what I assumed), then you need a separate table/set of tables. You could have one table for all the mods, but you're going to end up with a lot of null values for the FKs, which is not ideal and is why I suggested a separate mods table for each normal table.

     

    As a final point, as bahaa mentioned, if you go with my method, there doesn't need to be any admin information in the clients table.

    What modifications do you can get other than who created the record, updated , what date was created and what date it was updated?

    I can think of the IP address for the one who made the changes but what other things??

    If I was yo go your route I would only make one table not a mod table for each table.

    What if you have a really larg database like a thousand table, would you make another thousand table to track each table?

    I think there is no problem with having nullable Fks in your tracking table since(what I think) when fetching the table the nullable fields are skipped so it won't have any effect on the performance.

  10. yes please

     

    my table called user not admin, I log users to the admin area based on their access level... I have a table called user_group that has a fk in the user table.

    users activate thier membership by an activation link sent to thier email upon registration

     

    here is the table

     

    CREATE TABLE IF NOT EXISTS `user`(

     

    UserID INT UNSIGNED AUTO_INCREMENT,

    UserName VARCHAR(75) NOT NULL,

    FirstName VARCHAR(60) NOT NULL,

    LastName VARCHAR(60) NOT NULL,

    Email VARCHAR(100) NOT NULL,

    Password CHAR(40) NOT NULL,

    PasswordLastUpdated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

    Gender CHAR(1),

    DOB DATE,

    RegistrationDate DATETIME,

    LastVisit DATETIME,

    RegistrationKey VARCHAR(32) NULL,

    TemporaryPassword VARCHAR(55),

    IsActivated TINYINT UNSIGNED DEFAULT 0,

    ReceiveEmail TINYINT UNSIGNED DEFAULT 1,

    IP CHAR(15),

    GroupID INT UNSIGNED NOT NULL COMMENT 'Fk to the usergroup',

    CountryID INT UNSIGNED NOT NULL COMMENT 'Fk to the country table',

     

    CONSTRAINT pk_user_UserID PRIMARY KEY(UserID),

    CONSTRAINT fk_usergroup_user_groupID FOREIGN KEY(GroupID) REFERENCES usergroup(GroupID),

    CONSTRAINT fk_country_user_countryID FOREIGN KEY(CountryID) REFERENCES country(CountryID),

    CONSTRAINT ck_user_userName UNIQUE(userName),

    CONSTRAINT ck_user_userEmail UNIQUE(Email),

    INDEX(userName),

    INDEX(FirstName),

    INDEX(LastName),

    INDEX(IsActivated)

     

    )ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

  11. I am talking about your method not Hartleys. When you have created_by, modified_by fields on tables, is the admin_id related to those tables?

    Yes the admin_id is related to those tables but you don't add the admin_id in the table and instread you make those fields fk to the admin_id

     

    here is an example of my tables

     

     

     

    CREATE TABLE IF NOT EXISTS `category`(

     

    CategoryID INT UNSIGNED AUTO_INCREMENT,

    ParentID INT UNSIGNED,

    Title VARCHAR(120),

    Description TEXT NULL,

    Ordering INT UNSIGNED NOT NULL DEFAULT 1,

    Visible TINYINT DEFAULT 0,

    CreationDate DATETIME,

    CreatedBy INT UNSIGNED NOT NULL COMMENT 'Fk to the user table',

    LastEdited timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

    LastEditedBy INT UNSIGNED COMMENT 'Fk to the user table',

     

     

    CONSTRAINT pk_category_CategoryID PRIMARY KEY(CategoryID),

    CONSTRAINT fk_user_category_CreatedBy FOREIGN KEY(CreatedBy) REFERENCES user(UserID),

    CONSTRAINT fk_user_category_LastEditedBy FOREIGN KEY(LastEditedBy) REFERENCES user(UserID),

    CONSTRAINT fk_category_ParentID FOREIGN KEY(ParentID) REFERENCES category(CategoryID)

    ON DELETE CASCADE,

    INDEX(Ordering),

    INDEX(Visible)

     

    )ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

  12. Ok thanks for clarifying Hartley's suggestion - I now understand what he was talking about.

     

    Here is how I understand Hartley's method below:

     

    mod table

    mod_id

    client_id (FK)

    location_id (FK)

    website_id (FK)

    image_id (FK)

     

     

    I would like to also understand your approach though. Explain your method - did you add at the admin user id to each table (the tables that contain the created by, modified by fields)?

    I am workin on webe site and in my database design i added those fields to my tables

    CreationDate, LastEdited, CreatedBy and LastEditedBy.... the createdBy and LastEditedBy are fk to the user table.

    When an admin make a changes to a record in a table, I use the userID in the session to add the userID to those fields

    for example, if the admin Bahaa make a changes to x record in the category table I add the userID for bahaa in the LastEditedBy fields.

     

    If you still don't understand any point let me know and I will be happy to help you.

  13. thanks bahaa, i thought it was possible. though through hartley's example there are alternatives.

     

    bahaa - do you have the user admin id foreign keys in those tables? Are they related?

     

    This would put the nail in the coffin as far as my initial question(s) are concerned. If not, then I'll have to find a solution

     

    thanks,

    Mark

     

    No, you should not add the admin_id to those table. the admin_id in the mod table should be a fk to the admin table

  14. hi hartley, i get an idea of what you mean. I just wanted to get a visual of what how a client_mods table would relate to normal tables. For the sake of simplicity, one example is fine.

     

    If I understand correctly, see example below:

     

    admin table

    admin_id

    admin_firstname

    admin_lastname

    admin_username

    admin_email

     

    client table

    client_id

    admin_id

    client_company_name

     

    client_mod table

    client_id

    last_modified

    date_created

     

    location table

    location_id

    client_id

    location_street_address

    location_city

    location_state

    location_zip

     

    location_mod table

    location_id

    last_modified

    date_created

     

    If you want to follow Hartley suggetion about making sperate table to track the changes, take off the admin_id from the clinet table because don't need it there.

    Second, the tracking table should be for all of other tables, you can't make a tracking table for each table.

    In the tracking table add the other tables ids.

    When an admin make a changes to the client table for example you add this record to the mod table the id for the client_id the date and the id for the admin and the ids for the other tables should be null

  15. You can easily add a last modified field to a table like the clients table, but that's not going to provide you with a history of changes, nor will it provide you with who made those changes.

     

    My point is, you can't just tack some extra fields onto the existing tables and call it a day. You need to create completely new tables to track the entire history of changes made to the clients, locations, and websites tables, etc.

     

    Probably the most logical solution would be to have another table for each normal table, which keeps track of the modifications made to that table, which is where the example table client_mods came from (with "mods" being short for "modifications").

     

    Anyway, I think you get the point.

     

    He can add those fileds to every table and get who made the changnes, I have them in my table and I manage to get such information.

    For example, in admin area I have a page called categories_manager that display the categorty information plus the creation date, modification date, created by and last eduted by for each record.

  16. That being said, I was confused as to why on chapter 9 there is a movie-actors table inbetween the movie table and the actors table. That is why on my first chart, I have so many tables.

     

    I don't remember the movie tables and I did not read the whole book but I think the table in between is because the many to many relation ship in movies case.

    An actor can have many movies and a movie can have many actors.

  17. You cam add auditing fields to the other tables and you can add more than one field to each table

    example of fields:

    CreationDate

    CreatedBy

    LastEdited

    LastEditedBy

     

     

    The client table did not have a contact name like first name and last name.

    I noticed you use varchar(255) for all of your varchar fields, you should only use the size you need , for example you won't need 255 for a city name or first name. for the city name I don't think you would need more than varchar(30), first name maybe 55.

     

    for the location, if you want to have more than one address for the same person then you to make a bridge table between the client table and the adress table. You also can break the location table into these tables: country and city

    • Upvote 1
×
×
  • Create New...