SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Insert Id Number Into Database

    Hi,

    I can echo the id number of a member. "The ID Number is 239." I am now trying to enter number of the user as the organiserid.

    I can insert eventname into the table but I am struggling to add the id number. Can anyone advise how I should be doing this please.

    Code:
          <div class="boardintro">
    The ID Number is <?php echo (!empty($row['id'])) ? $row['id'] : ''; ?>
    </div>
    
      <?php
      if(isset($_POST['createevent'])){
                    $organiserid = trim($_POST['$id']); 
            $eventname = mysql_real_escape_string(trim($_POST['eventname']));
     
    {
                    $query = mysql_query("INSERT INTO table (eventname, organiserid) VALUES ('" .$eventname."', '" .$organiserid."')");
                    if($query) {
                    } else {
      $error = "There was a problem with the submission. Please try again.";
                    }
    }
    }
    ?>

  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)
    $_POST['$id']
    should be

    $_POST['id']
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks I tried that but it didn't work. Do I need allocate the ID number to a variable?

  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)
    I am assuming that the id is in a field in the form that you have for creating an event?
    It can be a hidden field as long as it contains the ID value, show us the form that you have got set up.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $query = mysql_query("INSERT INTO table (eventname, organiserid) VALUES ('" .$eventname."', '" .$organiserid."')");
    table is probably a mysql reserved word - either quote it with `backticks`, or better, give your table a better name such as events.

    Also, check the value of mysql_error()

    You are also missing some other simple techniques that will help you solve these kind of common "it doesnt work" errors:
    • if organiserid is a number then typecast is as such, and there is no need to then quote it
    • PHP variables which are not arrays will expand correctly when wrapped in double quotes "".
    • Create a variable holding the sql statement so that you can echo it onto the screen, inspect what PHP is doing for you, and even then copy that directly into your database management tool to check it is valid


    Example: (if indeed id is an integer)
    PHP Code:
    // typecast id to an integer
    $organiserid = (int)$_POST['id'];

    // then build your query string
    $newevent_sql "INSERT INTO table (eventname, organiserid) VALUES ('$eventname', $organiserid)" ;

    // a line of debug so you can check that worked as expected
    echo $newevent_sql;

    // now send in your query
    $query mysql_query($newevent_sql); 

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    (Table's still a reserved word in your example there Cups )
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •