Advertisement
Guest User

Insert and retrieve data from databasse

a guest
Apr 5th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.18 KB | None | 0 0
  1.  
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>Document</title>
  7. </head>
  8. <body>
  9.    
  10.    
  11.  
  12.      <h2>Insert your Bangla data</h2>
  13.          <form method="post" action="indexx.php">
  14.  
  15.              <p>Your Name:</p> <input type="text" name="fullname" placholder="Your name"> <br>
  16.              
  17.              <textarea cols="20" rows="8" name="textBox" placeholder="Write down your query please"></textarea>
  18.              <br>
  19.              <input type="submit" name="sendData" value="submit">
  20.          </form>
  21.    
  22. </body>
  23. </html>
  24.  
  25. here is codes of indexx.php
  26.  
  27. <?php
  28.  
  29.         include('database.php');
  30.  
  31.             $database = new DB;
  32.             $database->set_db("localhost", "ourcms", "root", "");
  33.             $conn = $database->connect();
  34.        
  35.        
  36.         if (isset($_POST['sendData']))
  37.         {
  38.            
  39.  
  40.             $name = $_POST['fullname'];
  41.             $texts = $_POST['textBox'];
  42.             $stmt = $conn->prepare('INSERT INTO tests(fname, bodytext) VALUES(:allname, :textbody)');
  43.             $stmt->execute(array(':allname' => $name, ':textbody' => $texts));
  44.         }
  45.         $results = $conn->query('SELECT * FROM tests')->fetchAll(PDO::FETCH_ASSOC);        
  46.      ?>
  47.  
  48. <?php if(isset($_POST['sendData'])) : ?>
  49.             <?php foreach( $results as $result) : ?>
  50.             <li> <?php echo $result['fname']; ?></li> <br>
  51.             <li> <?php echo $result['bodytext']; ?> </li>
  52.  
  53.         <?php endforeach; endif; ?>
  54.  
  55.  
  56. // Codes below for database.php
  57.  
  58. <?php
  59.    
  60.     class DB
  61.     {
  62.         private $dbHost;
  63.         private $dbName;
  64.         private $dbUser;
  65.         private $dbPass;
  66.  
  67.         protected $con;
  68.  
  69.         function set_db($host, $db, $user, $pass)
  70.         {
  71.             $this->dbHost = $host;
  72.             $this->dbName = $db;
  73.             $this->dbUser = $user;
  74.             $this->dbPass = $pass;
  75.  
  76.         }
  77.  
  78.         function connect()
  79.         {
  80.             $info = 'mysql:host='.$this->dbHost.';dbname='.$this->dbName;
  81.             try
  82.             {
  83.                 $this->con = new PDO($info, $this->dbUser, $this->dbPass);
  84.             }
  85.             catch(PDOException $e)
  86.             {
  87.                 print "Error Founds: ".$e->getMessage();
  88.                 die();
  89.             }
  90.  
  91.             return $this->con;
  92.         }
  93.     }
  94. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement