mysql_query expected [closed]

Posted on

Question :

currently I’m getting the error stated above using thise code:

$Tijd = date_create($_POST ["Tijd"]);
$Tijd->format(DateTime::ISO8601);

$result = mysql_query ("SELECT Voorstellingsnummer FROM voorstelling v WHERE Tijd = '$Tijd' AND v.Voorstellingsnummer NOT IN(SELECT r.Voorstellingsnummer FROM reserveringen r WHERE r.Voorstellingsnummer = v.Voorstellingsnummer GROUP BY r.Voorstellingsnummer HAVING COUNT(*) >= 50) AND Vestigingsnaam = '" . $_SESSION["geselecteerdvestiging"] . "' AND Filmnaam = '" . $_SESSION["geselecteerdfilmnaam"] . "';", $db);

Object of class DateTime could not be converted to string

the error happens in the $result = etc…

Full error:
Catchable fatal error: Object of class DateTime could not be converted to string in E:xampplitehtdocshavo5groep2reserveren.php on line 60

I use this form to get the post data

                            echo "<form action='reserveren.php' method='post'>
                            <H3>Kies een Tijd</H3><br>
                            <select name='Tijd'> ";
                        while ($row = mysql_fetch_array($_SESSION["Tijd"])) {
                            echo "<option value='" . $row['Tijd'] ."'>" . $row['Tijd'] ."</option>";
                        }
                        echo "</select>
                            <input type='submit' name='Reserveer' value='Reserveren'>
                            </form>";

I get the values from $row from the following query:

        $resultaat = mysql_query ("SELECT Tijd FROM voorstelling v WHERE v.Voorstellingsnummer NOT IN(SELECT r.Voorstellingsnummer FROM reserveringen r WHERE r.Voorstellingsnummer = v.Voorstellingsnummer GROUP BY r.Voorstellingsnummer HAVING COUNT(*) >= 50) AND Vestigingsnaam = '" . $_SESSION["geselecteerdvestiging"] . "' AND Filmnaam = '" . $_SESSION["geselecteerdfilmnaam"] . "';", $db);
    $_SESSION["Tijd"] = $resultaat;

the value of Tijd in the select are all datetime records.

So my question is how do I succesfully get the value form the select form to convert into a timedate that I can use in my query

Answer :

As has been intimated in the comments quite possibly is not a database isue, in fact the error you are receiving is generated from PHP.

The error is caused by a slight misunderstanding of what datetime->format() does.

This does not change the format the date and time is stored in rather it returns the date and time in that format immediately (datetime->format() documentation)

To correct this in your code you need to capture the result of “$Tijd->format(DateTime::ISO8601);” in another variable such as:

$Tijd = date_create($_POST ["Tijd"]);
$formattedTime = $Tijd->format(DateTime::ISO8601);

You can then use this in the SQL such:

$result = mysql_query (
      "SELECT Voorstellingsnummer
      FROM voorstelling v
      WHERE Tijd = '$formattedTime' AND
      v.Voorstellingsnummer NOT IN(
         SELECT r.Voorstellingsnummer
         FROM reserveringen r
         WHERE r.Voorstellingsnummer = v.Voorstellingsnummer
         GROUP BY r.Voorstellingsnummer
         HAVING COUNT(*) >= 50
      ) AND
      Vestigingsnaam = '" . $_SESSION["geselecteerdvestiging"] . "' AND
      Filmnaam = '" . $_SESSION["geselecteerdfilmnaam"] . "';",
   $db
);

Leave a Reply

Your email address will not be published. Required fields are marked *