详解php实现商品添加功能并写入数据库

本篇文章给大家带来了关于php的相关知识,其中主要介绍了怎么通过php实现商品添加功能,并写入数据库,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

详解php实现商品添加功能并写入数据库

数据库表3cedc3b33ae764c77516c1f554362d5.png 写入数据的代码

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

 header ( "Content-type: text/html; charset=gb2312" );
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加商品</title>
<link rel="stylesheet" type="text/css" href="css/font.css">
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>
<?php include("conn/conn.php");?>
<body topmargin="0" leftmargin="0" bottommargin="0">
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="20" bgcolor="#FFCF60"><div align="center" class="style1">添加商品</div></td>
  </tr>
  <tr>
    <td height="253" bgcolor="#666666"><table width="720" border="0" cellpadding="0" cellspacing="1">
	<script language="javascript">
	function chkinput(form)
	{
	  if(form.mingcheng.value=="")
	   {
	     alert("请输入商品名称!");
		 form.mingcheng.select();
		 return(false);
	   }
	  
	
	
	  if(form.huiyuanjia.value=="")
	   {
	     alert("请输入商品会员价!");
		 form.huiyuanjia.select();
		 return(false);
	   }
	 
	
	
	  if(form.shichangjia.value=="")
	   {
	     alert("请输入商品市场价!");
		 form.shichangjia.select();
		 return(false);
	   }
	  if(form.dengji.value=="")
	   {
	     alert("请输入商品等级!");
		 form.dengji.select();
		 return(false);
	   }
	   
	   
	   if(form.pinpai.value=="")
	   {
	     alert("请输入商品品牌!");
		 form.pinpai.select();
		 return(false);
	   }
	   
	   if(form.xinghao.value=="")
	   {
	     alert("请输入商品型号!");
		 form.xinghao.select();
		 return(false);
	   }
	   if(form.shuliang.value=="")
	   {
	     alert("请输入商品数量!");
		 form.shuliang.select();
		 return(false);
	   }
	   if(form.jianjie.value=="")
	   {
	     alert("请输入商品简介!");
		 form.jianjie.select();
		 return(false);
	   }
	
	   return(true);
	}
    </script>
     <form name="form1" enctype="multipart/form-data" method="post" action="savenewgoods.php" onSubmit="return chkinput(this)">
	  <tr>
        <td width="129" height="25" bgcolor="#FFFFFF"><div align="center">商品名称:</div></td>
        <td width="618" bgcolor="#FFFFFF"><div align="left"><input type="text" name="mingcheng" size="25" class="inputcss"></div></td>
      </tr>
      <tr>
        <td height="25" bgcolor="#FFFFFF"><div align="center">上市时间:</div></td>
        <td height="25" bgcolor="#FFFFFF"><div align="left">
<select name="nian" class="inputcss">
 <?php 
  for($i=1995;$i<=2050;$i++)
  {
 ?>
  <option><?php echo $i;?></option>
  <?php 
  }
 ?>
</select>          
年
          <select name="yue" class="inputcss">
            <?php 
            for($i=1;$i<=12;$i++)
             {
            ?>
           <option><?php echo $i;?></option>
            <?php 
             }
             ?>
          </select>
          月
          <select name="ri" class="inputcss">
		  <?php 
            for($i=1;$i<=31;$i++)
             {
            ?>
		  
            <option><?php echo $i;?></option>
		 <?php 
             }
             ?>
          </select>
          日</div></td>
      </tr>
      <tr>
        <td height="25" bgcolor="#FFFFFF"><div align="center">价格:</div></td>
        <td height="25" bgcolor="#FFFFFF"><div align="left">市场价:<input type="text" name="shichangjia" size="10" class="inputcss" >
          元&nbsp;&nbsp;会员价:
          <input type="text" name="huiyuanjia" size="10" class="inputcss">
          元</div></td>
      </tr>
      <tr>
        <td height="25" bgcolor="#FFFFFF"><div align="center">商品类型:</div></td>
        <td height="25" bgcolor="#FFFFFF"><div align="left">
           <?php
			$sql=mysqli_query($conn,"select * from tb_type order by id desc");
			$info=mysqli_fetch_array($sql);
			if($info==false)
			{
			  echo "请先添加商品类型!";
			}
			else
			{
			?>
            <select name="typeid" class="inputcss">
			<?php
			do
			{
			?>
              <option value=<?php echo $info[&#39;id&#39;];?>><?php echo $info[&#39;typename&#39;];?></option>
			<?php
			}
			while($info=mysqli_fetch_array($sql));
			?>  
            </select>
            <?php
		     }
		    ?>
        </div></td>
      </tr>
      <tr>
        <td height="25" bgcolor="#FFFFFF"><div align="center">商品等级:</div></td>
        <td height="25" bgcolor="#FFFFFF"><div align="left">
          <select name="dengji" class="inputcss">
            <option selected value="精品">精品</option>
            <option value="一般">一般</option>
            <option value="二手">二手</option>
            <option value="淘汰">淘汰</option>
          </select>
        </div></td>
      </tr>
      <tr>
        <td height="22" bgcolor="#FFFFFF"><div align="center">商品品牌:</div></td>
        <td height="22" bgcolor="#FFFFFF"><div align="left"><input type="text" name="pinpai" class="inputcss" size="20"></div></td>
      </tr>
      <tr>
        <td height="25" bgcolor="#FFFFFF"><div align="center">商品型号:</div></td>
        <td height="25" bgcolor="#FFFFFF"><div align="left"><input type="text" name="xinghao" class="inputcss" size="20"></div></td>
      </tr>
      <tr>
        <td height="25" bgcolor="#FFFFFF"><div align="center">是否推荐:</div></td>
        <td height="25" bgcolor="#FFFFFF"><div align="left">
          <select name="tuijian" class="inputcss" >
            <option selected value=1>是</option>
            <option value=0>否</option>
          </select>
     </div>
      </td>
      </tr>
      <tr>
        <td height="25" bgcolor="#FFFFFF"><div align="center">商品数量:</div></td>
        <td height="25" bgcolor="#FFFFFF"><div align="left"><input type="text" name="shuliang" class="inputcss" size="20"></div></td>
      </tr>
      <tr>
        <td height="25" bgcolor="#FFFFFF"><div align="center">商品图片:</div></td>
        <td height="25" bgcolor="#FFFFFF"><div align="left">
		<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
        <input type="file" name="upfile" class="inputcss" size="30"></div></td>
      </tr>
      <tr>
        <td height="80" bgcolor="#FFFFFF"><div align="center">商品简介:</div></td>
        <td height="25" bgcolor="#FFFFFF"><div align="left"><textarea name="jianjie" cols="80" rows="8" class="inputcss"></textarea>
        </div></td>
      </tr>
      <tr>
        <td height="25" colspan="2" bgcolor="#FFFFFF"><div align="center"><input name="submit" type="submit" class="buttoncss" id="submit" value="添加">
        &nbsp;&nbsp;<input type="reset" value="重写" class="buttoncss"></div></td>
      </tr>
	  </form>
    </table></td>
  </tr>
</table>
</body>
</html>

保存写入数据的处理文件

<?php
header ( "Content-type: text/html; charset=gb2312" ); //设置文件编码格式
include("conn/conn.php");
if(is_numeric($_POST[&#39;shichangjia&#39;])==false || is_numeric($_POST[&#39;huiyuanjia&#39;])==false)
 {
   echo "<script>alert(&#39;价格只能为数字!&#39;);history.back();</script>";
   exit;
 }
if(is_numeric($_POST[&#39;shuliang&#39;])==false)
 {
   echo "<script>alert(&#39;数量只能为数字!&#39;);history.back();</script>";
   exit;
 }
$mingcheng=$_POST[&#39;mingcheng&#39;];
$nian=$_POST[&#39;nian&#39;];
$yue=$_POST[&#39;yue&#39;];
$ri=$_POST[&#39;ri&#39;];
$shichangjia=$_POST[&#39;shichangjia&#39;];
$huiyuanjia=$_POST[&#39;huiyuanjia&#39;];
$typeid=$_POST[&#39;typeid&#39;];
$dengji=$_POST[&#39;dengji&#39;];
$xinghao=$_POST[&#39;xinghao&#39;];
$pinpai=$_POST[&#39;pinpai&#39;];
$tuijian=$_POST[&#39;tuijian&#39;];
$shuliang=$_POST[&#39;shuliang&#39;];
$upfile=$_POST[&#39;upfile&#39;];

if(ceil(($huiyuanjia/$shichangjia)*100)<=80)
 {
 
    $tejia=1;
 }
 else
 {
    $tejia=0;
 }


function getname($exname){
   $dir = "upimages/";
   $i=1;
   if(!is_dir($dir)){
      mkdir($dir,0777);
   }
   
   while(true){
       if(!is_file($dir.$i.".".$exname)){
	       $name=$i.".".$exname;
	       break;
	   }
	   $i++;
	 }

   return $dir.$name;
}

$exname=strtolower(substr($_FILES[&#39;upfile&#39;][&#39;name&#39;],(strrpos($_FILES[&#39;upfile&#39;][&#39;name&#39;],&#39;.&#39;)+1)));
$uploadfile = getname($exname);

move_uploaded_file($_FILES[&#39;upfile&#39;][&#39;tmp_name&#39;], $uploadfile);
if(trim($_FILES[&#39;upfile&#39;][&#39;name&#39;]!=""))
 { 
  $uploadfile="admin/".$uploadfile;
}
else
 {
  $uploadfile="";
 }

$jianjie=$_POST[&#39;jianjie&#39;];
$addtime=$nian."-".$yue."-".$ri;
mysqli_query($conn,"insert into tb_shangpin(mingcheng,jianjie,addtime,dengji,xinghao,tupian,typeid,shichangjia,huiyuanjia,pinpai,tuijian,shuliang,cishu)values(&#39;$mingcheng&#39;,&#39;$jianjie&#39;,&#39;$addtime&#39;,&#39;$dengji&#39;,&#39;$xinghao&#39;,&#39;$uploadfile&#39;,&#39;$typeid&#39;,&#39;$shichangjia&#39;,&#39;$huiyuanjia&#39;,&#39;$pinpai&#39;,&#39;$tuijian&#39;,&#39;$shuliang&#39;,&#39;0&#39;)");
echo "<script>alert(&#39;商品".$mingcheng."添加成功!&#39;);window.location.href=&#39;addgoods.php&#39;;</script>";
?>
推荐学习:《PHP视频教程》

以上就是详解php实现商品添加功能并写入数据库的详细内容,更多请关注其它相关文章!