1

I am trying to make a php calendar, as part of a project, and here I am executing some SQL with php:

mysql_select_db("waycov_dtm", $con);

$data="'Placeholder Text'";

$sql="INSERT INTO waycov_dtm.calDates (dateID, day, month, year)
VALUES (" . $dateID . ", " . $day . ", " . $month . ", " . $year . ");
INSERT INTO waycov_dtm.calData (dateID, data, memberID)
VALUES (" . $dateID . ", " . $data . ", 1);";

echo $sql;

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);

When this executes, I get the following (the echoed sql and then the error):

INSERT INTO waycov_dtm.calDates (dateID, day, month, year) VALUES (7072012, 7, 07, 2012); INSERT INTO waycov_dtm.calData (dateID, data, memberID) VALUES (7072012, 'Placeholder Text', 1);Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO waycov_dtm.calData (dateID, data, memberID) VALUES (7072012, 'Placeh' at line 3

If i take out any one insert statment, the other runs, but both together causes this??

0

1 Answer 1

4

Use it like this:

$sql="INSERT INTO waycov_dtm.calDates (dateID, day, month, year)
VALUES (" . $dateID . ", " . $day . ", " . $month . ", " . $year . ")";
$sql_second = "INSERT INTO waycov_dtm.calData (dateID, data, memberID)
VALUES (" . $dateID . ", " . $data . ", 1);";

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

From the manual:

mysql_query() sends a unique query (multiple queries are not supported)

The use of mysql_query is discouraged. Use MySQLi or PDO.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 -- any idea why it would claim the error happened halfway through the word "placeholder"? Is it due to buffering?
It seems actually that the error is just before the beginning of the second query (before the INSERT)
Ah yeah I see what you mean. The fact that it cut off the second query threw me off. 'Placeh' at line 3
Thanks! That solved it! Even though a missed dollar sign on sql_second confused me for a second

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.