0
<?php
$db = new mysqli("localhost", "HIDDEN", "HIDDEN", "HIDDEN");
if ($db->connect_error) {
    die("Failed to connect.");
}
if (isset($_POST["title"]) && isset($_POST["description"]) && isset($_POST["url"])) {
    $title = $db->real_escape_string($_POST["title"]);
    $description = $db->real_escape_string($_POST["description"]);
    $url = $db->real_escape_string($_POST["url"]);
    $sql = "INSERT INTO video (name, description, submission_date)
    VALUES ('{$title}', '{$description}', CURDATE());
    INSERT INTO video_source (video_id, url)
    VALUES (LAST_INSERT_ID(), '{$url}');";
    if ($db->query($sql) === TRUE) {
        echo "Successfully added.";
    } else {
        echo "Query failed.<br><br>Data: {$title} {$description} {$url}";
    }
} else {
    echo "Data not set.";
}
$db->close();?>

Outputs "Query failed." with the data I entered. Replacing variables such as title with constants still has the same problem. I tried the query in PHPMyAdmin and it worked fine (with constants).

It seems to be unhappy with setting the value of video_id.

4
  • Your missing " after localhost Commented Jul 16, 2015 at 18:04
  • When localhost is properly enclosed in quotes the code still outputs the same error. Commented Jul 16, 2015 at 18:04
  • You're attempting to run two queries, which one fails? Commented Jul 16, 2015 at 18:11
  • Second one, when I remove video_id from it, it works. Commented Jul 16, 2015 at 18:12

1 Answer 1

2

Anytime you're running multiple queries with MySQLi you should use multi_query():

$db->multi_query($sql)

In addition, LAST_INSERT_ID() in your second query is not returning any sort of value. If you're looking for the last inserted value of the 1st query you have to return that prior to running the second query.

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

1 Comment

That worked, thank you :) Oddly enough when I just used the second query with only constants it still didn't work :/

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.