products.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. require_once('conn.php');
  3. // Check login status (assuming you have a similar function in PHP)
  4. checkLogin("信息管理");
  5. // Initialize all variables to avoid undefined warnings
  6. $act = isset($_GET['act']) ? $_GET['act'] : '';
  7. $product_name = isset($_POST['ProductName']) ? htmlspecialchars($_POST['ProductName']) : '';
  8. $product_img = isset($_POST['ProductImg']) ? htmlspecialchars($_POST['ProductImg']) : '';
  9. $unit = isset($_POST['unit']) ? htmlspecialchars($_POST['unit']) : '';
  10. $moq = isset($_POST['moq']) ? htmlspecialchars($_POST['moq']) : '';
  11. $nosale = isset($_POST['nosale']) ? $_POST['nosale'] : array();
  12. $num = isset($_POST['num']) ? $_POST['num'] : array();
  13. $price = isset($_POST['price']) ? $_POST['price'] : array();
  14. $note = isset($_POST['note']) ? htmlspecialchars($_POST['note']) : '';
  15. $tips = isset($_POST['tips']) ? htmlspecialchars($_POST['tips']) : '';
  16. $page = isset($_GET['Page']) ? $_GET['Page'] : 1;
  17. $keys = isset($_GET['Keys']) ? urlencode($_GET['Keys']) : '';
  18. $keyscode = isset($_GET['Keys']) ? htmlspecialchars($_GET['Keys']) : '';
  19. // Handle form submissions and redirects before any output
  20. if ($act == 'save') {
  21. $id = isset($_POST['id']) ? $_POST['id'] : '';
  22. $is_edit = (!empty($id) && is_numeric($id));
  23. // Process nosale array into comma-separated string
  24. $nosale_str = '';
  25. if (is_array($nosale) && !empty($nosale)) {
  26. $nosale_clean = array_map('intval', $nosale); // Ensure all values are integers
  27. $nosale_str = implode(',', $nosale_clean);
  28. }
  29. if ($is_edit) {
  30. // Update existing product
  31. $sql = "UPDATE products SET
  32. ProductName = '" . mysqli_real_escape_string($conn, $product_name) . "',
  33. ProductImg = '" . mysqli_real_escape_string($conn, $product_img) . "',
  34. Addtime = NOW(),
  35. moq = '" . mysqli_real_escape_string($conn, $moq) . "',
  36. unit = '" . mysqli_real_escape_string($conn, $unit) . "',
  37. nosale = '" . $nosale_str . "',
  38. note = '" . mysqli_real_escape_string($conn, $note) . "',
  39. tips = '" . mysqli_real_escape_string($conn, $tips) . "'
  40. WHERE id = " . (int)$id;
  41. mysqli_query($conn, $sql);
  42. // Handle price updates
  43. mysqli_query($conn, "DELETE FROM price WHERE productId = " . (int)$id . " AND AreaId = 0");
  44. if (is_array($num) && is_array($price)) {
  45. foreach ($num as $key => $num_value) {
  46. if (isset($price[$key])) { // Only process if we have both num and price
  47. $num_value = empty($num_value) ? 0 : (float)$num_value;
  48. $price_value = empty($price[$key]) ? 0 : (float)$price[$key];
  49. $sql = "INSERT INTO price (productId, AreaId, num, price) VALUES
  50. (" . (int)$id . ", 0, " . $num_value . ", '" . $price_value . "')";
  51. mysqli_query($conn, $sql);
  52. }
  53. }
  54. }
  55. } else {
  56. // Insert new product
  57. $sql = "INSERT INTO products (ProductName, ProductImg, Addtime, unit, moq, nosale, note, tips)
  58. VALUES (
  59. '" . mysqli_real_escape_string($conn, $product_name) . "',
  60. '" . mysqli_real_escape_string($conn, $product_img) . "',
  61. NOW(),
  62. '" . mysqli_real_escape_string($conn, $unit) . "',
  63. '" . mysqli_real_escape_string($conn, $moq) . "',
  64. '" . $nosale_str . "',
  65. '" . mysqli_real_escape_string($conn, $note) . "',
  66. '" . mysqli_real_escape_string($conn, $tips) . "'
  67. )";
  68. mysqli_query($conn, $sql);
  69. $id = mysqli_insert_id($conn);
  70. // Handle price insertions
  71. if (is_array($num) && is_array($price)) {
  72. foreach ($num as $key => $num_value) {
  73. if (isset($price[$key])) { // Only process if we have both num and price
  74. $num_value = empty($num_value) ? 0 : (float)$num_value;
  75. $price_value = empty($price[$key]) ? 0 : (float)$price[$key];
  76. $sql = "INSERT INTO price (productId, AreaId, num, price) VALUES
  77. (" . (int)$id . ", 0, " . $num_value . ", '" . $price_value . "')";
  78. mysqli_query($conn, $sql);
  79. }
  80. }
  81. }
  82. }
  83. // Redirect after save
  84. header("Location: ?keys=" . $keys . "&Page=" . $page);
  85. exit();
  86. }
  87. // Handle bulk actions
  88. if ($act == 'postchk') {
  89. if (isset($_POST['chkbox']) && isset($_POST['chkact'])) {
  90. $chk_ids = array_map('intval', $_POST['chkbox']);
  91. $chk_act = (int)$_POST['chkact'];
  92. if (!empty($chk_ids)) {
  93. $ids_str = implode(',', $chk_ids);
  94. switch ($chk_act) {
  95. case 0:
  96. case 1:
  97. $sql = "UPDATE customer SET cs_state = " . $chk_act . " WHERE id IN (" . $ids_str . ")";
  98. break;
  99. case -1:
  100. $sql = "DELETE FROM products WHERE id IN (" . $ids_str . ")";
  101. break;
  102. }
  103. if (isset($sql)) {
  104. mysqli_query($conn, $sql);
  105. }
  106. }
  107. header("Location: ?Keys=" . $keys . "&Page=" . $page);
  108. exit();
  109. }
  110. }
  111. ?>
  112. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  113. <html xmlns="http://www.w3.org/1999/xhtml">
  114. <head>
  115. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  116. <title>产品信息管理</title>
  117. <link rel="stylesheet" href="css/common.css" type="text/css" />
  118. <script language="javascript" src="js/jquery-1.7.2.min.js"></script>
  119. <script type="text/javascript" src="js/js.js"></script>
  120. <script type="text/javascript" src="js/SearchArea.js"></script>
  121. <script type="text/javascript" src="xheditor-1.1.9/xheditor-1.1.9-zh-cn.min.js"></script>
  122. </head>
  123. <body>
  124. <div id="man_zone">
  125. <?php
  126. // Handle add/edit form display
  127. if ($act == 'add' || $act == 'edit') {
  128. $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
  129. $is_edit = ($id > 0);
  130. if ($is_edit) {
  131. $sql = "SELECT ProductName, ProductImg, unit, moq, nosale, note, tips
  132. FROM products WHERE id = " . $id;
  133. $result = mysqli_query($conn, $sql);
  134. if ($row = mysqli_fetch_assoc($result)) {
  135. $product_name = htmlspecialchars($row['ProductName']);
  136. $product_img = htmlspecialchars($row['ProductImg']);
  137. $unit = htmlspecialchars($row['unit']);
  138. $moq = htmlspecialchars($row['moq']);
  139. $nosale = $row['nosale'];
  140. $note = htmlspecialchars($row['note']);
  141. $tips = htmlspecialchars($row['tips']);
  142. }
  143. }
  144. $href_str = "?keys=" . $keys . "&Page=" . $page;
  145. ?>
  146. <form name="form1" method="post" action="<?php echo $href_str; ?>&act=save">
  147. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  148. <tbody>
  149. <tr>
  150. <th width="8%">产品名称</th>
  151. <td><input type="text" id="ProductName" name="ProductName" value="<?php echo $product_name; ?>" class="txt1" />
  152. <input type="hidden" name="id" value="<?php echo $id; ?>" /></td>
  153. </tr>
  154. <tr>
  155. <th width="8%">产品图片</th>
  156. <td><input type="text" id="ProductImg" name="ProductImg" placeholder="186x*186px" value="<?php echo $product_img; ?>" class="txt1" style="width:390px;float:left;" />
  157. <iframe src="uploadfile.php" frameborder="0" scrolling="no" style="width:400px;height:22px;float:left;margin-left:10px;"></iframe></td>
  158. </tr>
  159. <tr>
  160. <th width="8%">计价单位</th>
  161. <td><input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" class="txt1"/></td>
  162. </tr>
  163. <tr>
  164. <th width="8%">起订数量</th>
  165. <td><input type="text" id="moq" name="moq" value="<?php echo $moq; ?>" class="txt1"/></td>
  166. </tr>
  167. <tr>
  168. <th width="8%">默认售价</th>
  169. <td>
  170. <div class="Price">
  171. <?php
  172. if ($is_edit) {
  173. $price_sql = "SELECT num, price FROM price WHERE AreaId = 0 AND productId = " . $id . " ORDER BY num ASC";
  174. $price_result = mysqli_query($conn, $price_sql);
  175. if (mysqli_num_rows($price_result) > 0) {
  176. while ($price_row = mysqli_fetch_assoc($price_result)) {
  177. ?>
  178. <div class="priceitem">
  179. <label>≥</label>
  180. <input type="number" class="txt3 num" name="num[]" value="<?php echo $price_row['num']; ?>">
  181. <label class="unit"><?php echo $unit; ?></label>
  182. <label>售价</label>
  183. <input type="text" class="txt3 price" name="price[]" value="<?php echo $price_row['price']; ?>">
  184. <label>RMB</label>
  185. <span class="additem"></span>
  186. <span class="delitem"></span>
  187. <span class="note"></span>
  188. </div>
  189. <?php
  190. }
  191. }
  192. }
  193. if (!$is_edit || mysqli_num_rows($price_result) == 0) {
  194. ?>
  195. <div class="priceitem">
  196. <label>≥</label>
  197. <input type="number" class="txt3 num" name="num[]">
  198. <label class="unit"><?php echo $unit; ?></label>
  199. <label>售价</label>
  200. <input type="text" class="txt3 price" name="price[]">
  201. <label>RMB</label>
  202. <span class="additem"></span>
  203. <span class="delitem"></span>
  204. <span class="note"></span>
  205. </div>
  206. <?php
  207. }
  208. ?>
  209. </div>
  210. </td>
  211. </tr>
  212. <tr>
  213. <th width="8%">不报价地区</th>
  214. <td>
  215. <ul class="areadd">
  216. <?php
  217. if (!empty($nosale)) {
  218. $area_sql = "SELECT id, countryName FROM country WHERE id IN(" . $nosale . ")";
  219. $area_result = mysqli_query($conn, $area_sql);
  220. while ($area_row = mysqli_fetch_assoc($area_result)) {
  221. ?>
  222. <li>
  223. <input type="hidden" name="nosale[]" value="<?php echo $area_row['id']; ?>">
  224. <span class="cname"><?php echo htmlspecialchars($area_row['countryName']); ?></span>
  225. <span class="close"></span>
  226. </li>
  227. <?php
  228. }
  229. }
  230. ?>
  231. </ul>
  232. <input type="text" id="AreaSearch" class="fastsearch">
  233. <div id="arealist" class="productlist"><ul></ul></div>
  234. </td>
  235. </tr>
  236. <tr>
  237. <th width="8%">不报价处理方式</th>
  238. <td><input type="text" id="note" name="note" value="<?php echo $note; ?>" class="txt1"/></td>
  239. </tr>
  240. <tr>
  241. <th width="8%">备注</th>
  242. <td><input type="text" id="tips" name="tips" value="<?php echo $tips; ?>" class="txt1"/></td>
  243. </tr>
  244. <tr>
  245. <th></th>
  246. <td colspan="2">
  247. <input type="submit" name="save" value="确定" class="btn1" />
  248. <input type="reset" name="reset" value="重置" class="btn1" />
  249. <input type="button" value="返回" class="btn1" onClick="location.href='<?php echo $href_str; ?>'" />
  250. </td>
  251. </tr>
  252. </tbody>
  253. </table>
  254. </form>
  255. <?php
  256. } else {
  257. // Display product list
  258. $sql = "SELECT id, ProductName, ProductImg FROM products ORDER BY id DESC";
  259. $result = mysqli_query($conn, $sql);
  260. $total_records = mysqli_num_rows($result);
  261. $records_per_page = 18;
  262. $total_pages = ceil($total_records / $records_per_page);
  263. // Validate page number
  264. if ($page == 'end') $page = $total_pages;
  265. if (!is_numeric($page) || $page < 1) $page = 1;
  266. if ($page > $total_pages) $page = $total_pages;
  267. $offset = ($page - 1) * $records_per_page;
  268. $sql .= " LIMIT $offset, $records_per_page";
  269. $result = mysqli_query($conn, $sql);
  270. ?>
  271. <form id="form1" method="post" action="?act=postchk&Keys=<?php echo $keys; ?>&Page=<?php echo $page; ?>" onSubmit="return false">
  272. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  273. <thead>
  274. <tr>
  275. <th width="4%"><input type="checkbox" name="chkall" id="chkall" onClick="chkboxall(this,'chkbox')" /></th>
  276. <th width="6%">序号</th>
  277. <th width="30%">产品名称</th>
  278. <th width="40%">图片</th>
  279. <th width="20%">操作</th>
  280. </tr>
  281. </thead>
  282. <tbody>
  283. <?php
  284. if (mysqli_num_rows($result) > 0) {
  285. $temp_num = $offset;
  286. while ($row = mysqli_fetch_assoc($result)) {
  287. $temp_num++;
  288. ?>
  289. <tr onMouseOver="this.style.background='#F7FCFF'" onMouseOut="this.style.background='#FFFFFF'">
  290. <td align="center"><input type="checkbox" name="chkbox[]" value="<?php echo $row['id']; ?>" /></td>
  291. <td align="center"><?php echo $temp_num; ?></td>
  292. <td align="center"><?php echo htmlspecialchars($row['ProductName']); ?></td>
  293. <td align="center"><img src="<?php echo htmlspecialchars($row['ProductImg']); ?>" width="80px"></td>
  294. <td align="center">
  295. <a href="?Keys=<?php echo $keys; ?>&Page=<?php echo $page; ?>&act=edit&id=<?php echo $row['id']; ?>" class="ico_edit ico">修改</a>
  296. </td>
  297. </tr>
  298. <?php
  299. }
  300. } else {
  301. ?>
  302. <tr>
  303. <td colspan="9" align="center">
  304. <?php echo empty($keys) ? 'Sorry,当前暂无信息' : '<a href="?">Sorry,没有找到"' . htmlspecialchars($keyscode) . '"相关的信息,点击返回</a>'; ?>
  305. </td>
  306. </tr>
  307. <?php
  308. }
  309. ?>
  310. </tbody>
  311. <tfoot>
  312. <tr>
  313. <td colspan="9">
  314. <div class="showpagebox">
  315. <?php
  316. if ($total_pages > 1) {
  317. $page_name = "?Keys=" . $keys . "&";
  318. $page_len = 3;
  319. // Previous page links
  320. if ($page > 1) {
  321. echo "<a href=\"{$page_name}Page=1\">首页</a>";
  322. echo "<a href=\"{$page_name}Page=" . ($page - 1) . "\">上一页</a>";
  323. }
  324. // Calculate page range
  325. if ($page_len * 2 + 1 >= $total_pages) {
  326. $start_page = 1;
  327. $end_page = $total_pages;
  328. } else {
  329. if ($page <= $page_len + 1) {
  330. $start_page = 1;
  331. $end_page = $page_len * 2 + 1;
  332. } else {
  333. $start_page = $page - $page_len;
  334. $end_page = $page + $page_len;
  335. }
  336. if ($page + $page_len > $total_pages) {
  337. $start_page = $total_pages - $page_len * 2;
  338. $end_page = $total_pages;
  339. }
  340. }
  341. // Page numbers
  342. for ($i = $start_page; $i <= $end_page; $i++) {
  343. if ($i == $page) {
  344. echo "<a class=\"current\">{$i}</a>";
  345. } else {
  346. echo "<a href=\"{$page_name}Page={$i}\">{$i}</a>";
  347. }
  348. }
  349. // Next page links
  350. if ($page < $total_pages) {
  351. if ($total_pages - $page > $page_len) {
  352. echo "<a href=\"{$page_name}Page={$total_pages}\">...{$total_pages}</a>";
  353. }
  354. echo "<a href=\"{$page_name}Page=" . ($page + 1) . "\">下一页</a>";
  355. echo "<a href=\"{$page_name}Page={$total_pages}\">尾页</a>";
  356. }
  357. ?>
  358. <input type="text" id="Pagego" value="<?php echo $page; ?>"
  359. onFocus="if(this.value == '<?php echo $page; ?>'){this.value='';}"
  360. onBlur="if(this.value == ''){this.value='<?php echo $page; ?>';}"
  361. onKeyUp="this.value=this.value.replace(/\D/g,'')"
  362. onKeyDown="if(event.keyCode==13){location.href='<?php echo $page_name; ?>Page='+document.getElementById('Pagego').value}" />
  363. <?php
  364. }
  365. ?>
  366. </div>
  367. <div class="postchkbox">
  368. <select id="chkact" name="chkact">
  369. <option value="1">显示</option>
  370. <option value="0">隐藏</option>
  371. <option value="-1">删除</option>
  372. </select>
  373. <input type="button" value="执行" onClick="postchk(1)" class="btn1" />
  374. <input type="button" value="新增" onClick="location.href='?act=add'" class="btn1" />
  375. </div>
  376. </td>
  377. </tr>
  378. </tfoot>
  379. </table>
  380. </form>
  381. <?php
  382. }
  383. mysqli_close($conn);
  384. ?>
  385. </div>
  386. </body>
  387. </html>