PHP 기본 예시: 상품정보 관리 시스템 v11

WBOY
풀어 주다: 2016-08-08 09:23:41
원래의
3586명이 탐색했습니다.

목표 달성하기: php와 mysql을 사용하여 장바구니 기능이 포함된 상품 정보 관리 시스템 작성

1. 데이터베이스 및 테이블 생성

1. 데이터베이스 및 테이블 생성: decodb

2. 테이블 생성: 상품

필드: 상품번호, 상품명, 상품 유형, 상품 사진, 단가, 상품 설명, 재고, 시간 추가

2 .PHP 파일 작성 코드 생성 (다음은 생성할 PHP 파일과 그 용도입니다)

Add.php 상품 추가 페이지

edit.php 상품 정보 편집 양식 페이지

index.php 상품 정보 조회 페이지

action.php 상품 정보 추가, 수정, 삭제 등의 작업 수행

dbconfig.php 공용 구성 파일, 데이터베이스 연결 구성 정보

menu.php 웹사이트 공개 탐색바

업로드/ 업로드된 이미지 저장 디렉터리

function.php 공개 기능 라이브러리 파일: 이미지 정보 업로드, 크기 조정 및 기타 처리 기능

addCart .php 장바구니 정보 추가 작업(구매할 정보를 SESSION에 넣는 작업)

MyCart.php는 장바구니 정보 조회 작업을 구현하고, 상품정보 통계(소계, 합계)를 구현합니다. 가격)

ClearCart.php는 단일 상품을 삭제하거나 장바구니의 장바구니 정보를 지우는 작업을 구현합니다.

updateCart.php 장바구니에 담긴 상품의 개수가 너무 적지 않도록 수정합니다. 제약 조건

각 PHP 파일 관계 다이어그램:

다음은 코드 부분입니다.

첫 번째는 테이블 생성 문입니다.

<span> 1</span><span>create</span><span>database</span> newsdb;<span>//</span><span>创建库语句
</span><span> 2</span><span> 3</span><span>create</span><span>table</span><span> goods (
</span><span> 4</span>     id <span>int</span>(<span>10</span>) unsigned <span>NOT</span><span>NULL</span><span> AUTO_INCREMENT,
</span><span> 5</span>     name <span>varchar</span>(<span>64</span>) <span>NOT</span><span>NULL</span><span>,
</span><span> 6</span>     typeid <span>int</span>(<span>10</span>) unsigned <span>NOT</span><span>NULL</span><span>,
</span><span> 7</span>     price <span>double</span>(<span>6</span>,<span>2</span>) unsigned <span>NOT</span><span>NULL</span><span>,
</span><span> 8</span>     total <span>int</span>(<span>10</span>) unsigned <span>NOT</span><span>NULL</span><span>,
</span><span> 9</span>     pic <span>varchar</span>(<span>32</span>) <span>NOT</span><span>NULL</span><span>,
</span><span>10</span>     note <span>text</span><span>,
</span><span>11</span>     addtime <span>int</span>(<span>10</span>) unsigned <span>NOT</span><span>NULL</span><span>,
</span><span>12</span><span>PRIMARY</span><span>KEY</span><span> (`id`)
</span><span>13</span> ) <span>//</span>创建表语句
로그인 후 복사
테이블 생성문

다음은 각 php 파일의 코드입니다. 각 코드를 직접 복사하여 동일한 디렉터리에 넣을 수 있습니다. 예, 업로드된 이미지를 저장하려면 동일한 디렉터리에 uplaods 폴더를 만들어야 합니다.

<span> 1</span> <html>
<span> 2</span>     <head>
<span> 3</span>         <title>商品信息管理</title>
<span> 4</span>     </head>
<span> 5</span>     <body>
<span> 6</span>         <center>
<span> 7</span>             <?php <span>include</span>("menu.php");<span>//</span><span>导入导航栏  ?><span> 8</span>             <h3>发布商品信息</h3>
<span> 9</span>             <form action="action.php?action=add" enctype="multipart/form-data" method="post">
<span>10</span>             <table border="0" width="300">
<span>11</span>                 <tr>
<span>12</span>                     <td align="right">名称:</td>
<span>13</span>                     <td><input type="text" name="name"/></td>
<span>14</span>                 </tr>
<span>15</span>                 <tr>
<span>16</span>                     <td align="right">类型:</td>
<span>17</span>                     <td>
<span>18</span>                         <select name="typeid">
<span>19</span>                         <?<span>php 
</span><span>20</span><span>include</span>("dbconfig.php"<span>);
</span><span>21</span><span>foreach</span>(<span>$typelist</span><span>as</span><span>$k</span>=><span>$v</span><span>)
</span><span>22</span><span>                            {
</span><span>23</span><span>echo</span> "<option value='{<span>$k</span>}'>{<span>$v</span>}</option>"<span>;
</span><span>24</span><span>                            }
</span><span>25</span>                         ?>
<span>26</span>                         </select>
<span>27</span>                     </td>
<span>28</span>                 </tr>
<span>29</span>                 <tr>
<span>30</span>                     <td align="right">单价:</td>
<span>31</span>                     <td><input type="text" name="price"/></td>
<span>32</span>                 </tr>
<span>33</span>                 <tr>
<span>34</span>                     <td align="right">库存:</td>
<span>35</span>                     <td><input type="text" name="total"/></td>
<span>36</span>                 </tr>
<span>37</span>                 <tr>
<span>38</span>                     <td align="right">图片:</td>
<span>39</span>                     <td><input type="file" name="pic"/></td>
<span>40</span>                 </tr>
<span>41</span>                 <tr>
<span>42</span>                     <td align="right" valign="top">描述:</td>
<span>43</span>                     <td><textarea rows="5" cols="20" name="note"></textarea></td>
<span>44</span>                 </tr>
<span>45</span>                 <tr>
<span>46</span>                     <td colspan="2" align="center">
<span>47</span>                         <input type="submit" value="添加"/> &<span>nbsp;
</span><span>48</span>                         <input type="reset" value="重置"/>
<span>49</span>                     </td>
<span>50</span>                 </tr>
<span>51</span>             </table>
<span>52</span>             </form>
<span>53</span>         </center>
<span>54</span>     </body>
<span>55</span> </html>
로그인 후 복사
add.php

<span>  1</span> <?<span>php
</span><span>  2</span><span>//</span><span>执行商品信息的增、删、改的操作
</span><span>  3</span><span>  4</span><span>//一、导入配置文件和函数库文件</span><span>  5</span><span>require</span>("dbconfig.php"<span>);
</span><span>  6</span><span>require</span>("function.php"<span>);
</span><span>  7</span><span>//</span><span>二、连接MySQL,选择数据库</span><span>  8</span><span>$link</span> = <span>mysql_connect</span>(HOST,USER,PASS) or <span>die</span>("数据库连接失败"<span>);
</span><span>  9</span><span>mysql_select_db</span>(DBNAME,<span>$link</span><span>);
</span><span> 10</span><span> 11</span><span> 12</span><span>//</span><span>三、获取action参数的值,并做对应的操作</span><span> 13</span><span>switch</span>(<span>$_GET</span>["action"<span>])
</span><span> 14</span><span>    {
</span><span> 15</span><span>case</span> "add": <span>//</span><span>添加
</span><span> 16</span><span>            //1.获取添加信息</span><span> 17</span><span>$name</span> = <span>$_POST</span>["name"<span>];
</span><span> 18</span><span>$typeid</span> = <span>$_POST</span>["typeid"<span>];
</span><span> 19</span><span>$price</span> = <span>$_POST</span>["price"<span>];
</span><span> 20</span><span>$total</span> = <span>$_POST</span>["total"<span>];
</span><span> 21</span><span>$note</span> = <span>$_POST</span>["note"<span>];
</span><span> 22</span><span>$addtime</span> = <span>time</span><span>();
</span><span> 23</span><span>//</span><span>2.验证()省略</span><span> 24</span><span>if</span>(<span>empty</span>(<span>$name</span><span>))
</span><span> 25</span><span>            {
</span><span> 26</span><span>die</span>("商品名称必须有值"<span>);
</span><span> 27</span><span>            }
</span><span> 28</span><span>//</span><span>3.执行图片上传</span><span> 29</span><span>$upinfo</span> = uploadFile("pic","./uploads/"<span>);
</span><span> 30</span><span>if</span>(<span>$upinfo</span>["error"]===<span>false</span><span>)
</span><span> 31</span><span>            {
</span><span> 32</span><span>die</span>("图片信息上传失败:".<span>$upinfo</span>["info"<span>]);
</span><span> 33</span>             }<span>else</span><span> 34</span><span>            {
</span><span> 35</span><span>//</span><span>上传成功</span><span> 36</span><span>$pic</span> = <span>$upinfo</span>["info"];<span>//</span><span>获取上传成功的图片名</span><span> 37</span><span> 38</span><span>            }
</span><span> 39</span><span>//</span><span>4.执行图片缩放</span><span> 40</span>             imageUpdateSize('./uploads/'.<span>$pic</span>,50,50<span>);
</span><span> 41</span><span>//</span><span>5.拼装sql语句,并执行添加</span><span> 42</span><span>$sql</span> = "insert into goods values(null,'{<span>$name</span>}','{<span>$typeid</span>}',{<span>$price</span>},{<span>$total</span>},'{<span>$pic</span>}','{<span>$note</span>}',{<span>$addtime</span>})"<span>;
</span><span> 43</span><span>mysql_query</span>(<span>$sql</span>,<span>$link</span><span>);
</span><span> 44</span><span>//</span><span>6.判断并输出结果</span><span> 45</span><span>if</span>(<span>mysql_insert_id</span>(<span>$link</span>)>0<span>)
</span><span> 46</span><span>            {
</span><span> 47</span><span>echo</span> "商品发布成功"<span>;
</span><span> 48</span>             }<span>else</span><span> 49</span><span>            {
</span><span> 50</span><span>echo</span> "商品发布失败"<span>;
</span><span> 51</span><span>            }
</span><span> 52</span><span>echo</span> "<br> <a href='index.php'>查看商品信息<a>"<span>;
</span><span> 53</span><span> 54</span><span>break</span><span>;
</span><span> 55</span><span>case</span> "del": <span>//</span><span>删除
</span><span> 56</span><span>            //获取要删除的id号并拼装删除sql,执行</span><span> 57</span><span>$sql</span> = "delete from goods where id={<span>$_GET</span>['id']}"<span>;
</span><span> 58</span><span> 59</span><span>mysql_query</span>(<span>$sql</span>,<span>$link</span><span>);
</span><span> 60</span><span>//</span><span>执行图片删除</span><span> 61</span><span>if</span>(<span>mysql_affected_rows</span>(<span>$link</span>)>0<span>)
</span><span> 62</span><span>            {
</span><span> 63</span>                 @<span>unlink</span>("./uploads/".<span>$_GET</span>['picname'<span>]);
</span><span> 64</span>                 @<span>unlink</span>("./uploads/s_".<span>$_GET</span>['picname'<span>]);
</span><span> 65</span><span>            }
</span><span> 66</span><span>//</span><span>跳转到浏览界面</span><span> 67</span><span>header</span>("Location:index.php"<span>);
</span><span> 68</span><span>break</span><span>;
</span><span> 69</span><span> 70</span><span>case</span> "update":  <span>//</span><span>修改
</span><span> 71</span><span>            //1.获取要修改的信息</span><span> 72</span><span>$name</span> = <span>$_POST</span>["name"<span>];
</span><span> 73</span><span>$typeid</span> = <span>$_POST</span>["typeid"<span>];
</span><span> 74</span><span>$price</span> = <span>$_POST</span>["price"<span>];
</span><span> 75</span><span>$total</span> = <span>$_POST</span>["total"<span>];
</span><span> 76</span><span>$note</span> = <span>$_POST</span>["note"<span>];
</span><span> 77</span><span>$id</span> = <span>$_POST</span>['id'<span>];
</span><span> 78</span><span>$pic</span> = <span>$_POST</span>['oldpic'<span>];
</span><span> 79</span><span>//</span><span>2.数据验证</span><span> 80</span><span>if</span>(<span>empty</span>(<span>$name</span><span>))
</span><span> 81</span><span>            {
</span><span> 82</span><span>die</span>("商品名称必须有值"<span>);
</span><span> 83</span><span>            }
</span><span> 84</span><span>//</span><span>3.判断有无图片上传</span><span> 85</span><span>if</span>(<span>$_FILES</span>['pic']['error']!=4<span>)
</span><span> 86</span><span>            {
</span><span> 87</span><span>//</span><span>执行上传</span><span> 88</span><span>$upinfo</span> = uploadFile("pic","./uploads/"<span>);
</span><span> 89</span><span>if</span>(<span>$upinfo</span>["error"]===<span>false</span><span>)
</span><span> 90</span><span>                {
</span><span> 91</span><span>die</span>("图片信息上传失败:".<span>$upinfo</span>["info"<span>]);
</span><span> 92</span>                 }<span>else</span><span> 93</span><span>                {
</span><span> 94</span><span>//</span><span>上传成功</span><span> 95</span><span>$pic</span> = <span>$upinfo</span>["info"];<span>//</span><span>获取上传成功的图片名
</span><span> 96</span><span>                    //4.有图片上传执行缩放</span><span> 97</span>                     imageUpdateSize('./uploads/'.<span>$pic</span>,50,50<span>);
</span><span> 98</span><span>                }
</span><span> 99</span><span>            }
</span><span>100</span><span>101</span><span>102</span><span>//</span><span>5.执行修改</span><span>103</span><span>$sql</span> = "update goods set name='{<span>$name</span>}',typeid={<span>$typeid</span>},price={<span>$price</span>},total={<span>$total</span>},note='{<span>$note</span>}',pic='{<span>$pic</span>}' where id={<span>$id</span>}"<span>;
</span><span>104</span><span>mysql_query</span>(<span>$sql</span>,<span>$link</span><span>);
</span><span>105</span><span>//</span><span>6.判断是否修改成功</span><span>106</span><span>if</span>(<span>mysql_affected_rows</span>(<span>$link</span>)>0<span>)
</span><span>107</span><span>            {
</span><span>108</span><span>if</span>(<span>$_FILES</span>['pic']['error']!=4<span>)
</span><span>109</span><span>                {
</span><span>110</span><span>//</span><span>若有图片上传,就删除老图片</span><span>111</span>                     @<span>unlink</span>("./uploads/".<span>$_POST</span>['oldpic'<span>]);
</span><span>112</span>                     @<span>unlink</span>("./uploads/s_".<span>$_POST</span>['oldpic'<span>]);
</span><span>113</span><span>                }
</span><span>114</span><span>echo</span> "修改成功"<span>;
</span><span>115</span>             }<span>else</span><span>116</span><span>            {
</span><span>117</span><span>echo</span> "修改失败".<span>mysql_error</span><span>();
</span><span>118</span><span>            }
</span><span>119</span><span>echo</span> "<br> <a href='index.php'>查看商品信息<a>"<span>;
</span><span>120</span><span>break</span><span>;
</span><span>121</span><span>default</span>:
<span>122</span><span>echo</span> "错误";<span>break</span><span>;
</span><span>123</span><span>124</span><span>    }
</span><span>125</span><span>//</span><span>四、关闭数据库</span><span>126</span><span>mysql_close</span>(<span>$link</span>);
로그인 후 복사
action.php

<span> 1</span> <?<span>php
</span><span> 2</span><span>//</span><span>公共信息配置文件
</span><span> 3</span><span> 4</span><span>//数据库信息配置</span><span> 5</span><span>define</span>("HOST","localhost");<span>//</span><span>主机名</span><span> 6</span><span>define</span>("USER","root");     <span>//</span><span>用户名</span><span> 7</span><span>define</span>("PASS","root");     <span>//</span><span>密码</span><span> 8</span><span>define</span>("DBNAME","demodb"); <span>//</span><span>数据库名
</span><span> 9</span><span>10</span><span>//商品类型列表信息</span><span>11</span><span>$typelist</span>=<span>array</span>(1=>"服装",2=>"数码",3=>"食品"<span>);
</span><span>12</span><span>13</span><span>14</span> ?>
로그인 후 복사
dbconfig .php

<span> 1</span> <html>
<span> 2</span>     <head>
<span> 3</span>         <title>商品信息管理</title>
<span> 4</span>     </head>
<span> 5</span>     <body>
<span> 6</span>         <center>
<span> 7</span>             <?php <span>include</span>("menu.php");<span>//</span><span>导入导航栏  ?><span> 8</span>             <h3>浏览商品信息<h3>
<span> 9</span><span>10</span>             <table border="1" width="700">
<span>11</span>                 <tr>
<span>12</span>                     <th>商品编号</th>
<span>13</span>                     <th>商品名称</th>
<span>14</span>                     <th>商品图片</th>
<span>15</span>                     <th>单价</th>
<span>16</span>                     <th>库存量</th>
<span>17</span>                     <th>添加时间</th>
<span>18</span>                     <th>操作</th>
<span>19</span>                 </tr>
<span>20</span>                 <?<span>php 
</span><span>21</span><span>//</span><span>从数据库中读取信息并输出到浏览器表格中
</span><span>22</span><span>                //1.导入配置文件</span><span>23</span><span>require</span>("dbconfig.php"<span>);
</span><span>24</span><span>//</span><span>2.连接数据库,并选择数据库</span><span>25</span><span>$link</span> = @<span>mysql_connect</span>(HOST,USER,PASS) or <span>die</span>("数据库连接失败"<span>);
</span><span>26</span><span>mysql_select_db</span>(DBNAME,<span>$link</span><span>);
</span><span>27</span><span>//</span><span>3.执行商品信息查询</span><span>28</span><span>$sql</span>="select * from goods"<span>;
</span><span>29</span><span>$result</span> = <span>mysql_query</span>(<span>$sql</span>,<span>$link</span><span>);
</span><span>30</span><span>31</span><span>//</span><span>4.解析商品信息(解析结果集)</span><span>32</span><span>while</span>(<span>$row</span> = <span>mysql_fetch_assoc</span>(<span>$result</span><span>))
</span><span>33</span><span>                    {
</span><span>34</span><span>echo</span> "<tr>"<span>;
</span><span>35</span><span>echo</span> "<td>{<span>$row</span>["id"]}</td>"<span>;
</span><span>36</span><span>echo</span> "<td>{<span>$row</span>["name"]}</td>"<span>;
</span><span>37</span><span>echo</span> "<td><img src='./uploads/s_{<span>$row</span>['pic']}'/></td>"<span>;
</span><span>38</span><span>echo</span> "<td>{<span>$row</span>["price"]}</td>"<span>;
</span><span>39</span><span>echo</span> "<td>{<span>$row</span>["total"]}</td>"<span>;
</span><span>40</span><span>echo</span> "<td>".<span>date</span>("Y-m-d H:i:s",<span>$row</span>['addtime'])."</td>"<span>;
</span><span>41</span><span>echo</span> "<span><td>
</span><span>42</span>                                 <a href='action.php?action=del&id={<span>$row</span>['id']}&picname={<span>$row</span><span>['pic']}'>删除</a>  
<span>43</span>                                 <a href='edit.php?id={<span>$row</span><span>['id']}'>修改</a>  
<span>44</span>                                 <a href='addCart.php?id={<span>$row</span><span>['id']}'>放入购物车</a>  
<span>45</span><span>46</span>                               </td>"<span>;
</span><span>47</span><span>echo</span> "</tr>"<span>;
</span><span>48</span><span>                    }
</span><span>49</span><span>//</span><span>5.释放结果集,关闭数据库</span><span>50</span>                 ?>
<span>51</span>             </table>
<span>52</span>         </center>
<span>53</span>     </body>
<span>54</span> </html>
로그인 후 복사
index.php

<span> 1</span> <html>
<span> 2</span>     <head>
<span> 3</span>         <title>商品信息管理</title>
<span> 4</span>     </head>
<span> 5</span>     <body>
<span> 6</span>         <center>
<span> 7</span>             <?<span>php 
</span><span> 8</span><span>include</span>("menu.php");<span>//</span><span>导入导航栏  
</span><span> 9</span><span>                //1.导入配置文件</span><span>10</span><span>require</span>("dbconfig.php"<span>);
</span><span>11</span><span>//</span><span>2.连接数据库,并选择数据库</span><span>12</span><span>$link</span> = @<span>mysql_connect</span>(HOST,USER,PASS) or <span>die</span>("数据库连接失败"<span>);
</span><span>13</span><span>mysql_select_db</span>(DBNAME,<span>$link</span><span>);
</span><span>14</span><span>//</span><span>3.获取要修改的商品信息</span><span>15</span><span>$sql</span>="select *from goods where id={<span>$_GET</span>['id']}"<span>;
</span><span>16</span><span>$result</span> = <span>mysql_query</span>(<span>$sql</span>,<span>$link</span><span>);
</span><span>17</span><span>//</span><span>4.判断是否获取到要编辑的商品信息</span><span>18</span><span>if</span>(<span>$result</span>&&<span>mysql_num_rows</span>(<span>$result</span>)>0<span>)
</span><span>19</span><span>                {
</span><span>20</span><span>$shop</span>=<span>mysql_fetch_assoc</span>(<span>$result</span>);<span>//</span><span>解析出要修改的商品信息</span><span>21</span>                 }<span>else</span><span>22</span><span>                {
</span><span>23</span><span>die</span>("没有找到要修改的商品信息"<span>);
</span><span>24</span><span>                }
</span><span>25</span><span>26</span>             ?>
<span>27</span>             <h3>编辑商品信息</h3>
<span>28</span>             <form action="action.php?action=update" enctype="multipart/form-data" method="post">
<span>29</span>                 <input type="hidden" name="id" value="<?php echo <span>$shop</span>['id']; ?>" />
<span>30</span>                 <input type="hidden" name="oldpic" value="<?php echo <span>$shop</span>['pic']; ?>" />
<span>31</span><span>32</span>             <table border="0" width="300">
<span>33</span>                 <tr>
<span>34</span>                     <td align="right">名称:</td>
<span>35</span>                     <td><input type="text" name="name" value="<?php echo <span>$shop</span>['name']; ?>" /></td>
<span>36</span>                 </tr>
<span>37</span>                 <tr>
<span>38</span>                     <td align="right">类型:</td>
<span>39</span>                     <td>
<span>40</span>                         <select name="typeid">
<span>41</span>                         <?<span>php 
</span><span>42</span><span>include</span>("dbconfig.php"<span>);
</span><span>43</span><span>foreach</span>(<span>$typelist</span><span>as</span><span>$k</span>=><span>$v</span><span>)
</span><span>44</span><span>                            {
</span><span>45</span><span>$sd</span> = (<span>$shop</span>['typeid']==<span>$k</span>)?"selected":"";<span>//</span><span>判断是否当前的类型</span><span>46</span><span>echo</span> "<option value='{<span>$k</span>}' {<span>$sd</span>}>{<span>$v</span>}</option>"<span>;
</span><span>47</span><span>                            }
</span><span>48</span>                         ?>
<span>49</span>                         </select>
<span>50</span>                     </td>
<span>51</span>                 </tr>
<span>52</span>                 <tr>
<span>53</span>                     <td align="right">单价:</td>
<span>54</span>                     <td><input type="text" name="price" value="<?php echo <span>$shop</span>['price']; ?>" /></td>
<span>55</span>                 </tr>
<span>56</span>                 <tr>
<span>57</span>                     <td align="right">库存:</td>
<span>58</span>                     <td><input type="text" name="total" value="<?php echo <span>$shop</span>['total']; ?>" /></td>
<span>59</span>                 </tr>
<span>60</span>                 <tr>
<span>61</span>                     <td align="right">图片:</td>
<span>62</span>                     <td><input type="file" name="pic"/></td>
<span>63</span>                 </tr>
<span>64</span>                 <tr>
<span>65</span>                     <td align="right" valign="top">描述:</td>
<span>66</span>                     <td><textarea rows="5" cols="20" name="note"><?php <span>echo</span><span>$shop</span>['note']; ?></textarea></td>
<span>67</span>                 </tr>
<span>68</span><span>69</span>                 <tr>
<span>70</span>                     <td colspan="2" align="center">
<span>71</span>                         <input type="submit" value="修改" /> &<span>nbsp;
</span><span>72</span>                         <input type="reset" value="重置" />
<span>73</span>                     </td>
<span>74</span>                 </tr>
<span>75</span>                 <tr>
<span>76</span>                     <td align="right" valign="top"> </td>
<span>77</span>                     <td><img src="./uploads/<?php echo <span>$shop</span>['pic'];?>" /></td>
<span>78</span>                 </tr>
<span>79</span>             </table>
<span>80</span>             </form>
<span>81</span>         </center>
<span>82</span>     </body>
<span>83</span> </html>
로그인 후 복사
edit.php

<span>  1</span> <?<span>php
</span><span>  2</span><span>//</span><span>公共函数库</span><span>  3</span><span>  4</span><span>/*</span><span>  5</span><span> * 文件上传处理函数
</span><span>  6</span><span> * @param string filename 要上传的文件表单项名
</span><span>  7</span><span> * @param string $path  上传文件的保存路径
</span><span>  8</span><span> * @param array     允许的文件类型
</span><span>  9</span><span> * @return array    两个单元: ["error"] false:失败,ture:成功
</span><span> 10</span><span> *                          ["info"] 存放失败原因或成功的文件名
</span><span> 11</span><span>*/</span><span> 12</span><span> 13</span><span>function</span> uploadFile(<span>$filename</span>,<span>$path</span>,<span>$typelist</span>=<span>null</span><span>)
</span><span> 14</span><span>{
</span><span> 15</span><span>//</span><span>1.获取上传文件的名字</span><span> 16</span><span>$upfile</span> = <span>$_FILES</span>[<span>$filename</span><span>];
</span><span> 17</span><span>if</span>(<span>empty</span>(<span>$typelist</span><span>))
</span><span> 18</span><span>    {
</span><span> 19</span><span>$typelist</span>=<span>array</span>("image/gif","image/jpg","image/jpeg","image/png","image/pjpeg","image/x-png");<span>//</span><span>允许的文件类型</span><span> 20</span><span>    }
</span><span> 21</span><span>$res</span>=<span>array</span>("error"=><span>false</span>);<span>//</span><span>存放返回的结果
</span><span> 22</span><span>    //2.过滤上传文件的错误号</span><span> 23</span><span>if</span>(<span>$upfile</span>["error"]>0<span>)
</span><span> 24</span><span>    {
</span><span> 25</span><span>switch</span>(<span>$upfile</span>["error"<span>])
</span><span> 26</span><span>        {
</span><span> 27</span><span>case</span> 1:
<span> 28</span><span>$res</span>["info"]="上传的文件超过了 php.ini中upload_max_filesize选项大小"<span>;
</span><span> 29</span><span>break</span><span>;
</span><span> 30</span><span>case</span> 2:
<span> 31</span><span>$res</span>["info"]="上传文件的大小超过了HTML表单中MAX_FILE_SIZE选项"<span>;
</span><span> 32</span><span>break</span><span>;
</span><span> 33</span><span>case</span> 3:
<span> 34</span><span>$res</span>["info"]="文件只有部分被上传"<span>;
</span><span> 35</span><span>break</span><span>;
</span><span> 36</span><span>case</span> 4:
<span> 37</span><span>$res</span>["info"]="没有文件被上传"<span>;
</span><span> 38</span><span>break</span><span>;
</span><span> 39</span><span>case</span> 6:
<span> 40</span><span>$res</span>["info"]="找不到临时文件夹"<span>;
</span><span> 41</span><span>break</span><span>;
</span><span> 42</span><span>case</span> 7:
<span> 43</span><span>$res</span>["info"]="文件写入失败"<span>;
</span><span> 44</span><span>break</span><span>;
</span><span> 45</span><span>default</span>:
<span> 46</span><span>$res</span>["info"]="未知错误!"<span>;
</span><span> 47</span><span>break</span><span>;
</span><span> 48</span><span> 49</span><span>        }
</span><span> 50</span><span>return</span><span>$res</span><span>;
</span><span> 51</span><span>    }
</span><span> 52</span><span>//</span><span>3.本次文件大小的限制</span><span> 53</span><span>if</span>(<span>$upfile</span>["size"]>1000000<span>)
</span><span> 54</span><span>    {
</span><span> 55</span><span>$res</span>["info"]="上传文件过大!"<span>;
</span><span> 56</span><span>return</span><span>$res</span><span>;
</span><span> 57</span><span>    }
</span><span> 58</span><span>//</span><span>4.过滤类型</span><span> 59</span><span>if</span>(!<span>in_array</span>(<span>$upfile</span>["type"],<span>$typelist</span><span>))
</span><span> 60</span><span>    {
</span><span> 61</span><span>$res</span>["info"]="上传类型不符!".<span>$upfile</span>["type"<span>];
</span><span> 62</span><span>return</span><span>$res</span><span>;
</span><span> 63</span><span>    }
</span><span> 64</span><span>//</span><span>5.初始化下信息(为图片产生一个随机的名字)</span><span> 65</span><span>$fileinfo</span> = <span>pathinfo</span>(<span>$upfile</span>["name"<span>]);
</span><span> 66</span><span>do</span><span> 67</span><span>    {
</span><span> 68</span><span>$newfile</span> = <span>date</span>("YmdHis").<span>rand</span>(1000,9999).".".<span>$fileinfo</span>["extension"];<span>//</span><span>随机产生名字</span><span> 69</span><span> 70</span>     }<span>while</span>(<span>file_exists</span>(<span>$newfile</span><span>));
</span><span> 71</span><span>//</span><span>6.执行上传处理</span><span> 72</span><span>if</span>(<span>is_uploaded_file</span>(<span>$upfile</span>["tmp_name"<span>]))
</span><span> 73</span><span>    {
</span><span> 74</span><span>if</span>(<span>move_uploaded_file</span>(<span>$upfile</span>["tmp_name"],<span>$path</span>."/".<span>$newfile</span><span>))
</span><span> 75</span><span>        {
</span><span> 76</span><span>//</span><span>将上传成功后的文件名赋给返回数组</span><span> 77</span><span>$res</span>["info"]=<span>$newfile</span><span>;
</span><span> 78</span><span>$res</span>["error"]=<span>true</span><span>;
</span><span> 79</span><span>return</span><span>$res</span><span>;
</span><span> 80</span>         }<span>else</span><span> 81</span><span>        {
</span><span> 82</span><span>$res</span>["info"]="上传文件失败!"<span>;
</span><span> 83</span><span>        }
</span><span> 84</span>     }<span>else</span><span> 85</span><span>    {
</span><span> 86</span><span>$res</span>["info"]="不是一个上传的文件"<span>;
</span><span> 87</span><span>    }
</span><span> 88</span><span>return</span><span>$res</span><span>;
</span><span> 89</span><span>}
</span><span> 90</span><span>//</span><span>==================================================</span><span> 91</span><span>/*</span><span> 92</span><span> * 
</span><span> 93</span><span> * 等比缩放函数(以保存的方式实现)
</span><span> 94</span><span> * @param string $picname 被缩放的处理图片源
</span><span> 95</span><span> * @param int $maxx 缩放后的图片的最大宽度
</span><span> 96</span><span> * @param int $maxy 缩放后图片的最大高度
</span><span> 97</span><span> * @param string $pre 缩放后图片名的前缀名
</span><span> 98</span><span> * @param string  返回后的图片名称(带路径),如a.jpg=>s_a.jpg
</span><span> 99</span><span>*/</span><span>100</span><span>function</span> imageUpdateSize(<span>$picname</span>,<span>$maxx</span>=100,<span>$maxy</span>=100,<span>$pre</span>="s_"<span>){
</span><span>101</span><span>$info</span>=<span>getimagesize</span>(<span>$picname</span>); <span>//</span><span>获取图片的基本信息</span><span>102</span><span>$w</span> = <span>$info</span>[0];<span>//</span><span>获取宽度</span><span>103</span><span>$h</span> = <span>$info</span>[1]; <span>//</span><span> 获取高度</span><span>104</span><span>switch</span>(<span>$info</span>[2<span>]){
</span><span>105</span><span>case</span> 1: <span>//</span><span>gif</span><span>106</span><span>$im</span>=imagecreatefromgif(<span>$picname</span><span>);
</span><span>107</span><span>break</span><span>;
</span><span>108</span><span>case</span> 2: <span>//</span><span>jpg</span><span>109</span><span>$im</span>=imagecreatefromjpeg(<span>$picname</span><span>);
</span><span>110</span><span>break</span><span>;
</span><span>111</span><span>case</span> 3: <span>//</span><span>png</span><span>112</span><span>$im</span>=imagecreatefrompng(<span>$picname</span><span>);
</span><span>113</span><span>break</span><span>;
</span><span>114</span><span>default</span> :
<span>115</span><span>die</span>("图片类型错误"<span>);
</span><span>116</span><span>    }
</span><span>117</span><span>//</span><span>计算缩放比例</span><span>118</span><span>if</span>((<span>$maxx</span>/<span>$w</span>)>(<span>$maxy</span>/<span>$h</span><span>)){
</span><span>119</span><span>$b</span>=<span>$maxy</span>/<span>$h</span><span>;
</span><span>120</span>     }<span>else</span><span>{
</span><span>121</span><span>$b</span>=<span>$maxx</span>/<span>$w</span><span>;
</span><span>122</span><span>    }
</span><span>123</span><span>//</span><span>计算缩放后的尺寸</span><span>124</span><span>$nw</span>=<span>floor</span>(<span>$w</span>*<span>$b</span><span>);
</span><span>125</span><span>$nh</span>=<span>floor</span>(<span>$h</span>*<span>$b</span><span>);
</span><span>126</span><span>//</span><span>创建一个新的图像源</span><span>127</span><span>$nim</span>=imagecreatetruecolor(<span>$nw</span>,<span>$nh</span><span>);
</span><span>128</span><span>//</span><span>执行等比缩放</span><span>129</span>     imagecopyresampled(<span>$nim</span>,<span>$im</span>,0,0,0,0,<span>$nw</span>,<span>$nh</span>,<span>$w</span>,<span>$h</span><span>);
</span><span>130</span><span>//</span><span>输出图像</span><span>131</span><span>$picinfo</span>=<span>pathinfo</span>(<span>$picname</span><span>);
</span><span>132</span><span>$newpicname</span>=<span>$picinfo</span>["dirname"]."/".<span>$pre</span>.<span>$picinfo</span>["basename"<span>];
</span><span>133</span><span>134</span><span>switch</span>(<span>$info</span>[2<span>]){
</span><span>135</span><span>case</span> 1:
<span>136</span>             imagegif(<span>$nim</span>,<span>$newpicname</span><span>);
</span><span>137</span><span>break</span><span>;
</span><span>138</span><span>case</span> 2:
<span>139</span>             imagejpeg(<span>$nim</span>,<span>$newpicname</span><span>);
</span><span>140</span><span>break</span><span>;
</span><span>141</span><span>case</span> 3:
<span>142</span>             imagepng(<span>$nim</span>,<span>$newpicname</span><span>);
</span><span>143</span><span>break</span><span>;
</span><span>144</span><span>default</span>:
<span>145</span><span>echo</span> "图片压缩错误"<span>;
</span><span>146</span><span>    }
</span><span>147</span><span>//</span><span>释放图片资源</span><span>148</span>     imagedestroy(<span>$im</span><span>);
</span><span>149</span>     imagedestroy(<span>$nim</span><span>);
</span><span>150</span><span>//</span><span>返回结果</span><span>151</span><span>return</span><span>$newpicname</span><span>;
</span><span>152</span> }
로그인 후 복사
function.php

<span>1</span>     <h2>商品信息管理--购物车</h2>
<span>2</span>     <a href="index.php">浏览商品</a>|
<span>3</span>     <a href="add.php">添加商品</a>|
<span>4</span><span>5</span>     <a href="myCart.php">我的购物车</a>|
<span>6</span>     <a href="clearCart.php">清空购物车</a>
<span>7</span><span>8</span><span>9</span>     <hr width=80%/>
로그인 후 복사
menu.php

<span> 1</span> <?<span>php 
</span><span> 2</span><span>session_start</span>();<span>//</span><span>启动会话</span><span> 3</span><span> 4</span> ?>
<span> 5</span> <html>
<span> 6</span>     <head>
<span> 7</span>         <title>商品信息管理</title>
<span> 8</span>     </head>
<span> 9</span>     <body>
<span>10</span>         <center>
<span>11</span>             <?php <span>include</span>("menu.php");<span>//</span><span>导入导航栏  ?><span>12</span>             <h3>添加商品到购物车<h3>
<span>13</span><span>14</span>                 <?<span>php 
</span><span>15</span><span>//</span><span>从数据库中读取要购买的信息并添加到购物车中
</span><span>16</span><span>                //1.导入配置文件</span><span>17</span><span>require</span>("dbconfig.php"<span>);
</span><span>18</span><span>//</span><span>2.连接数据库,并选择数据库</span><span>19</span><span>$link</span> = @<span>mysql_connect</span>(HOST,USER,PASS) or <span>die</span>("数据库连接失败"<span>);
</span><span>20</span><span>mysql_select_db</span>(DBNAME,<span>$link</span><span>);
</span><span>21</span><span>//</span><span>3.执行商品信息查询(获取要购买的信息)</span><span>22</span><span>$sql</span>="select * from goods where id={<span>$_GET</span>['id']}"<span>;
</span><span>23</span><span>$result</span> = <span>mysql_query</span>(<span>$sql</span>,<span>$link</span><span>);
</span><span>24</span><span>25</span><span>//</span><span>4.判断是否没有找到要购买的信息,若有就读取出要购买的信息</span><span>26</span><span>if</span>(<span>empty</span>(<span>$result</span>) || <span>mysql_num_rows</span>(<span>$result</span>)==0<span>)
</span><span>27</span><span>                    {
</span><span>28</span><span>die</span>("没有找到要购买的信息!"<span>);
</span><span>29</span>                     }<span>else</span><span>30</span><span>                    {
</span><span>31</span><span>$shop</span> = <span>mysql_fetch_assoc</span>(<span>$result</span><span>);
</span><span>32</span><span>                    }
</span><span>33</span><span>$shop</span>["num"]=1;<span>//</span><span>添加一个数量的字段
</span><span>34</span><span>                //5.放入购物车中(若已存在的商品实现数量累加)</span><span>35</span><span>if</span>(<span>isset</span>(<span>$_SESSION</span>["shoplist"]{<span>$shop</span>['id'<span>]}))
</span><span>36</span><span>                    {
</span><span>37</span><span>//</span><span>若存在数量增加1</span><span>38</span><span>$_SESSION</span>["shoplist"][<span>$shop</span>['id']]["num"]++<span>;
</span><span>39</span>                     }<span>else</span><span>40</span><span>                    {
</span><span>41</span><span>//</span><span>若不存在,作为新购买的商品添加到购物车中</span><span>42</span><span>$_SESSION</span>["shoplist"][<span>$shop</span>['id']]=<span>$shop</span><span>;
</span><span>43</span><span>                    }
</span><span>44</span><span>45</span>                 ?>
<span>46</span>             </table>
<span>47</span>         </center>
<span>48</span>     </body>
<span>49</span> </html>
로그인 후 복사
addCart

<span> 1</span> <?<span>php 
</span><span> 2</span><span>session_start</span>();<span>//</span><span>启动会话</span><span> 3</span><span> 4</span> ?>
<span> 5</span> <html>
<span> 6</span>     <head>
<span> 7</span>         <title>商品信息管理</title>
<span> 8</span>     </head>
<span> 9</span>     <body>
<span>10</span>         <center>
<span>11</span>             <?php <span>include</span>("menu.php");<span>//</span><span>导入导航栏  ?><span>12</span>             <h3>浏览我的购物车<h3>
<span>13</span>             <table border="1" width="600">
<span>14</span>                 <tr>
<span>15</span>                     <th>商品id号</th>
<span>16</span>                     <th>商品名称</th>
<span>17</span>                     <th>商品图片</th>
<span>18</span>                     <th>单价</th>
<span>19</span>                     <th>数量</th>
<span>20</span>                     <th>小计</th>
<span>21</span>                     <th>操作</th>
<span>22</span>                 </tr>
<span>23</span>                 <?<span>php 
</span><span>24</span><span>$sum</span> =0;<span>//</span><span>定义总金额的变量</span><span>25</span><span>if</span>(<span>isset</span>(<span>$_SESSION</span>["shoplist"<span>])){
</span><span>26</span><span>foreach</span>(<span>$_SESSION</span>["shoplist"] <span>as</span><span>$v</span><span>)
</span><span>27</span><span>                        {
</span><span>28</span><span>echo</span> "<tr>"<span>;
</span><span>29</span><span>echo</span> "<td>{<span>$v</span>['id']}</td>"<span>;
</span><span>30</span><span>echo</span> "<td>{<span>$v</span>['name']}</td>"<span>;
</span><span>31</span><span>echo</span> "<td><img src='./uploads/s_{<span>$v</span>['pic']}' /></td>"<span>;
</span><span>32</span><span>echo</span> "<td>{<span>$v</span>['price']}</td>"<span>;
</span><span>33</span><span>echo</span> "<span><td align='center'>
</span><span>34</span>                                     <button >$v</span><span>['id']}&num=-1\"'>-</button>
<span>35</span>                                     {<span>$v</span><span>['num']}
</span><span>36</span>                                     <button >$v</span><span>['id']}&num=+1\"'>+</button>
<span>37</span>                                     </td>"<span>;
</span><span>38</span><span>echo</span> "<td>".(<span>$v</span>["price"]*<span>$v</span>['num'])."</td>"<span>;                        
</span><span>39</span><span>echo</span> "<td><a href='clearCart.php?id={<span>$v</span>['id']}'>删除</a></td>"<span>;
</span><span>40</span><span>echo</span> "</tr>"<span>;
</span><span>41</span><span>$sum</span>+=<span>$v</span>["price"]*<span>$v</span>['num'];<span>//</span><span>累计金额</span><span>42</span><span>                        }
</span><span>43</span><span>                    }
</span><span>44</span>                 ?>
<span>45</span>                 <tr>
<span>46</span>                     <th>总计金额:</th>
<span>47</span>                     <th colspan ="5" align="right"><?php <span>echo</span><span>$sum</span>; ?></th>
<span>48</span>                     <td> </td>
<span>49</span>                 </tr>
<span>50</span>             </table>
<span>51</span>         </center>
<span>52</span>     </body>
<span>53</span> </html>
로그인 후 복사
myCart.php

<span> 1</span> <?<span>php
</span><span> 2</span><span> 3</span><span>//</span><span>删除购物车session中的信息</span><span> 4</span><span>session_start</span>();<span>//</span><span>启动会话
</span><span> 5</span><span> 6</span><span>    //判断是删除一个商品还是清空购物车</span><span> 7</span><span>if</span>(<span>$_GET</span>['id'<span>])
</span><span> 8</span><span>    {
</span><span> 9</span><span>//</span><span>只删除一种商品</span><span>10</span><span>unset</span>(<span>$_SESSION</span>['shoplist'][<span>$_GET</span>['id'<span>]]);
</span><span>11</span>     }<span>else</span><span>12</span><span>    {
</span><span>13</span><span>//</span><span>清空session中的商品</span><span>14</span><span>unset</span>(<span>$_SESSION</span>["shoplist"<span>]);
</span><span>15</span><span>    }
</span><span>16</span><span>17</span><span>18</span><span>//</span><span>跳转到浏览购物车界面</span><span>19</span><span>header</span>("Location:myCart.php"<span>);
</span><span>20</span> ?>
로그인 후 복사
clearCart.php

<span> 1</span> <?<span>php
</span><span> 2</span><span>session_start</span>();<span>//</span><span>启动会话
</span><span> 3</span><span>    //修改购物车中的信息
</span><span> 4</span><span> 5</span><span>    //获取要修改的信息</span><span> 6</span><span> 7</span><span>$id</span> = <span>$_GET</span>['id'<span>];
</span><span> 8</span><span>$num</span> = <span>$_GET</span>['num'<span>];
</span><span> 9</span><span>10</span><span>//</span><span>修改商品信息</span><span>11</span><span>$_SESSION</span>["shoplist"][<span>$id</span>]["num"]+=<span>$num</span><span>;
</span><span>12</span><span>13</span><span>//</span><span>防止商品数量过小</span><span>14</span><span>if</span>(<span>$_SESSION</span>["shoplist"][<span>$id</span>]["num"]<1<span>)
</span><span>15</span><span>    {
</span><span>16</span><span>$_SESSION</span>["shoplist"][<span>$id</span>]["num"]=1<span>;
</span><span>17</span><span>    }
</span><span>18</span><span>//</span><span>跳转回我的购物车界面</span><span>19</span><span>header</span>("Location:myCart.php"<span>);
</span><span>20</span><span>21</span> ?>
로그인 후 복사
updateCart.php

다음 index.php 스크린샷:

myCart.php 스크린샷:

마지막으로 한마디 하고 싶습니다. 하하하하하하하하하하하하하! ! ! !

위 내용은 PHP: Product Information Management System v11의 기본 예제와 관련 내용을 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿