#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Entering values in an array


    I have a data base that controls the classes in a school. I enter classes into the data base via php code (which I wrote with some help) , for example one class every tuesday and thursday between 2 specified dates.

    PHP Code:
    $days = array('tuesday''thursday');
    if (empty(
    $errors))
    {



    while(
    $current_date <= $end_date) {
        if(
    in_array(strtolower($current_date->format('l')), $days)) {
            
    $date_string $current_date->format('Y-m-d');
            
    $values[] = "('$date_string', $ti, '$tch', '$drt', '$grp')";
        }
        
    $current_date->modify("+1 day");
    }
    $values implode(', '$values);
    $query "INSERT INTO clases (fecha, hora, teacher, duracion, grupo) VALUES $values"
    So I want to vary the days without having to manually change them in the code.

    I have tried selecting the days from a drop down box the and entering the values following way.

    PHP Code:
    $cop = ($_POST['daya']);
    $jp = ($_POST['dayb']);
    $days = array('$cop''$jp'); 
    When I do this I get this message

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

    Query: INSERT INTO clases (fecha, hora, teacher, duracion, grupo) VALUES
    If I leave the second day as 'thursday' the query works but only enters the thursday classes.

    Any ideas?
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    There's no reason to quote the values in this line:
    PHP Code:
    $days = array('$cop''$jp'); 
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    OK thanks!
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Also, your code is wide open to attack.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo