php与mysql数据库增删改查的一些操作!

| |
时间: 不指定 2007/07/26 00:11 , 作者: admin
php与mysql通过修改完善一个php商城程序而记录的一些常用的php-mysql的基本应用!
1:数据库连接:

<?php
session_start();
$conn =  mysql_connect('127.0.0.1','root','') or die(mysql_error);
mysql_select_db('shoping',$conn);
mysql_query("SET character_set_results = gb2312") ;
mysql_query("SET character_set_client = gb2312");
mysql_query("SET character_set_connection = gb2312");


2:嵌入其他:

<?php
include("conn.php");
?>


3:简单的数据库调用输出

               <?php
     $sql_news="select id,title from news order by id desc";
     $rs_news=mysql_query($sql_news);
     while(list($news_id,$title)=mysql_fetch_row($rs_news))
     {
     ?>
               <tr>
                 <td width="10"> </td>
                 <td><a href="*.php?id=<?php echo $news_id;?>" title="<?php echo $title;?>" target="_blank"><?php echo $title;?></a></td>
               </tr>
               <?php
     }
     ?>


4:按一行12个循环排列

<table>
<tr>
           <?php
     $sql="select id,name from shoping_type";
     $rs=mysql_query($sql);
     $i=1;
     $j=mysql_num_rows($rs);
     while(list($id,$name)=mysql_fetch_row($rs))
     {
   ?>
           <td height="25" nowrap><a href="more_types2.php?shoping_id=<?php echo $id;?>" target="_blank" ><font color="#FF0000"><?php echo $name;?></font></a> <font color="#FF0000"><?php if($i!=$j and $i!=12){?>&#124; <?php }?></font> </td>
           <?php
    if($i%12==0)
    {
   echo "</tr><tr>";
    }
 $i++;
   }
 ?>
</table>

另一种循环

<table>
                 <tr>
                   <?php
     $sql_shoping_type="select id,name,img from shoping_type order by id";
     $rs_shoping_type=mysql_query($sql_shoping_type);
     $j=1;
     while(list($id,$name,$img)=mysql_fetch_row($rs_shoping_type))
     {
     ?><td></td>
.................................................................................
                   <?php
     if($j%3==0)
     {
     ?>
                 </tr>
                 
                 <?php
     }
     $j++;
     }
     ?>
               </table>

5:根据一个字段进行数据库读取
 
 <?php
 $sql1="select img,url from img_news where id=1";
 $rs_news=mysql_query($sql1);
 while(list($img,$url)=mysql_fetch_row($rs_news))
 {
 ?>
 <tr>
   <td colspan="3"><div align="center"><span class="zi"><a href="<?php echo $url;?>" target="_blank"><img name="shopping_new_r8_c1" src="<?php echo $img;?>" width="100%" height="104" border="0" alt=""></a></span></div></td>
 </tr>
 <?php
 }
 ?>

6:根据一个id进行数据库读取,只提取6个!

             <?php
     $select_say="select id,title,date_time from news where newsdalei_id='16'";  
     $select_say.=" order by id desc";
     //echo $select_say;
     $rs_say = mysql_query($select_say);
     $j=1;
     while(list($id,$title,$date_time)=mysql_fetch_row($rs_say) and $j<=6)
     {
     ?>
             <tr>
               <td width="5" align="right"> </td>
               <td align="left"><a href="newsinfo.php?id=<?php echo $id;?>"><?php echo $title;?></a></td>
             </tr>
   <?php
   $j++;
   }
  ?>

7:同上,..另一种范围读取

<?php
     $sql_product="select id,name from xiaolei where dalei_id='".$id."' limit 0,4";
     //echo $sql_product;
     $rs_product=mysql_query($sql_product);
     $i=1;
     while(list($xiaolei_id,$name)=mysql_fetch_row($rs_product))
      {
     ?>
                       <td height="22" align="left"> <a href="shoplist.php?xiaolei_id=<?php echo $xiaolei_id;?>" target="_blank"><?php echo $name;?></a></td>
                       <?php
     if($i%1==0)
      {
     echo "</tr><tr>";
        }
     $i++;
      }
     ?>


7:读取来路的id:


$id=$_GET["id"];
$sql_news="select id,title,content,date_time from news where id=".$id;

或者
$sql_news="select id,title,content,date_time from news where id=".$_GET["id"];

8:插入:


$sql = "insert into product2(product_name,user_id) values('".$_POST["product_name"]."','".$_SESSION["session_shop_user_id"]."')";


9更改:

$sql="update product2 set product_name='".$_POST["product_name"]."',
if($_FILES['img_urld'][tmp_name]!="")
   {
     $sql.="img='".$url1a."',";
   }
   
   $sql.="reback_address='".$_POST["reback_address"]."',
   user_id='".$_SESSION["session_shop_user_id"]."' where id='".$_POST["id"]."'";

10:删除:

<?php
include("../../conn.php");
 $sql="delete from product2 where id=".$_GET["id"];
 //echo $sql;
 $rs=mysql_query($sql);
 //var_dump($rs)."ddddddd";
 echo "<script>alert('删除成功!');window.location='shop_center.php';</script>";
 exit;
?>


11:返回前进

echo "<script>alert('提交成功!');window.location='shop_center.php';</script>";
 exit;
}
else
{
 echo "<script>alert('添加失败!');histroy.back();</script>";
 exit;
}


12:简单的搜索与分页:
搜索部分  
本页list.php

 
<form action="list.php" method="post" name="form1">
 <tr class="forumRowHighlight">
   <td bgcolor="#1865A5">按商品名称查询:
     <input name="productname" type="text" value="<?php echo $_POST["productname"];?>"></td>
   <td bgcolor="#1865A5">按品牌查询:
     <input name="brand_name" type="text" value="<?php echo $brand_name;?>"></td>
     <td bgcolor="#1865A5"><input type="submit" class="buttonstyle" value="查找"></td>
     <td bgcolor="#1865A5"><input type="reset" class="buttonstyle" value="重置"></td>
 </tr></form>
下部是分页与显示文章部分
<table>
<?php
   $sql="select id,product_name from product where 1=1";
   if(trim($_POST["productname"])!="")
   {
     $sql.=" and product_name like '%".trim($_POST["productname"])."%' order by id desc";
   }
   elseif(trim($brand_name)!="")
   {
     $sql.=" and product_brand like '%".trim($brand_name)."%' order by id desc";
   }
   else
   {
     $sql.=" order by id desc";
   }
   //echo $sql;
   $rs = mysql_query($sql);
   $pagesize = 15;
   $cnt = mysql_num_rows($rs);
   if($cnt==0)
       {
         echo "暂无数据";
         exit;
       }
   $pagecount = ceil($cnt/$pagesize);
   if(!isset($pageno)) $pageno=1;
   if($pageno<1) $pageno = 1;
   if($pageno>$pagecount) $pageno = $pagecount;
   mysql_data_seek($rs,($pageno-1)*$pagesize) or die("无法定位数据!");
   $j=0;
 ?>  



.......................................内容循环部分
<?php
   while(list($id,$product_name)=mysql_fetch_row($rs) and $j < $pagesize)
   {
 ?>
<tr><td></td></tr>
<?php
   $j++;
   }
  ?>
</table>
.......................................内容循环部分

<table width="80%"  border="0" align="center" cellpadding="0" cellspacing="0">  <tr>    <td height="30" align="center" class="linkus">共<?php echo mysql_num_rows($rs);?>篇 <a href="list.php?pageno=1&productname=<?echo trim($_POST["productname"]);?>&brand_name=<?php echo trim($brand_name);?>">首页</a> <a href="list.php?pageno=<?php echo $pageno-1;?>&productname=<?echo trim($_POST["productname"]);?>&brand_name=<?php echo trim($brand_name);?>">上一页</a> <a href="list.php?pageno=<?php echo $pageno+1;?>&productname=<?echo trim($_POST["productname"]);?>&brand_name=<?php echo trim($brand_name);?>">下一页</a> <a href="list.php?pageno=<? echo $pagecount;?>&productname=<?echo trim($_POST["productname"]);?>&brand_name=<?php echo trim($brand_name);?>">尾页</a> 页次:<?php echo $pageno;?>/<?php echo $pagecount;?>页 <?php echo $pagesize?>篇/页 转到:        
 <select name="pageno" onChange="MM_jumpMenu(this.value)">          <?php
         for($i=1;$i<=$pagecount;$i++)
         {            
           $option_list.="<option value='".$i."'";
           if($pageno==$i) $option_list.="selected";
           $option_list.=">第".$i."页</option>";
         }
         echo $option_list;
         ?>        </select>



其中的js

<script>
<!--
function del()
{
 document.all.cnt.value=2;
 if(document.all.del1.value <= 0 &amp;amp;#124;&amp;amp;#124; document.all.del1.value=="")
 {
   alert("请选择要删除信息!");
   return false;
 }
 else
 {
   if(confirm("确认要删除这些信息?"))
   {
     hrong.submit();
   }
   else
   {
     return false;
   }    
 }
}
function clk(aa)
{
 var cnt = Number(document.all.cnt.value);
 var del = Number(document.all.del1.value);  
 if(aa)
 {
   cnt = cnt + 1;
   del = del + 1;
   document.all.cnt.value=cnt;
   document.all.del1.value = del;
 }
 else
 {
   cnt =cnt - 1;
   del =del - 1;
   document.all.cnt.value=cnt;
   document.all.del1.value=del;
 }
}

function MM_jumpMenu(pageno)
{
 window.location="list.php?pageno="+pageno+"&amp;amp;productname="+document.all.productname.value+"&amp;amp;brand_name="+document.all.brand_name.value;
}
//-->
</script>


13:一个onmouseover 属性

<tr id="tr<?php echo $j;?>" onMouseOver="FocusMe(this.id);" onDblClick="window.location='add.php?id=<?php echo $id; ?>'">



14:一个二级分类

<script language="javascript">
function ClickType(typeclass) {
 frmUpdate.tType.value=selecttype(frmUpdate);
}
function inittype(myfrm)
{
       var type1box;
       type1box = myfrm.selJWHName;
       for(i = 1; i <= tIndexes.length-1; i++)
       {
               oOption=document.createElement("OPTION");
               oOption.text=tName[i];
               oOption.value=tIndexes[i];
               type1box.add(oOption);
       }
       
       for(i = 1; i <= tIndexes.length-1; i++)
       {
           if(tIndexes[i]=="<? echo trim($product_type1);?>"){
       
                 type1box.options[i-1].selected = true;
               }
       }
       type1box.length = i-1;
}

function selecttype(myfrm)
{
       var type1box;
       type1box = myfrm.selJWHName;
       seltype = parseInt(type1box.selectedIndex);
       seltype++;
       tIdx = tIdxs[seltype];
       tContent = tContents[seltype];
       if(tIdx != null && tContent != null)
       {
         type2box = myfrm.selStreetName;
         type2box.length = 0;
               for(i = 0; i < tIdx.length; i++)
               {
                 oOption = document.createElement("OPTION");
                 oOption.text = tContent[i];
                 oOption.value = tIdx[i];
                 
                 //str = /*i + "." + */tContent[i];
                       //type2box.options[i] = new Option(str, tIdx[i]);
                       type2box.add(oOption);
                }
                for(i = 0; i < tIdx.length; i++)
               {
                  if(tIdx[i] == "<?php echo $newsxiaolei_id;?>"){
                   type2box.options[i].selected = true;
                 }
               }
               type2box.length = i;  
        }
}

</SCRIPT>
<script language="Javascript">
tIndexes = new Array;
tName = new Array;
tIdxs = new Array;
tContents = new Array;
<?
$sql = "select Id,Name from newsdalei order by id desc";
$rsjwh= mysql_query($sql);
$i = 1;
while(list($jwhid,$jwhName) = mysql_fetch_row($rsjwh)){
?>
tIndexes[<?echo $i;?>]=<?echo $jwhid;?>;
tName[<?echo $i;?>]="<?echo $jwhName;?>";
tIdxs[<?echo $i;?>] = new Array;
tContents[<?echo $i;?>] = new Array;
<?
   $sql1 = "select id,Name from newsxiaolei where dalei_id=$jwhid";
   $rs1 =mysql_query($sql1);
   $j = 0;
   while(list($tid,$tName)=mysql_fetch_row($rs1)){
?>
tIdxs[<?echo $i;?>][<?echo $j;?>] = <?echo $tid;?>;
tContents[<?echo $i;?>][<?echo $j;?>] = "<?echo $tName;?>";
<?
$j++;
}
$i++;
}
?>
</script>


<FORM name="frmUpdate" action="add_ok.php" method="post" enctype="multipart/form-data" onSubmit="return frmsubmit();">
<input type="hidden" name="id" value="<?php echo $_GET["id"];?>">
<INPUT name=tType type=hidden>
<td height="30" colspan="3" class="forumRowHighlight">
     <br>
     大类:<select name="selJWHName" class="formsty"  onChange="ClickType(1);" style="width:290px;">
 <script language=Javascript>inittype(frmUpdate);</script>
</select>          
<br>
<br>          
小类:<select name="selStreetName" class="formsty" style="width:290px;">
                       <script language="javascript">
                       ClickType(1);
                       </script>
                 </select>
     </td>
<input name="Submit" type="submit" value="确定">
       <input name="Submit" type="reset" value="重写"></td>
</FORM>
  • 中查看更多“php与mysql数据库增删改查的一些操作!”相关内容
  • 中查看更多“php与mysql数据库增删改查的一些操作!”相关内容
  • 中查看更多“php与mysql数据库增删改查的一些操作!”相关内容

  • 最后编辑: admin 编辑于2007/07/26 00:19
    类别:mysql::mssql | Tags: , | 0 条评论, 1062 次阅读
    网友评论(0):
    发表评论
    表情
    emotemotemotemot
    emotemotemotemot
    emotemotemotemot
    emotemotemotemot
    emotemotemotemot
    emotemotemotemot
    emotemotemotemot
    emotemotemotemot
    打开HTML
    打开UBB
    打开表情
    隐藏
    昵称   密码   游客无需密码
    网址   电邮   [注册]
                   

    验证码 不区分大小写