2011-01-01 04:08:00 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
$MYSQLCONN = new mysqli(
|
2011-01-01 04:08:00 +00:00
|
|
|
"localhost",
|
|
|
|
"learningphp",
|
|
|
|
"learningphp",
|
|
|
|
"learningphp"
|
|
|
|
);
|
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
if (mysqli_connect_errno()) {
|
|
|
|
printf("Could not connect to database: %s\n", mysqli_connect_error());
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$ACTION = "";
|
|
|
|
|
|
|
|
if (array_key_exists("action", $_POST)) {
|
|
|
|
$ACTION = $_POST["action"];
|
|
|
|
}
|
|
|
|
|
2011-01-01 04:08:00 +00:00
|
|
|
|
|
|
|
require_once("_head.php");
|
|
|
|
|
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
function show_clear_link() {
|
|
|
|
?>
|
|
|
|
<a href="<?php echo $_SERVER['PHP_SELF']; ?>">clear</a>
|
|
|
|
<?php
|
|
|
|
}
|
2011-01-01 04:08:00 +00:00
|
|
|
|
|
|
|
mkheader("mysql!", '#aff');
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if ($ACTION == "create-tables") {
|
|
|
|
?><p>Creating tables...</p><pre><?php
|
2011-01-01 04:08:00 +00:00
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
$MYSQLCONN->query("DROP TABLE IF EXISTS customers");
|
|
|
|
$create_result = $MYSQLCONN->query("
|
|
|
|
CREATE TABLE customers (
|
|
|
|
customer_id MEDIUMINT NOT NULL AUTO_INCREMENT,
|
|
|
|
name VARCHAR(255) NOT NULL,
|
|
|
|
email VARCHAR(255) NOT NULL,
|
|
|
|
PRIMARY KEY(customer_id)
|
|
|
|
) ENGINE=InnoDB
|
|
|
|
");
|
2011-01-01 04:08:00 +00:00
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
if (!$create_result) {
|
|
|
|
printf("Failed to create tables: %s\n", $MYSQLCONN->error);
|
|
|
|
} else {
|
|
|
|
printf("%d Row inserted.\n", $MYSQLCONN->affected_rows);
|
|
|
|
print_r($MYSQLCONN->query("DESCRIBE customers"));
|
|
|
|
print_r($MYSQLCONN->query("SELECT * FROM customers"));
|
|
|
|
}
|
2011-01-01 04:08:00 +00:00
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
?></pre><?php
|
2011-01-01 04:08:00 +00:00
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
show_clear_link();
|
|
|
|
|
|
|
|
} elseif ($ACTION == "show-conn") {
|
2011-01-01 04:08:00 +00:00
|
|
|
|
|
|
|
?>
|
2011-01-01 13:50:02 +00:00
|
|
|
<!-- ======================================================= -->
|
2011-01-01 04:08:00 +00:00
|
|
|
<pre><?php
|
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
print "\$MYSQLCONN:\n";
|
|
|
|
print_r($MYSQLCONN);
|
|
|
|
|
|
|
|
?></pre><?php
|
|
|
|
|
|
|
|
} elseif ($ACTION == "add-customer") {
|
|
|
|
|
|
|
|
$add_customer = $MYSQLCONN->prepare(
|
|
|
|
"INSERT INTO customers (customer_id, name, email) VALUES (NULL, ?, ?)"
|
|
|
|
);
|
|
|
|
$add_customer->bind_param('ss', $_POST["name"], $_POST["email"]);
|
|
|
|
$result = $add_customer->execute();
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
?><p>Failed to create customer!: <?php echo $MYSQLCONN->error ?></p><?php
|
|
|
|
} else {
|
|
|
|
?><p><?php echo $MYSQLCONN->affected_rows; ?> Row inserted.</p><?php
|
|
|
|
}
|
|
|
|
|
|
|
|
show_clear_link();
|
|
|
|
|
2011-01-01 04:08:00 +00:00
|
|
|
} else {
|
|
|
|
?>
|
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
<form name="database_admin" action="" method="POST">
|
|
|
|
<fieldset>
|
|
|
|
<legend>Database Admin</legend>
|
|
|
|
|
|
|
|
<span class="block">
|
|
|
|
<input type="radio" id="action-create-tables" name="action" value="create-tables" />
|
|
|
|
<label for="action-create-tables">create tables</label>
|
|
|
|
</span>
|
|
|
|
|
|
|
|
<span class="block">
|
|
|
|
<input type="radio" id="action-show-conn" name="action" value="show-conn" />
|
|
|
|
<label for="action-show-conn">show connection info</label>
|
|
|
|
</span>
|
|
|
|
|
|
|
|
<input type="submit" name="submit" value="Do Stuff" />
|
|
|
|
|
|
|
|
</fieldset>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<form name="add_customer" action="" method="POST">
|
|
|
|
<fieldset><legend>Add Customer</legend>
|
|
|
|
<input type="hidden" name="action" value="add-customer" />
|
|
|
|
|
|
|
|
<label class="block" for="customer-name">Name:</label>
|
|
|
|
<input type="text" id="customer-name" name="name" />
|
|
|
|
|
|
|
|
<label class="block" for="customer-email">Email:</label>
|
|
|
|
<input type="email" name="email" />
|
|
|
|
|
|
|
|
<input class="block" type="submit" name="submit" value="Add Customer" />
|
|
|
|
|
|
|
|
</fieldset>
|
2011-01-01 04:08:00 +00:00
|
|
|
</form>
|
|
|
|
|
2011-01-01 13:50:02 +00:00
|
|
|
|
|
|
|
<pre id="customer-listing"><?php
|
|
|
|
$results = $MYSQLCONN->query("SELECT * FROM customers");
|
|
|
|
while ($row = $results->fetch_array(MYSQLI_ASSOC)) {
|
|
|
|
print_r($row);
|
|
|
|
}
|
|
|
|
?></pre>
|
|
|
|
|
2011-01-01 04:08:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|
2011-01-01 13:50:02 +00:00
|
|
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$MYSQLCONN->close();
|
|
|
|
|
|
|
|
?>
|