-4
if($count <= 0 ) // IF TABLE DOES NOT EXIST -> CREATE AND INSERT DATA
{
    $CREATE_TABLE= "CREATE TABLE $TABLE_NAME LIKE student; INSERT $TABLE_NAME SELECT * FROM student;";
    $created = $connect->exec($CREATE_TABLE);

    if($created!=FALSE)
    {
        $SQL = "INSERT INTO $TABLE_NAME (name, roll_number, father_name, dob, gender, address, email, phone, department, program, semester, section) VALUES(:name, :roll_number, :father_name, :dob, :gender, :address, :email, :phone, :department, :program, :semester, :section)";
        $pdo_statement = $connect->prepare($SQL);

        $pdo_statement->bindparam(':name',          $name);
        $pdo_statement->bindparam(':roll_number',   $roll_number);
        $pdo_statement->bindparam(':father_name',   $father_name);
        $pdo_statement->bindparam(':dob',           $dob);
        $pdo_statement->bindparam(':gender',        $gender);
        $pdo_statement->bindparam(':address',       $address);
        $pdo_statement->bindparam(':email',         $email);
        $pdo_statement->bindparam(':phone',         $phone);
        $pdo_statement->bindparam(':department',    $department);
        $pdo_statement->bindparam(':program',       $program);
        $pdo_statement->bindparam(':semester',      $semester);
        $pdo_statement->bindparam(':section',       $section);

        $result = $pdo_statement->execute();
    }
}
else if($count > 0) // IF TABLE EXIST -> INSERT DATA
{
    $SQL = "INSERT INTO $TABLE_NAME (name, roll_number, father_name, dob, gender, address, email, phone, department, program, semester, section) VALUES (:name, :roll_number, :father_name, :dob, :gender, :address, :email, :phone, :department, :program, :semester, :section)";
    $pdo_statement = $connect->prepare($SQL);

    $pdo_statement->bindparam(':name',          $name);
    $pdo_statement->bindparam(':roll_number',   $roll_number);
    $pdo_statement->bindparam(':father_name',   $father_name);
    $pdo_statement->bindparam(':dob',           $dob);
    $pdo_statement->bindparam(':gender',        $gender);
    $pdo_statement->bindparam(':address',       $address);
    $pdo_statement->bindparam(':email',         $email);
    $pdo_statement->bindparam(':phone',         $phone);
    $pdo_statement->bindparam(':department',    $department);
    $pdo_statement->bindparam(':program',       $program);
    $pdo_statement->bindparam(':semester',      $semester);
    $pdo_statement->bindparam(':section',       $section);

    $result = $pdo_statement->execute();

} // ELSE IF ENDS
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Vishal Sharma
  • 51
  • 2
  • 10

2 Answers2

0

So i understand you will create the table, if it does not exist and insert data. So call first

$pdo->query("CREATE TABLE $TABLE IF NOT EXISTS;");

It will do nothing, when table exists.

And then insert your data.

$pdo->query("INSERT INTO $TABLE ... ");

No 'if then else' in PHP!

daddykom
  • 222
  • 2
  • 8
-1

function tableExists($pdo, $table) {

// Try a select statement against the table
// Run it in try/catch in case PDO is in ERRMODE_EXCEPTION.
try {
    $result = $pdo->query("SELECT 1 FROM $table LIMIT 1");
} catch (Exception $e) {
    // We got an exception == table not found
    return FALSE;
}

// Result is either boolean FALSE (no table found) or PDOStatement Object (table found)
return $result !== FALSE;

}

test xv
  • 11