user1114080
user1114080

Reputation: 65

Inserting Different Data Into Two Tables

I am trying to insert some data into one table and after that is successful I want one field from my other table to get updated. my code is as follows:

 <?php

$room = $_REQUEST['room'];
$date = $_REQUEST['date'];
$time = $_REQUEST['time'];
$id = $_GET['meeting_id'];

$con = mysql_connect("******","****","****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db('mdb_hj942', $con);

$sql="INSERT INTO Rooms (room, date, time, meeting_id) VALUES ('$room','$date', '$time','$id')";
$sql2="UPDATE Meetings SET action = 'success'";

if (!mysql_query($sql,$con,$sql2))
  {
        die('Error: ' . mysql_error());
  }
  else 
  {
      echo '<h2>Room & Time Has Now Been Updated</h2>';

  }
?>

is this possbile? if so, what changes do i make as i am getting the following error message:

'Warning: Wrong parameter count for mysql_query() in E:\webareas\hj942\conference\Secretary\bookedsuccessfully.php on line 80
Error: '

Upvotes: 0

Views: 340

Answers (4)

Mouna Cheikhna
Mouna Cheikhna

Reputation: 39638

you can't give mysql_query two queries at once :

resource mysql_query ( string $query [, resource $link_identifier ] )

mysql_query() sends a unique query (multiple queries are not supported) to the
currently active database on the server that's associated 
with the specified link_identifier  

(see here)

try this :

 $sql="INSERT INTO Rooms (room, date, time, meeting_id) VALUES ('$room','$date', '$time','$id')";
        $sql2="UPDATE Meetings SET action = 'success'";
        if (mysql_query($sql,$con)) // will return true if succefull else it will return false
          {
                if(mysql_query($sql2, $con)){ //$sql2 succeeded
                     echo '<h2>Room & Time Has Now Been Updated</h2>';
                }
          }

Upvotes: 0

liquorvicar
liquorvicar

Reputation: 6096

You need to execute each query separately. Something like this

$sql="INSERT INTO Rooms (room, date, time, meeting_id) VALUES ('$room','$date', '$time','$id')";
if (!mysql_query($sql,$con))
{
    die('Error: ' . mysql_error());
}
$sql2="UPDATE Meetings SET action = 'success'";

if (!mysql_query($sql2,$con))
{
    die('Error: ' . mysql_error());
}

But please before you go any further, go and read about SQL injection (search SO for "SQL injection"). Your code as it stands is very vulnerable to an SQL-injection attack. You might also want to look into PHP's PDO functions to make life easier.

Upvotes: 0

Andrew HunzekerHesed
Andrew HunzekerHesed

Reputation: 324

mysql_query does not support multiple queries. You will have to break your db access into two separate calls.

From php.net:

resource mysql_query ( string $query [, resource $link_identifier ] )

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

Upvotes: 0

clyfe
clyfe

Reputation: 23770

if (!mysql_query($sql, $con)){
  die('Error: ' . mysql_error());
} else {
  if (!mysql_query($sql2, $con)){
    die('Error: ' . mysql_error());
  } else {
    echo '<h2>Room & Time Has Now Been Updated</h2>';
  }
}

Upvotes: 1

Related Questions