PHP5+Mysql5+Apache2 is currently the most popular website building model. When operating Mysql tables, it would be too cumbersome to only use SQL language to operate Mysql. So the author referred to the codes on the Internet and modified them to write a set of PHP codes for adding, deleting, and modifying records in various tables. For your reference and entertainment only.
The code posted below passed debugging on PHP5+Mysql5+Apache2 for WinXp.
First, let me explain the applicable scope of the code. According to the characteristics of most tables, each table has an id, so these codes are applicable to the table with the id in the first column. It doesn't matter what the name is. What's important is that the code operates according to the first column as the primary key by default. There are no other restrictions. This code can operate on tables with any column. Cons: No input review. Therefore, when entering records, do not leave blank lines or enter unmatched types. This needs to be coordinated with Mysql's table creation.
Note: Reminder! When copying the above code, be sure to remove the tab in front of each line of code in the editor, otherwise php5 will not compile.
And in each file, $database is currently sunsite and $table is software. This can be modified as needed.
The first is index.php. Its main function is to allow users to enter the table name, and then click the view button to see the contents of the table. It is also the default homepage.
//index.php
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>admin</title>
<body bgcolor=#ffffff>
<h2>Please input the table name</h2>
<form method="post" action="tables.php">
<table width=90% align=center>
<tr>table name: <input type=text name="tablename" size=30 maxlength=30><input type=submit value=show></tr>
</table>
</form>
</body>
</html>
Then there is tables.php, whose function is to display the contents of the table. There are also entry points for adding, deleting and modifying.
//tables.php
<html>
<title>current table</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<body bgcolor=#ffffff>
<?
$database = "sunsite"
$tablename = $_REQUEST['tablename'];
echo "<h2>Data from $tablename</h2>"
mysql_connect("localhost","root","") or die ("Problem connecting to DataBase");
$query = "show columns from $tablename"
$result = mysql_db_query($database,$query);
$column = 0;
if ($result)
{
echo "Found these entries in the database:<br><p></p>"
echo "<table width=90% align=center border=1><tr>"
while ($r = mysql_fetch_array($result))
{
echo "<td align=center bgcolor=#00FFFF>$r[0]</td>"
$column = $column + 1;
}
echo "</tr>"
mysql_free_result($result);
$query = "select * from $tablename"
$result = mysql_db_query($database, $query);
if ($result)
while ($r = mysql_fetch_array($result))
{
echo "<tr>"
for($col=0;$col<$column;$col++) echo "<td>$r[$col]</td>"
echo "</tr>"
}
echo "</table>"
}
else echo "No data."
mysql_free_result($result);
if ($column != 0)//($tablename == "software" || $tablename == "techtalk")
echo "<p></p>
<ul>
<li><a href="index.php">Home</a>
<li><a href="add.php?tablename=$tablename">Add a new entry</a>
<li><a href="edit.php?tablename=$tablename">Edit an entry</a>
<li><a href="del.php?tablename=$tablename">Delete an entry</a>
</ul>"
?>
</body>
</html>
To be continued.