溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php如何實(shí)現(xiàn)商品添加功能并寫入數(shù)據(jù)庫

發(fā)布時(shí)間:2023-03-29 15:36:00 來源:億速云 閱讀:121 作者:iii 欄目:編程語言

這篇文章主要介紹了php如何實(shí)現(xiàn)商品添加功能并寫入數(shù)據(jù)庫的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇php如何實(shí)現(xiàn)商品添加功能并寫入數(shù)據(jù)庫文章都會(huì)有所收獲,下面我們一起來看看吧。

數(shù)據(jù)庫表php如何實(shí)現(xiàn)商品添加功能并寫入數(shù)據(jù)庫 

寫入數(shù)據(jù)的代碼

<?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("請(qǐng)輸入商品名稱!");
form.mingcheng.select();
return(false);
  }
 


 if(form.huiyuanjia.value=="")
  {
    alert("請(qǐng)輸入商品會(huì)員價(jià)!");
form.huiyuanjia.select();
return(false);
  }



 if(form.shichangjia.value=="")
  {
    alert("請(qǐng)輸入商品市場價(jià)!");
form.shichangjia.select();
return(false);
  }
 if(form.dengji.value=="")
  {
    alert("請(qǐng)輸入商品等級(jí)!");
form.dengji.select();
return(false);
  }
 
 
  if(form.pinpai.value=="")
  {
    alert("請(qǐng)輸入商品品牌!");
form.pinpai.select();
return(false);
  }
 
  if(form.xinghao.value=="")
  {
    alert("請(qǐng)輸入商品型號(hào)!");
form.xinghao.select();
return(false);
  }
  if(form.shuliang.value=="")
  {
    alert("請(qǐng)輸入商品數(shù)量!");
form.shuliang.select();
return(false);
  }
  if(form.jianjie.value=="")
  {
    alert("請(qǐng)輸入商品簡介!");
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">上市時(shí)間:</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">價(jià)格:</div></td>
       <td height="25" bgcolor="#FFFFFF"><div align="left">市場價(jià):<input type="text" name="shichangjia" size="10" class="inputcss" >
         元&nbsp;&nbsp;會(huì)員價(jià):
         <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 "請(qǐng)先添加商品類型!";
}
else
{
?>
           <select name="typeid" class="inputcss">
<?php
do
{
?>
             <option value=<?php echo $info['id'];?>><?php echo $info['typename'];?></option>
<?php
}
while($info=mysqli_fetch_array($sql));
?>  
           </select>
           <?php
    }
   ?>
       </div></td>
     </tr>
     <tr>
       <td height="25" bgcolor="#FFFFFF"><div align="center">商品等級(jí):</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">商品型號(hào):</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">商品數(shù)量:</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>

保存寫入數(shù)據(jù)的處理文件

<?php
header ( "Content-type: text/html; charset=gb2312" ); //設(shè)置文件編碼格式
include("conn/conn.php");
if(is_numeric($_POST['shichangjia'])==false || is_numeric($_POST['huiyuanjia'])==false)
{
  echo "<script>alert('價(jià)格只能為數(shù)字!');history.back();</script>";
  exit;
}
if(is_numeric($_POST['shuliang'])==false)
{
  echo "<script>alert('數(shù)量只能為數(shù)字!');history.back();</script>";
  exit;
}
$mingcheng=$_POST['mingcheng'];
$nian=$_POST['nian'];
$yue=$_POST['yue'];
$ri=$_POST['ri'];
$shichangjia=$_POST['shichangjia'];
$huiyuanjia=$_POST['huiyuanjia'];
$typeid=$_POST['typeid'];
$dengji=$_POST['dengji'];
$xinghao=$_POST['xinghao'];
$pinpai=$_POST['pinpai'];
$tuijian=$_POST['tuijian'];
$shuliang=$_POST['shuliang'];
$upfile=$_POST['upfile'];

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['upfile']['name'],(strrpos($_FILES['upfile']['name'],'.')+1)));
$uploadfile = getname($exname);

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

$jianjie=$_POST['jianjie'];
$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('$mingcheng','$jianjie','$addtime','$dengji','$xinghao','$uploadfile','$typeid','$shichangjia','$huiyuanjia','$pinpai','$tuijian','$shuliang','0')");
echo "<script>alert('商品".$mingcheng."添加成功!');window.location.href='addgoods.php';</script>";
?>

關(guān)于“php如何實(shí)現(xiàn)商品添加功能并寫入數(shù)據(jù)庫”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“php如何實(shí)現(xiàn)商品添加功能并寫入數(shù)據(jù)庫”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI