· 4 years ago · May 15, 2021, 06:56 PM
1<html>
2
3<body>
4 <form method="post">
5 <input type="text" name="Number">
6 <button name="submit">Submit</button>
7 </form>
8</body>
9
10</html>
11<?php
12$conn = mysqli_connect('localhost', 'root', '');
13
14$sql = "CREATE DATABASE IF NOT EXISTS mydb1";
15
16if ($conn->query($sql) === TRUE) {
17 echo "Database created successfully";
18}
19
20if (!$conn->select_db('mydb1')) {
21 die("Couldn't connect to db: " . $conn->connect_error);
22}
23
24
25$sql = "CREATE TABLE IF NOT EXISTS myprogram1 (
26 id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
27 number_value INT(11) NOT NULL
28)";
29
30if ($conn->query($sql) === TRUE) {
31 echo "Table myprogram1 created successfully";
32}
33
34if (isset($_POST['submit'])) {
35 $n = $_POST['Number'];
36}
37
38function insert($n, $conn)
39{
40 $sql = "INSERT INTO myprogram1 SET number_value= $n";
41 if ($conn->query($sql) !== TRUE) {
42 echo "Error inserting record: " . $conn->error;
43 }
44}
45
46insert(2, $conn);
47
48function selectData($conn)
49{
50 $sql = "SELECT * FROM MyProgram1";
51 $res = $conn->query($sql);
52 $out = array();
53
54 if ($res->num_rows > 0) {
55 while ($row = $res->fetch_assoc()) {
56 $out[] = $row;
57 }
58 }
59
60 return $out;
61}
62
63$numbers = selectData($conn);
64
65if (isset($numbers)) {
66?>
67 <h1>Table from mydb1<h1>
68 <table>
69 <thead>
70 <tr>
71 <th colspan="1">number</th>
72 <th colspan="2">value</th>
73 </tr>
74 </thead>
75 <tbody>
76 <?php
77 foreach ($numbers as $item) {
78 echo "<tr>";
79 echo "<td>" . $item['id'] . "</td>";
80 echo "<td>" . $item['number_value'] . "</td>";
81 echo "</tr>";
82 }
83 ?>
84 </tbody>
85 </table>
86
87 <?php } ?>