ホームページ >バックエンド開発 >PHPチュートリアル >PHP を使用してショッピング商品を追加する、PHP を使用してショッピング商品を追加する_PHP チュートリアル
<span><span> 1</span> <?<span>php </span><span> 2</span> <span>session_start</span><span>(); </span><span> 3</span> <span>header</span> ( "Content-type: text/html; charset=UTF-8" ); <span>//</span><span>设置文件编码格式</span> <span> 4</span> <span>require</span>("system/system.inc.php"); <span>//</span><span>包含配置文件</span> <span> 5</span> <span>/*</span><span>* </span><span> 6</span> <span> * 1表示添加成功 </span><span> 7</span> <span> * 2表示用户没有登录 </span><span> 8</span> <span> * 3表示商品已添加过 </span><span> 9</span> <span> * 4表示添加时出现错误 </span><span>10</span> <span> * 5表示没有商品添加 </span><span>11</span> <span>*/</span> <span>12</span> <span>$reback</span> = '0'<span>; </span><span>13</span> <span>if</span>(<span>empty</span>(<span>$_SESSION</span>['member'])){ <span>//</span><span>判断用户有没有登录</span> <span>14</span> <span>$reback</span> = '2'; <span>//</span><span>没有就返回2</span> <span>15</span> }<span>else</span><span>{ </span><span>16</span> <span>$key</span> = <span>$_GET</span>['key']; <span>//</span><span>判断用户有没有添加商品</span> <span>17</span> <span>if</span>(<span>$key</span> == ''){ <span>//</span><span>判断用户有没有添加商品,如果为空就表示没有商品喽</span> <span>18</span> <span>$reback</span> = '5'; <span>//</span><span>返回值</span> <span>19</span> }<span>else</span><span>{ </span><span>20</span> <span>$boo</span> = <span>false</span>; <span>//</span><span>定义商品有没有被添加</span> <span>21</span> <span>$sqls</span> = "select id,shopping from tb_user where name = '".<span>$_SESSION</span>['member']."'"<span>; </span><span>22</span> <span>$shopcont</span> = <span>$admindb</span>->ExecSQL(<span>$sqls</span>,<span>$conn</span><span>); </span><span>23</span> <span>if</span>(!<span>empty</span>(<span>$shopcont</span>[0]['shopping'])){ <span>//</span><span>shopping为三维数组,用empty判断商品是否为空</span> <span>24</span> <span>$arr</span> = <span>explode</span>('@',<span>$shopcont</span>[0]['shopping']);<span>//</span><span>@分割数组也就是每个商品的值</span> <span>25</span> <span>foreach</span>(<span>$arr</span> <span>as</span> <span>$value</span>){ <span>//</span><span>foreach取出每个数组的值</span> <span>26</span> <span>$arrtmp</span> = <span>explode</span>(',',<span>$value</span>); <span>//</span><span>用explode分割得出商品的俱体信息</span> <span>27</span> <span>if</span>(<span>$key</span> == <span>$arrtmp</span>[0]){ <span>//</span><span>如果添加的商品等于已添加的商品</span> <span>28</span> <span>$reback</span> = '3'; <span>//</span><span>那么返回值就表示已添加</span> <span>29</span> <span>$boo</span> = <span>true</span><span>; </span><span>30</span> <span>break</span><span>; </span><span>31</span> <span> } </span><span>32</span> <span> } </span><span>33</span> <span>if</span>(<span>$boo</span> == <span>false</span>){ <span>//</span><span>方法一添加商品</span> <span>34</span> <span>$shopcont</span>[0]['shopping'] .= '@'.<span>$key</span>.',1'<span>; </span><span>35</span> <span>$update</span> = "update tb_user set shopping='".<span>$shopcont</span>[0]['shopping']."' where name = '".<span>$_SESSION</span>['member']."'"<span>; </span><span>36</span> <span>$shop</span> = <span>$admindb</span>->ExecSQL(<span>$update</span>,<span>$conn</span><span>); </span><span>37</span> <span>if</span>(<span>$shop</span><span>){ </span><span>38</span> <span>$reback</span> = 1<span>; </span><span>39</span> }<span>else</span><span>{ </span><span>40</span> <span>$reback</span> = '4'<span>; </span><span>41</span> <span> } </span><span>42</span> <span> } </span><span>43</span> }<span>else</span><span>{ </span><span>44</span> <span>$arrtmp</span> = <span>$key</span>.",1"; <span>//</span><span>方法二添加商品</span> <span>45</span> <span>$updates</span> = "update tb_user set shopping='".<span>$arrtmp</span>."' where name = '".<span>$_SESSION</span>['member']."'"<span>; </span><span>46</span> <span>$result</span> = <span>$admindb</span>->ExecSQL(<span>$updates</span>,<span>$conn</span><span>); </span><span>47</span> <span>if</span>(<span>$result</span><span>){ </span><span>48</span> <span>$reback</span> = 1<span>; </span><span>49</span> }<span>else</span><span>{ </span><span>50</span> <span>$reback</span> = '4'<span>; </span><span>51</span> <span> } </span><span>52</span> <span> } </span><span>53</span> <span> } </span><span>54</span> <span>} </span><span>55</span> <span>echo</span> <span>$reback</span><span>; </span><span>56</span> ?></span>
PHP オンライン ストア プログラムを使用することも、自分で作成することもできます。
オンライン ストア プログラムを使用する場合は、関連する背景説明を詳しくお読みください。実は、とてもシンプルなのです。
書く能力があれば、間違いなく自分で書くことができます、簡単です。
これは、Ajax を使用して非同期送信を行う方法です。 。 。カートに追加();