SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot charles_i's Avatar
    Join Date
    Apr 2007
    Location
    Toronto, Canada
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I insert a PHP variable to an array for SQL table?

    Hello,

    I have a variable 'link' that's being passed through GET (URL from previous page) that I need to insert into my SQL table. Something like this:

    Code:
    $ID_workshop = $_GET['link'];
    
    $form_vars = array('ID_num','day','workshop','comment','ID_workshop');
    How do I get $ID_workshop into the field 'ID_workshop'? I've tried every combination of single and double quotes that I can think of.

    Thanks

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    did you try it with NO quotes?!
    PHP Code:
    $ID_workshop $_GET['link'];

    $form_vars = array('ID_num','day','workshop','comment',$ID_workshop); 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Zealot charles_i's Avatar
    Join Date
    Apr 2007
    Location
    Toronto, Canada
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just tried it. Doesn't work. It thinks the value in $ID_workshop - which is 'link1' for testing - is the column name. So it says:

    Error in query: . Unknown column 'link1' in 'field list'

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Can you show us the actual full code for the insert?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Zealot charles_i's Avatar
    Join Date
    Apr 2007
    Location
    Toronto, Canada
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, some more detail. I'm working on a seminar feedback website. There are 2 pages involved here - one with a list of seminars and times - the 2nd one is an evaluation form page. The evaluation form goes back to the seminar list so people can evaluate as many seminars as they want.

    I am coding this so that each time the same person returns to the seminar list, the link that they just clicked on will be disabled. That part works - but I need to have an array of accumulating links already clicked so that they can all be turned off each time. So - my idea was to have a field in the database beside each evaluation with 'link1' etc. Then, when the seminar page is hit, I can get an array of links to turn off by reading that column.

    So, I'm passing the link that was last clicked in the URL to the evaluation page and back to the seminar links page - at that point if I can insert it into the database I can then get all the links clicked in that column.

    Hope this makes sense.

    Charles

  6. #6
    SitePoint Zealot charles_i's Avatar
    Join Date
    Apr 2007
    Location
    Toronto, Canada
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the code (rate and comment are from the form):

    Code:
    $ID_num = $_GET['ID_num'];
    $day = $_GET['day'];
    $workshop = $_GET['seminar'];
    $ID_seminar= $_GET['link'];
    
    $form_vars = array('ID_num','day','seminar','rate','comment','ID_seminar');
    As I said, they get passed through the link using:

    Code:
    evaluate.php?ID_num=$ID_num&day=Saturday&seminar=whatever&link=link1

  7. #7
    SitePoint Addict CVPer's Avatar
    Join Date
    Sep 2007
    Location
    Vancouver, BC, Canada
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what is $form_vars used for?
    not it's just an array with six strings, 'ID_num','day','seminar',...

    if you want it to store the values passed, you should write like this:
    PHP Code:
    $form_vars = array($ID_num$day$workshop$ID_seminar); 
    in addition, to make sure you get correct values, you may use print_r($form_vars) to list out all the elements of $form_vars.

    this will give you a better output of the array elements:
    PHP Code:
    echo '<pre>'print_r($form_vars); echo '</pre>'
    print_r($_GET) also can help you double check what your script receives from the url request.
    * @location Vancouver, BC, Canada
    * @name Steve
    * @job PHP/MySQL, Drupal, WordPress Developer

  8. #8
    SitePoint Zealot charles_i's Avatar
    Join Date
    Apr 2007
    Location
    Toronto, Canada
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, $form_vars is being used to insert into the db. That's why I can't use the $ID_workshop directly - I need whatever value it's holding so I can insert 'ID_workshop' Here's what happens after

    $form_vars = array('ID_num','day','workshop','comment','ID_workshop');

    Code:
    $query = "INSERT INTO evaluate SET ";
    		for ($i = 0; $i < count($form_vars); $i++) {
    		$query .=   $form_vars[$i].'="'.AddSlashes($_REQUEST[$form_vars[$i]]).'", ';
    		}
    	$query .= 'dateTime="'.date('y/m/d g:i a').'"';
    	$result = mysql_query($query) or die("Error in query: $sql. ".mysql_error());
    Thanks for the print_r($form_vars); tip though.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •