首页 > 开发 > PHP > 正文

一个对数据库进行操作的程序

2024-05-04 22:59:15
字体:
来源:转载
供稿:网友

db.func:

<?
function getrows( $database, $query ) {
   mysql_connect ("ftp", "root", "");
   $db = mysql_select_db( $database );
   if ( ! $db ) {
      echo "无法连结数据库!";
      exit;
   }

   $rows = mysql_query( $query );
   if ( ! $rows ) {
      echo "sql指令错误!";
      exit;
   }
   return $rows;
}

function connectdatabase( $database ) {
   mysql_connect ("ftp", "root", "");
   $db = mysql_select_db( $database );
   if ( ! $db ) {
      echo "无法连结数据库!";
      exit;
   }
}
?>

cratetable.php:

<?
function createtable() {
   $link = mysql_connect ("ftp", "root", "");
   $db = mysql_select_db("pcadmin");
   if ( ! $db ) {
      echo "连接到数据库失败!";
      exit;
   }
   $rows = mysql_query("select * from profile");
   if ( ! $rows ) {
      $sql = "create table profile (id integer auto_increment primary key,name char(255),produce char(255),lianluo char(255),tel integer(15),email char(255))";
      mysql_query($sql);
   }
  
   $rows = mysql_query("select * from aplication");
   if ( ! $rows ) {
      $sql = "create table aplication(name char(255) primary key,ip char(255),ap char(255),directory char(255))";
      mysql_query($sql);
   }
   mysql_close ($link);
}
createtable();
?>

input.htm:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>輸入表單</title>
<style type="text/css">
<!--
.input {
 border: 1px solid #003c74;
}
.font {
 font-family: "新宋体";
 font-size: 14px;
 color: #0000ff;
 font-weight: lighter;
}
.table {
 border: 1px solid #000000;
}
.title {
 font-family: "新宋体";
 font-size: 14px;
 color: #ff0000;
}
-->
</style>
</head>

<body><h3><center class="title">輸入表單
</center><hr size="1" noshade>
</h3>

<form name="form1" method="post" action="insert.php">
<table width="400"align=center>
 <tr>
    <th scope="row"><div align="left" class="font">主機名稱</div></th>
    <td><input name="name" type="text" class="input"></td>
  </tr>
  <tr>
    <th scope="row"><div align="left" class="font">生產廠商</div></th>
    <td><input name="produce" type="text" class="input"></td>
  </tr>
  <tr>
    <th scope="row"><div align="left" class="font">聯絡人</div></th>
    <td><input name="lianluo" type="text" class="input">      </td>
  </tr>
  <tr>
    <th scope="row"><div align="left" class="font">聯絡人電話</div></th>
    <td><input name="tel" type="text" class="input"></td>
  </tr>
  <tr>
    <th scope="row"><div align="left" class="font">聯絡人郵箱</div></th>
    <td><input name="email" type="text" class="input"></td>
  </tr>
  <tr>
    <th scope="row"><p align="left" class="font">ip address</p>
      </th>
    <td><input name="ip" type="text" class="input"></td>
  </tr>
  <tr>
    <th scope="row"><div align="left" class="font">應用名稱</div></th>
    <td><input name="ap" type="text" class="input"></td>
  </tr>
  <tr>
    <th align="left" valign="top" scope="row"><div align="left" class="font">配置文件</div></th>
    <td><textarea name="directory" cols="40" rows="5" class="input" clums="15"></textarea></td>
  </tr>
  <tr>
    <th scope="row"><input name="send" type="submit" class="input" value="提交"></th>
    <td><input name="submit" type="reset" class="input" value="重寫">
      <a href="show.php">查看记录</a></td>
  </tr>
</table>

</form><hr size="1" noshade>
<p>&nbsp;</p>
</body>
</html>


insert.php:

<html>
<style type="text/css">
<!--
.font {
 font-family: "新宋体";
 font-size: 14px;
 font-weight: lighter;
 color: #ff0000;
}
-->
</style>
<body bgcolor="#ffffff">
<h3><a href="show.php" class="font">成功輸入一筆資料去看看</a> 
<hr></h3>
<?
include("../db.func");

if ( !empty($send) ) {
   connectdatabase( "pcadmin" );
   $sql = "insert into profile(name,produce,lianluo,tel,email) values('$name','$produce','$lianluo','$tel','$email')";
  
   mysql_query( $sql );
    }
?>
</body>
</html>

show.php:

<?
function showonepage( $rows, $no ) {
  mysql_data_seek( $rows, $no );
 

  echo "<table border=1 align=center bordercolordark='#ffffff'  bordercolorlight='#000000' cellpadding='3' cellspacing='0' >";
  echo "<tr bgcolor=cyan>";
  while ( $field = mysql_fetch_field($rows) ) {
     echo "<td>" . $field->name . "</td>";
  }
  echo "</tr>";
  $nums = mysql_num_rows($rows);
  for ( $i=1; $i <=10; $i++ ) {
     if ( $row = mysql_fetch_row($rows) ) {
          for ( $k=0 ; $k < count($row); $k++ ) {
           echo "<td align=right>" . $row[$k] . "</td>";
        }
        echo "</tr>";
     }
  }
  echo "</table>";
}
?>

<html>
<style type="text/css">
<!--
.title {
 color: #ff0000;
 font-family: "新宋体";
 font-size: 14px;
}
.body {
 font-family: "新宋体";
 font-size: 12px;
 font-weight: lighter;
 color: #0000ff;
}
-->
</style>
<title>伺服器清单</title><body bgcolor="#ffffff">
<h2 align=center><span class="title">伺服器清單</span>
  <hr size="1" noshade color="#0000ff" class="hr" >
</h2>
<?
  include("../db.func");
  $rows = getrows( "pcadmin", "select * from profile" );

  $num = mysql_num_rows($rows);
  $pages = intval(($num - 1) / 10) + 1;

  if ( $page < 1 ) $page = 1;
  if ( $page > $pages ) $page = $pages;

  showonepage( $rows, ($page-1)*10 );
?>
<hr size="1" noshade color="#0000ff" class="hr">
<form action=<? echo $php_self;?> method=get >
<div align=right>
<?
if ( $page <> 1 ) {  // 当不是第一页时
   echo "<a href=$php_self?page=1>第一页</a>";
   echo "<a href=$php_self?page=" . ($page-1) . ">上一页</a>";
}

if ( $page <> $pages ) {  // 当不是最后一页时

 echo "<a href=$php_self?page=" . ($page+1) . ">下一页</a>";
   echo "<a href=$php_self?page=$pages>最后一页</a>";
}
?>
<span class="body">跳转</span>:
<input type=text name=page size=3>
<span class="body">当前页</span>:<font color=red><?echo $page;?>/<?echo $pages;?>
</div>
</form>
</body>
</html>

cxshow.php:

<title>get id</title>
<?php

if ($id){
//编辑状态,选择一条记录
include("../db.func");
mysql_connect("ftp","root","");
mysql_select_db("pcadmin");
$sql = "update profile set name='$name',produce='$produce',lianluo='$lianluo',tel='$tel',email='$email' where id=5";
mysql_query($sql);
//$rows = getrows( "pcadmin", "select * from profile where id=$id" );
//$myrow = mysql_fetch_array($rows);
//$id = $myrow["id"];
//$name = $myrow["name"];
//$produce = $myrow["produce"];
//$lianluo = $myrow["lianluo"];
//$tel = $myrow["tel"];
//$email = $myrow["email"];

echo "你的id是'$id'";

exit;
} else {
echo "failure!";
exit;
}

?>

printf.php:

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
<?php
$db=mysql_connect("ftp","root","");
mysql_select_db ("pcadmin",$db);
if ($id) {
  echo "有id的情况,id是:$id<br>";
  if ($submit){
  echo "执行了submit时:<br>";
  $sql = "update profile set name='$name',produce='$produce',lianluo='$lianluo',tel='$tel',email='$email' where id=$id";
  $result = mysql_query($sql);
  echo "thank you!information updated!";
  } else {
          echo "没有执行submit时:<br>";
          $sql= "select * from profile where id=$id";
          $result = mysql_query($sql);
          $myrow = mysql_fetch_array($result);
          ?>
<form action = <? echo $php_self;?> method=post>
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
名称:<input type="test" name="name" value="<? echo $myrow["name"] ?>"><br>
厂商:<input type="test" name="produce" value="<? echo $myrow["produce"] ?>"><br>
联络:<input type="test" name="lianluo" value="<? echo $myrow["lianluo"] ?>"><br>
电话:<input type="test" name="tel" value="<? echo $myrow["tel"] ?>"><br>
邮箱:<input type="test" name="email" value="<? echo $myrow["email"] ?>"><br>
<input type="submit" name="submit" value="输入信息">
</form>
<?
}
} else {
       echo "没有id的情况:<br>";
       $result = mysql_query("select * from profile",$db);
       if ($myrow = mysql_fetch_array($result)){
       do {
        printf ("<a href=/"%s?id=%s/">%s %s %s %s %s</a><br>/n",$path_info, $myrow["id"],$myrow["name"],$myrow["produce"],$myrow["lianluo"],$myrow["tel"],$myrow["email"]);
       } while ($myrow = mysql_fetch_array($result));
       } else {
       echo "对不起,没有找到相关记录!";
       exit;
       }
       }
?>

delete.php:

<title>删除一条记录</title><?php
$db = mysql_connect("ftp","root","");
mysql_select_db("pcadmin",$db);
$sql="delete from profile where id=$id";
$result=mysql_query($sql);
echo "delete one record!";
?>

 



收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表