2

I am trying to add data into 2 tables using PHP

My PHP code: insert.php

<?php
session_start();
$db['host'] = "dbhost";
$db['user'] = "user";
$db['pass'] = "pass";
$db['name'] = "dbname";

//making an array with the data recieved
$data = array('f_name' => $_POST['txt_f_name'],
              'l_name' => $_POST['txt_l_name'],
              'VNum' => $_POST['txtVisaNo']);

try {
    // preparing database handle $dbh
    $dbh = new PDO("mysql:host=".$db['host']."; dbname=".$db['name']."", $db['user'], $db['pass']);
    // set the PDO error mode to exception
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$insertall = "BEGIN; "
        . "INSERT INTO students (f_name, l_name) "
        . "VALUES (:f_name, :l_name); "
        . "INSERT INTO visa (students_id, VNum) "
        . "VALUES (:LAST_INSERT_ID(), :VNum); "
        . "$addStdInf->execute($data); "
        . "COMMIT;";
$addStdInf = $dbh->prepare($insertall);

echo 'Success!';
}
catch(PDOException $e){
    echo $sql,'<br />', $e->getMessage();

}
$dbh = null;
?>

Notice is "Success!" but it inserted nothing into database, please guide me the ERROR.Thank you.

3
  • What is the table structure? Commented May 7, 2015 at 11:04
  • 2
    You prepare the statement but never execute the query. php.net/manual/en/pdostatement.execute.php Commented May 7, 2015 at 11:08
  • $addStdInf->execute($data); is this? Commented May 7, 2015 at 11:15

2 Answers 2

4

You are only preparing the statement - you never execute it. After the prepare call you receive a ready to execute statement, if you execute it with some parameters, it will be inserted in the database: http://php.net/manual/en/pdostatement.execute.php

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

1 Comment

You also need to check the return value from execute() before declaring success - and be aware that there are some problems with issuing multiple SQL statements in a single API call - see also stackoverflow.com/questions/6346674/…
1

You are forget to execute your pdo statements

<?php
session_start();
$db['host'] = "dbhost";
$db['user'] = "user";
$db['pass'] = "pass";
$db['name'] = "dbname";

//making an array with the data recieved
$data = array('f_name' => $_POST['txt_f_name'],
              'l_name' => $_POST['txt_l_name'],
              'VNum' => $_POST['txtVisaNo']);

try {
    // preparing database handle $dbh
    $dbh = new PDO("mysql:host=".$db['host']."; dbname=".$db['name']."", $db['user'], $db['pass']);
    // set the PDO error mode to exception
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$insertall = "BEGIN; "
        . "INSERT INTO students (f_name, l_name) "
        . "VALUES (:f_name, :l_name); "
        . "INSERT INTO visa (students_id, VNum) "
        . "VALUES (:LAST_INSERT_ID(), :VNum); "
        . "$addStdInf->execute($data); "
        . "COMMIT;";
$addStdInf = $dbh->prepare($insertall);
$result = $addStdInf->execute();
if ($result) {
    echo 'Success!'; 
} else {
    echo 'please check';
}
}
catch(PDOException $e){
    echo $sql,'<br />', $e->getMessage();

}
$dbh = null;
?>

Comments

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.