I setup a "phptest" to see if I could connect and retreive/update data using php.
Ive made a database called "phptest" and a table called "users".
I have 4 different codes/files:
header.php
footer.php
mysqlcore.php
users.php
It retreives the data from my table cause it loads up on the mysqlcore.php when i run it on my server.
mysqlcore.php:
<?php
include("header.php");
include("footer.php");
include("users.php");
switch($cmd)
{
case "doAddUser":
doAddUser();
exit();
}
showHeader();
switch($cmd)
{
case "addUser":
addUser();
break;
default:
showUsers();
break;
}
showFooter();
?>
users.php:
<?php
function showUsers()
{
?>
<center>
<br><b>Registered Users</b><br><br>
<a href="mysqlcore.php?cmd=addUser">Add User</a>
<br><br>
<table width="450" border="1" cellpadding="4">
<tr bgcolor="#BBBBBB">
<td width="25%"><b>Name</td>
<td width="25%"><b>Age</td>
<td width="25%"><b>Allias</td>
<td width="25%"><b>Password</td>
</tr>
<?php
$dbh = mysql_connect("localhost", "root", "");
mysql_select_db("phptest", $dbh);
$results = mysql_query("SELECT * FROM users ORDER BY name", $dbh);
while($row = mysql_fetch_array($results))
{
print "<tr>";
print "<td>".htmlspecialchars($row["name"])."</td>";
print "<td>".htmlspecialchars($row["age"])."</td>";
print "<td>".htmlspecialchars($row["alias"])."</td>";
print "<td>".htmlspecialchars($row["password"])."</td>";
print "</tr>";
}
mysql_close($dbh);
?>
</table>
</center>
<?php
}
function addUser()
{
?>
<center>
<br><b>Add User Form</b><br><br>
<form method="POST" action="mysqlcore.php">
<input type="hidden" name="cmd" value="doadduser">
<table width="450" border="1" cellpadding="4">
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="age" size=3></td>
</tr>
<tr>
<td>Allias:</td>
<td><input type="text" name="alias"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
</table>
<br><br>
<input type="submit" value="Add User">
<input type="button" value="Cancel" onClick="window.location='mysqlcore.php'">
</form>
<?php
}
function doAddUser()
{
$dbh = mysql_connect("localhost", "root", "");
mysql_select_db("phptest", $dbh);
mysql_query("INSERT INTO users VALUES (NULL, \"".$_POST["name"]."\",
\"".$_POST["age"]."\",\"".$_POST["alias"]."\",
\"".$_POST["password"]."\")",$dbh);
mysql_close($dbh);
header("Location: mysqlcore.php");
}
?>
The problem is when i click "Add User" the Add user form wont appear. The page stays the same. :bemused: