仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 857|回复: 18
打印 上一主题 下一主题

[学习教程] PHP教程之一个对数据库停止操作的法式

[复制链接]
活着的死人 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:12:10 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
我先把我自己学习PHP的过程做一下概括:法式|数据|数据库   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">主C名Q</div></th>
    <td><input name="Name" type="text" class="input"></td>
  </tr>
  <tr>
    <th scope="row"><div align="left" class="font">生aS商</div></th>
    <td><input name="Produce" type="text" class="INPUT"></td>
  </tr>
  <tr>
    <th scope="row"><div align="left" class="font">j人</div></th>
    <td><input name="Lianluo" type="text" class="INPUT">      </td>
  </tr>
  <tr>
    <th scope="row"><div align="left" class="font">j人</div></th>
    <td><input name="Tel" type="text" class="INPUT"></td>
  </tr>
  <tr>
    <th scope="row"><div align="left" class="font">j人]箱</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">妹Q</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> </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">胜利入一PY料去看看</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 ) {  // p当不是第一页时
   echo "<A HREF=$PHP_SELF?Page=1>第一页</A>@";
   echo "<A HREF=$PHP_SELF?Page=" . ($Page-1) . ">上一页</A>@";
}
if ( $Page <> $Pages ) {  // p当不是最初一页时
 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!";
?>

在学习中,我也一直这样要求着自己。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-22 21:00

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表