-
Notifications
You must be signed in to change notification settings - Fork 0
/
creat-table.php
155 lines (138 loc) · 6.18 KB
/
creat-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
include("css/css/css.php");
include("include/db.php");
include("include/session.php");
include("include/sidebar.php");
include("include/functions.php");
?>
<div class="container-fluid">
<div class="row">
<div class="col-sm-10 title">
<h1><i class="fa fa-bars"></i> DASHBOARD</h1>
</div>
<div class="col-sm-12">
<?php
echo failed_alert();
echo success_alert();
?>
<div class="content">
<!-- ... rest of your HTML code ... -->
<table class="table">
<form action="creat-table.php" method="post">
<span>Add <input type="number" value="1" min="1" name="num_columns"> Column(s) <button type="submit" name="go">Go</button></span>
</form>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Type</th>
<th scope="col">Length/Values</th>
<th scope="col">Default </th>
<th scope="col">Collation</th>
<th scope="col">Attributes</th>
<th scope="col">A_I</th>
</tr>
</thead>
<form action="creat-table.php" method="post">
<tbody>
<?php
$numColumns = isset($_POST['go']) ? intval($_POST['num_columns']) : 4;
for ($i = 0; $i < $numColumns; $i++) {
echo '<tr>';
echo '<th style="width: 15px;" scope="row"><input name="column_name[]" type="text"></th>';
echo '<td>
<select name="column_type[]">
<option value="INT">INT</option>
<option value="VARCHAR">VARCHAR</option>
<option value="TEXT">TEXT</option>
<option value="DATE">DATE</option>
</select>
</td>';
echo '<td><input name="column_length[]" type="text"></td>';
echo '<td>
<select name="column_default_type[]">
<option value="DEFAULT NULL">None</option>
<option value="NULL">NULL</option>
<option value="DEFAULT">As defined:</option>
<option value="CURRENT_TIMESTAMP">CURRENT_TIMESTAMP</option>
</select>
</td>';
echo '<td>
<select name="collation[]">
<option value=""></option>
<option value="utf8_general_ci">utf8_general_ci</option>
<option value="utf8_unicode_ci">utf8_unicode_ci</option>
<!-- Add more valid collation options here -->
</select>
</td>';
echo '<td>
<select name="column_attributes[]">
<option value=""></option>
<option value="BINARY">BINARY</option>
<option value="UNSIGNED">UNSIGNED</option>
<option value="UNSIGNED ZEROFILL">UNSIGNED ZEROFILL</option>
<option value="ON UPDATE CURRENT_TIMESTAMP">ON UPDATE CURRENT_TIMESTAMP</option>
<option value="COMPRESSED=Zlib">COMPRESSED=Zlib</option>
</select>
</td>';
echo '<td>';
echo '<input value="AUTO_INCREMENT" name="column_auto_increment[]" type="checkbox">';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
<button type="submit" name="submit">Creat</button>
</form>
</table>
<!-- ... rest of your HTML and form code ... -->
<?php
if (isset($_POST['submit'])) {
// Collect user input for column details
$column_names = $_POST['column_name'];
$column_types = $_POST['column_type'];
$column_lengths = $_POST['column_length'];
$column_default_types = $_POST['column_default_type']; // Collect default value types
$column_collations = $_POST['collation'];
$column_attributes = $_POST['column_attributes'];
$column_auto_increment = isset($_POST['column_auto_increment']) ? $_POST['column_auto_increment'] : array();
// Collect the table name
$column_definitions = array();
for ($i = 0; $i < count($column_names); $i++) {
$column_definition = '`' . $column_names[$i] . '` ';
if ($column_auto_increment[$i] === 'AUTO_INCREMENT') {
// For AUTO_INCREMENT columns, use INT or BIGINT and make it primary key
$column_definition .= 'INT PRIMARY KEY AUTO_INCREMENT';
} else {
// Handle other column types and attributes
$column_definition .= $column_types[$i];
if ($column_types[$i] === 'VARCHAR') {
// Make sure to provide a valid length for VARCHAR
$column_definition .= '(' . intval($column_lengths[$i]) . ')';
}
// Handle nullable attribute
if (isset($column_default_types[$i]) && $column_default_types[$i] === 'on') {
$column_definition .= ' NULL';
} else {
$column_definition .= ' NOT NULL';
}
}
$column_definitions[] = $column_definition;
}
// Construct the CREATE TABLE query
$columns_string = implode(', ', $column_definitions);
$create_table_query = "CREATE TABLE `table_name` ($columns_string)";
// Execute the CREATE TABLE query
try {
$connect->exec($create_table_query);
echo "Table created successfully!";
} catch (PDOException $e) {
echo "Error creating table: " . $e->getMessage();
}
}
?>
</div>
<?php include("include/footer.php"); ?>
</div>
</div>
</div>
</div>