首頁 >後端開發 >php教程 >PHP基礎範例:商品資訊管理系統v11

PHP基礎範例:商品資訊管理系統v11

WBOY
WBOY原創
2016-08-08 09:23:413596瀏覽

實現目標:使用php和mysql寫一個商品資訊管理系統,並帶有購物車功能

一、建立資料庫和表格

  1.建立資料庫和表格:demodb

  2.建立表格:goods🎀 :商品編號,商品名稱,商品類型,商品圖片,單價,商品描述,庫存量,新增時間

二、建立php檔案編寫程式碼(以下為要建立的php檔案及其用途)

  add.php  商品新增頁面

  edit.php      商品資訊編輯表單頁

  index.php  商品資訊瀏覽頁

  配置資訊

  menu.php  網站公共導航欄

  uploads/    網站公共導航列

  uploads/   〔網站上存放目錄的存放目錄

  function.php 公共資訊庫檔案的存放目錄

  function.php 公共資訊庫檔案的存放目錄

  function.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>);

<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> ?>
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>             <?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>
edit.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>

<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> }
addCart

<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%/>
myCart.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>
clearCart.php

以下為index.php截圖:

myCart .php截圖:

最後送上一句:哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈! ! ! !

以上就介紹了PHP基礎範例:商品資訊管理系統v11,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn