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.

      <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.";
                }
}
}
?>

$_POST[‘$id’]
should be

$_POST[‘id’]

Thanks I tried that but it didn’t work. Do I need allocate the ID number to a variable?

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.

$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)


// 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);

(Table’s still a reserved word in your example there Cups :wink: )