VikTHunder

Insesert Using funct HARD

Feb 28th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1.  
  2. Insert data using function
  3.  
  4.  
  5.  
  6. First , We are going to create a class for database connection. This class has two main methods , one is connect which contains connectivity code with PHP and MYSQL.
  7.  
  8. Another method is saveRecord() . It has one mandatory argument that is first argument(database table name) and another arguments are data inputted by users using html form.
  9.  
  10.  
  11.  
  12. Example :
  13.  
  14. 1. Database Connection Class:
  15.  
  16.  
  17.  
  18. <?php
  19.  
  20. class Database
  21.  
  22. {
  23.  
  24.  
  25.  
  26. public $conn;
  27.  
  28. var $dbhost = 'localhost';
  29.  
  30. var $dbuser = 'root';
  31.  
  32. var $dbpass = '';
  33.  
  34. var $dbase = 'usermanagementsystem';
  35.  
  36.  
  37.  
  38. public function __construct() {
  39.  
  40. $this->conn = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbase);
  41.  
  42. if (!$this->conn) {
  43.  
  44. die('failed' . mysqli_error());
  45.  
  46. } else {
  47.  
  48. // echo 'done';
  49.  
  50. }
  51.  
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. public function insert($table_name, $data) //member function
  59.  
  60. {
  61.  
  62. $string = "INSERT INTO ".$table_name." (";
  63.  
  64. $string .= implode(",", array_keys($data)) . ') VALUES (';
  65.  
  66. $string .= "'" . implode("','", array_values($data)) . "')";
  67.  
  68. if(mysqli_query($this->conn, $string))
  69.  
  70. {
  71.  
  72. echo "inserted";
  73.  
  74. }
  75.  
  76. else
  77.  
  78. {
  79.  
  80. echo mysqli_error($this->con);
  81.  
  82. }
  83.  
  84. } }
  85.  
  86. ?>
  87.  
  88.  
  89.  
  90. 2. HTML Form
  91.  
  92. <?php
  93.  
  94.  
  95.  
  96. include 'database.php';
  97.  
  98. $data = new Database;
  99.  
  100.  
  101.  
  102. if(isset($_POST["submit"]))
  103.  
  104. {
  105.  
  106. $insert_data = array(
  107.  
  108. 'fname' => mysqli_real_escape_string($data->conn, $_POST['fname']),
  109.  
  110. 'age' => mysqli_real_escape_string($data->conn, $_POST['age'])
  111.  
  112. );
  113.  
  114. if($data->insert('demo', $insert_data))
  115.  
  116. {
  117.  
  118. //echo "inserted";
  119.  
  120. }
  121.  
  122. }
  123.  
  124.  
  125.  
  126. ?>
  127.  
  128. <html>
  129.  
  130. <head>
  131.  
  132. <title>Registration Form</title>
  133.  
  134. <link rel="stylesheet" href="css/style.css" type="text/css">
  135.  
  136. </head>
  137.  
  138.  
  139.  
  140. <body>
  141.  
  142. <center>
  143.  
  144. <form action="" method="POST">
  145.  
  146. <input type="text" name="fname" placeholder="First name"/><br>
  147.  
  148. <input type="number" name="age" placeholder="Enter age"/><br>
  149.  
  150.  
  151.  
  152. <input type="submit" value="submit" name="submit" class="btn"/>
  153.  
  154. </form>
  155.  
  156. </center>
  157.  
  158. </body>
  159.  
  160. </html>
  161.  
  162. So, this way we can insert data using function.
Add Comment
Please, Sign In to add comment