tags:

views:

46

answers:

1

How do I insert two rows into two different tables. I can't seem to get this right?

Here is the code below.

// tag is not there, so insert a new instance
  $mysqli = new mysqli("localhost", "root", "", "sitename");
  $clean_url = mysqli_real_escape_string($mysqli, $page);
  $dbc = mysqli_query($mysqli,"INSERT INTO q_tags (tag_id, users_q_id) VALUES ('$tag_num', '$page')");
  $dbc .= mysqli_query($mysqli,"INSERT INTO tags (tag) VALUES ('$tag')");

  echo "$tag has been entered";

if (!$dbc) {
  // There was an error...do something about it here...
  print mysqli_error($mysqli);
}
A: 

The . operator is used to append strings. You're trying to append booleans (the results of mysqli_query) with this operator, which means it's not going to hold the value you expect when you check its value later. Try this instead:

// tag is not there, so insert a new instance
$mysqli = new mysqli("localhost", "root", "", "sitename");
$clean_url = mysqli_real_escape_string($mysqli, $page);

$query1 = "INSERT INTO q_tags (tag_id, users_q_id) VALUES ('$tag_num', '$page')";
$query2 = "INSERT INTO tags (tag) VALUES ('$tag')":

if (!mysqli_query($mysqli, $query1)) {
    print mysqli_error($mysqli);
    return;  // or exit if this isn't in a function call
}

if (!mysqli_query($mysqli, $query2)) {
    print mysqli_error($mysqli);
    return;  // or exit if this isn't in a function call
}

echo "$tag has been entered";
Matt Huggins