php编程 ·

php mysql增删改查实例教程

PHP5+Mysql5+Apache2已经是目前最流行的建站模式了。在对Mysql的表操作的时候如果仅仅使用sql语言对Mysql去操作未
免太繁琐了一点。于是笔者参考互联网上的代码通过修改写了一套对各种表进行添加、删除、修改记录的php代码。仅供大家参考娱乐。
php mysql增删改查实例教程(一)
以下贴出的代码在PHP5+Mysql5+Apache2 for WinXp上调试通过。

首先说明一下代码适用范围,按照大多数表的特点,每个表都有id,所以这些代码适用于第一列为id的表。至于名称起什么无关紧要,重要的是代码都是
默认按照第一列为primary
key来进行操作。其他并无任何限制,本代码可以对任意列的表进行操作。缺点:未进行输入审查。所以输入记录时不要空行也不要输不匹配的类型,这个需要与
Mysql的建表相配合。

注意:提醒!以上代码在copy的时候一定注意在编辑器里面把每一行代码前面的tab去掉,不然php5编译不过。
而且在每个文件中$database目前为sunsite,$table为software,这个可以根据需要修改。

首先是index.php,主要实现功能为让用户输入表名,然后点击查看按钮就可以看到表的内容。也是默认的首页。

//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>

然后是tables.php,功能为显示出表的内容。并附有添加删除和修改的入口。

//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>

续:点击添加一个条目就会跳转到add.php
//add.php

<html>
<head><title>Add an entry to the database</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body bgcolor=#ffffff>
<h2>Add an entry</h2>
<?php
$database = "sunsite";
$tablename = $_GET['tablename'];
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)
{
while ($r = mysql_fetch_array($result))
{
$colname[$column] = $r[0];
$column = $column + 1;
}
mysql_free_result($result);
}
$id = 0;
do
{
$id++;
$query = "select * from $tablename where $colname[0]='$id'";
$result = mysql_db_query($database,$query);
}
while(mysql_fetch_array($result))
?>
<form method="post" action="addsoftware.php">
<table width=90% align=center>
<tr><td><?php echo "$colname[0]";?>:</td><td><?php echo "$id"; ?></td></tr>
<?
for ($col=1;$col<$column;$col++)
echo
"<tr><td>$colname[$col]:</td><td><input
type=text name=$colname[$col] size="100%"></td></tr>";
?>
<input type=hidden name="<?php echo "$colname[0]"; ?>" value="<?php echo "$id"; ?>">
<input type=hidden name=tablename value="<?php echo "$tablename"; ?>">
<tr><td></td><td><input type=submit value=" Add "></td></tr>
</table>
</form>
<a href="tables.php?tablename=<? echo "$tablename"?>">Finish</a>
</body>
</html>

其中id是自动寻找并生成的,而且保证在数据库中id是唯一的。点击add之后会将内容写入数据库,然后会继续让你添加内容,直至添加完毕点击finish回到上一层,显示出添加条目後的数据库内容。
下面是添加过程中对数据库进行操作的addsoftware.php
//addsoftware.php

<?php
$database = "sunsite";
$tablename = $_POST['tablename'];
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)
{
while ($r = mysql_fetch_array($result))
{
$colname[$column] = $r[0];
$column = $column + 1;
}
mysql_free_result($result);
}

for($col=0;$col<$column;$col++)
$para[$col] = $_POST[$colname[$col]];

if ($_POST['name'])
{
mysql_connect("localhost","root","") or die ("Problem connecting to DataBase");
$query = "insert into $tablename values ('$para[0]' ";
for($col=1;$col<$column;$col++)
$query = $query . ",'" . $para[$col] . "'";
$query = $query . ");";

$result = mysql_db_query($database, $query);
Header("Location: add.php?tablename=$tablename");
}
else
{
echo "No name Entered. Please go back and reenter name";
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

续:点击编辑一个条目会跳转至edit.php
//edit.php

<html>
<head><title>Editing an entry from the database</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body bgcolor=#ffffff>
<h2>Edit an entry</h2>
<?
$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>";
$colname[$column] = $r[0];
$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>";
echo "<td><a href="editing.php?$colname[0]=$r[0]&tablename=$tablename">$r[0]</td>";
for($col=1;$col<$column;$col++) echo "<td>$r[$col]</td>";
echo "</tr>";
}
echo "</table>";
}
else echo "No data.";
mysql_free_result($result);
?>
<a href="tables.php?tablename=<? echo "$tablename"?>">Finish</a>
</body>
</html>

然后选择其中一个id,会跳出具体的内容并要求用户修改.
//editing.php

<html>
<head><title>Editing an entry</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body bgcolor=#ffffff>
<h1>Editing an entry</h1>
<?
$database = "sunsite";
$tablename = $_GET['tablename'];
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)
{
while ($r = mysql_fetch_array($result))
{
$colname[$column] = $r[0];
$column = $column + 1;
}
mysql_free_result($result);
}
$temp = $_GET[$colname[0]];
$query = "select * from $tablename where $colname[0]=$temp";
$result = mysql_db_query($database,$query);
$r = mysql_fetch_array($result);
?>
<form method="post" action="editdb.php">
<table width=90% align=center>
<tr><td><?php echo "$colname[0]";?>:</td><td><?php echo "$r[0]"; ?></td></tr>
<?
for ($col=1;$col<$column;$col++)
echo
"<tr><td>$colname[$col]:</td><td><input
type=text name=$colname[$col] size="100%"
value="$r[$col]"></td></tr>";
?>
<input type=hidden name=id value="<?php echo "$r[0]"; ?>">
<input type=hidden name=tablename value="<?php echo"$tablename";?>">
<tr><td></td><td><input type=submit value="Confirm!"></td></tr>
</table>
</form>
</body>
</html>

最后是写入数据库
//editdb.php

<?
$database = "sunsite";
$tablename = $_POST['tablename'];
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)
{
while ($r = mysql_fetch_array($result))
{
$colname[$column] = $r[0];
$column = $column + 1;
}
mysql_free_result($result);
}
for($col=0;$col<$column;$col++)
$para[$col] = $_POST[$colname[$col]];
if ($_POST['name'])
{
mysql_connect("localhost","root","") or die ("Problem connecting to DataBase");
$query = "update $tablename set $colname[1]='$para[1]'";
for($col=2;$col<$column;$col++)
$query = $query . ",$colname[$col]='$para[$col]'";
$query = $query . " where $colname[0]='$para[0]';";
$result = mysql_db_query($database, $query);
Header("Location: edit.php?tablename=$tablename");
}
else
{
echo "No name Entered. Please go back and reenter name";
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

待续.

续:如果点击删除一个条目则会跳转到del.php
//del.php

<html>
<head><title>Deleting an entry from the database</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body bgcolor=#ffffff>
<h2>Delete an entry</h2>
<?
$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>";
$colname[$column] = $r[0];
$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>";
echo "<td><a href="dele.php?$colname[0]=$r[0]&tablename=$tablename">$r[0]</td>";
for($col=1;$col<$column;$col++) echo "<td>$r[$col]</td>";
echo "</tr>";
}
echo "</table>";
}
else echo "No data.";
mysql_free_result($result);
?>
<a href="tables.php?tablename=<? echo "$tablename"?>">Finish</a>
</body>
</html>

然后写入数据库
//dele.php

<?php
$database = "sunsite";
$tablename = $_GET['tablename'];
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)
{
while ($r = mysql_fetch_array($result))
{
$colname[$column] = $r[0];
$column = $column + 1;
}
mysql_free_result($result);
}
$para = $_GET[$colname[0]];
mysql_connect("localhost","root","") or die ("Problem connecting to DataBase");
$query = "delete from $tablename where $colname[0]='$para';";
$result = mysql_db_query($database, $query);
Header("Location: edit.php?tablename=$tablename");
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

以上就是所有的代码了.
把它们都放到同一个目录下面即可.
注意:提醒!以上代码在copy的时候一定注意在编辑器里面把每一行代码前面的tab去掉,不然php5编译不过.
而且在每个文件中$database目前为sunsite,$table为software,这个可以根据需要修改.
续:如果点击删除一个条目则会跳转到del.php
//del.php

<html>
<head><title>Deleting an entry from the database</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body bgcolor=#ffffff>
<h2>Delete an entry</h2>
<?
$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>";
$colname[$column] = $r[0];
$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>";
echo "<td><a href="dele.php?$colname[0]=$r[0]&tablename=$tablename">$r[0]</td>";
for($col=1;$col<$column;$col++) echo "<td>$r[$col]</td>";
echo "</tr>";
}
echo "</table>";
}
else echo "No data.";
mysql_free_result($result);
?>
<a href="tables.php?tablename=<? echo "$tablename"?>">Finish</a>
</body>
</html>

然后写入数据库
//dele.php

<?php
$database = "sunsite";
$tablename = $_GET['tablename'];
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)
{
while ($r = mysql_fetch_array($result))
{
$colname[$column] = $r[0];
$column = $column + 1;
}
mysql_free_result($result);
}
$para = $_GET[$colname[0]];
mysql_connect("localhost","root","") or die ("Problem connecting to DataBase");
$query = "delete from $tablename where $colname[0]='$para';";
$result = mysql_db_query($database, $query);
Header("Location: edit.php?tablename=$tablename");
?>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

以上就是所有的代码了.
把它们都放到同一个目录下面即可.
注意:提醒!以上代码在copy的时候一定注意在编辑器里面把每一行代码前面的tab去掉,不然php5编译不过.
而且在每个文件中$database目前为sunsite,$table为software,这个可以根据需要修改.

参与评论