0

I have the following code which inserts 1 row into the database:

public function fixed($fieldDay, $fieldNight) {

    $pdo = new SQL();
    $dbh = $pdo->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass);

    $this->sql = "INSERT INTO tblfixedfare (SELECT NULL, MAX(FixedFareID)+1, '1', '$fieldDay' FROM tblfixedfare)";

try {
        // Query
        $stmt = $dbh->prepare($this->sql);

        $stmt->execute();

        $count = $stmt->rowCount();

        echo $count.' row(s) inserted by SQL: '.$stmt->queryString;

        $stmt->closeCursor();

    }

    catch (PDOException $pe) {
        echo 'Error: ' .$pe->getMessage(). 'SQL: '.$stmt->queryString;
        die();
    }

    // Close connection
    $dbh = null;
}

}

At the same time this is being executed, I want to insert another row into the same table:

$this->sql = "INSERT INTO tblfixedfare (SELECT NULL, MAX(FixedFareID)+1, '2', '$fieldNight' FROM tblfixedfare)";

How would I go about doing this?

EDIT

Also, for both inserts the value for MAX(FixedFareID)+1 needs to be the same value.

3
  • why don't you just use Auto_increment? Commented Oct 12, 2012 at 12:37
  • Just UNION the two SELECT statements. Commented Oct 12, 2012 at 12:37
  • You're using prepared statements, but you're not binding any parameters, at the same time you're passing raw variables to the queries? Kinda pointless, isn't it? Commented Oct 12, 2012 at 12:41

1 Answer 1

3

Change the SQL query definition to this:

$this->sql = "
    INSERT INTO tblfixedfare 
        SELECT NULL, MAX(FixedFareID) + 1, '1', :fieldDay
        FROM tblfixedfare
        UNION
        SELECT NULL, MAX(FixedFareID) + 1, '2', :fieldNight
        FROM tblfixedfare
";

...and change the execute() call to this:

$stmt->execute(array(
    'fieldDay' => $fieldDay,
    'fieldNight' => $fieldNight
));
Sign up to request clarification or add additional context in comments.

10 Comments

Say for example MAX(FixedFareID)+1 = 5, would that use value 5 for both inserts?
@nsilva Yes. What else would you expect?
I've just tried this and I get the following: Syntax error or access violation: 1064 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 'SELECT NULL, MAX(FixedFareID)+1, '1', '100' FROM tblfixedfare), ' at line 2SQL: INSERT INTO tblfixedfare VALUES (SELECT NULL, MAX(FixedFareID)+1, '1', '100' FROM tblfixedfare), (SELECT NULL, MAX(FixedFareID)+1, '2', '200' FROM tblfixedfare)
@nsilva Oh right yes actually you can't do this, duh. Try INSERT INTO tblfixedfare (SELECT NULL, MAX(FixedFareID)+1, '1', '$fieldDay' FROM tblfixedfare UNION SELECT NULL, MAX(FixedFareID)+1, '2', '$fieldNight' FROM tblfixedfare) - although also note that N.B.'s comment on the question is valid, there is no point in preparing the statement if you just drop raw variables into the query string - use placeholders instead.
@nsilva Yeh that was wrong as well. I'm not doing well here am I? Try it without the parens: INSERT INTO tblfixedfare SELECT NULL, MAX(FixedFareID)+1, '1', '$fieldDay' FROM tblfixedfare UNION SELECT NULL, MAX(FixedFareID)+1, '2', '$fieldNight' FROM tblfixedfare - that really should work, I've done it many time before.
|

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.