Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(isset($_POST["submit"])){
- //$hostname='localhost';
- //$username='root';
- //$password='';
- try {
- $dbh = new PDO("mysql:host=$hostname;dbname=college",$username,$password);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "INSERT INTO students (student_name, student_email, student_city)
- VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
- if ($conn->query($sql)){
- echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
- }
- else{
- echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
- }
- $dbh = null;
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- }
- }
- require_once('../config/database.php'); //file with connection to db
- class Category {
- ...
- public function create() {
- ???
- }
- class Database {
- public function getConnection() {
- $servername = "localhost";
- $username = "root";
- $password = "";
- try {
- $conn = new PDO("mysql:host=$servername;dbname=demo", $username, $password);
- // set the PDO error mode to exception
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- echo "Connected successfully with the database!<br/><br/>";
- return $conn;
- } catch (PDOException $e) {
- echo "Connection failed with the database!<br/><br/> " . $e->getMessage();
- }
- }
- }
- $db = new Database();
- $conn = $db->getConnection();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement