SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    posting multiple values to the database

    Hi guys

    Trying to post the values of the checkbox into the database
    I think im almost there but something is stopping it going into the database

    PHP Code:
    <form method="post" action="<?php print $_SELF?>">

    Avon:<input type="checkbox" value="avon" name="location[]">:<br />
    Bedfordshire:<input type="checkbox" value="Bedfordshire" name="location[]">:<br />
    Buckinghamshire:<input type="checkbox" value="Buckinghamshire" name="location[]">:<br />
    Cambridgeshire:<input type="checkbox" value="Cambridgeshire" name="location[]">:<br />
    Cheshire:<input type="checkbox" value="Cheshire" name="location[]">:<br /> 

    <input type="submit" name="submit_alert value="Submit Job" />

    PHP Code:
    <?PHP
    //connect to your database ** EDIT REQUIRED HERE **
    $connection mysql_connect("localhost","notexper_two","three"); //(host, username, password)

    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("notexper_one") or die("Unable to select database"); //select which database we're using



    if(isset($_POST['submit_alert']))

    {

    $location implode(',',$_POST['location']);



    $sql "INSERT INTO jobalerts (location, location1, location2, location3, location4, location5) VALUES ('".$location."')";



    mysql_query($sql);


    }

    ?>

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    if(isset($_POST['submit_alert'])) {
      $location = implode("','",array_map('mysql_real_escape_string',$_POST['location']));
     
      $sql = "INSERT INTO jobalerts (location, location1, location2, location3, location4, location5) VALUES ('".$location."')";
     
      mysql_query($sql) or die(mysql_error());
     
    }
    Saul

  3. #3
    SitePoint Evangelist superuser2's Avatar
    Join Date
    Aug 2006
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at the query you can easily spot the error; it'd tell you that you have an invalid parameter count.

    You say your going to specify multiple values: "(location, location1, location2, location3, location4, location5)" but then after VALUES you only supply one. You need to set them all in the order they were declared in the first half of your query. Does that make sense?

  4. #4
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i done that but no error appears

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That's still only going to work if there are 5 values, and an unchecked box is not posted, so there may be 0 to 5 values in $_POST...

    You should really have one row per location instead, with a foreign key referencing whatever these alerts are for.


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
  •