/
products_addf.php
357 lines (322 loc) · 16.5 KB
/
products_addf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
require_once("codelibrary/inc/db.php");
require_once("codelibrary/inc/functions.php");
/**
*@author RN Kushwaha
*@email at Rn.kushwaha022@gmail.com
*@created 5th April 2014
*/
@extract($_REQUEST);
validate_admin();
if( $_POST["submitForm"]=="yes"){
$_SESSION["sess_error_msg"] = array();
if($product_code==""){
$_SESSION["sess_error_msg"][] = "Please enter Product Code";
}
if($product_name==""){
$_SESSION["sess_error_msg"][] = "Please enter Product Name";
}
if($price==""){
$_SESSION["sess_error_msg"][] = "Please enter Price";
}
if( count( $_SESSION["sess_error_msg"] ) ){
$_SESSION["sess_msg"] = "<p>".implode("</p><p>", $_SESSION["sess_error_msg"])."</p>";
} else{
if( array_key_exists("image",$_FILES)){//used for image upload
if($_FILES["image"]["size"] > 0){
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES["image"]["tmp_name"]);
if(!in_array($detectedType, $allowedTypes)){//check for image type
$_SESSION["sess_msg"]="Invalid file type!";
} else{
$imgpath="image/";//img folder with slash in last
//$small_imgpath="image/thumb/";//small img folder with slash in last
$image = time().str_replace(" ","",$_FILES["image"]["name"]);
@move_uploaded_file($_FILES["image"]["tmp_name"],$imgpath.$image);
//@resize_img($imgpath.$image,100,80,false); //use it for image resize
//@copy($imgpath.$image,$small_imgpath.$image);
$new_name = "image/". $image;
// @watermark_image($imgpath . $image, $new_name);//for creating watermark with png logo, but does not add watermarks to a png image
//@watermark_text($imgpath.$image, $new_name, 20, "RN", "Kushwaha");//for creating watermark with text, does not add watermarks to a png image
}
}
}
$added_date = mysql_date($added_date);$datetime = format_date_time_mysql($datetime);
if(!$id){//insert query
$sql = "insert into tbl_products (product_code, product_name, product_desc, image, price, added_by, added_date, datetime, status) values( ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
if($stmt === false) {
trigger_error("Wrong SQL: " . $sql . " Error: " . $conn->error, E_USER_ERROR);
}
$stmt->bind_param("ssssdissi",$product_code, $product_name, $product_desc, $image, $price, $added_by, $added_date, $datetime, $status);
$stmt->execute();
if($stmt->affected_rows){
$_SESSION["sess_msg"] = $stmt->affected_rows." Products inserted Successfully with id: ".$stmt->insert_id;
$stmt->close();
header("Location: products_addf.php");
exit();
} else {
$stmt->close();
$_SESSION["sess_msg"] ="No Products inserted!";
}
} else {//update Query
$sql = "update tbl_products set product_code =?, product_name =?, product_desc =?, image =?, price =?, added_by =?, added_date =?, datetime =?, status =? where id=?";
$stmt = $conn->prepare($sql);
if($stmt === false) {
trigger_error("Wrong SQL: " . $sql . " Error: " . $conn->error, E_USER_ERROR);
}
$stmt->bind_param("ssssdissii",$product_code, $product_name, $product_desc, $image, $price, $added_by, $added_date, $datetime, $status, $id);
$stmt->execute();
if($stmt->affected_rows){
$_SESSION["sess_msg"] = "Record successfully updated! ";
$stmt->close();
header("Location: products_list.php");
exit();
} else {
$stmt->close();
$_SESSION["sess_msg"] ="No Products updated!";
}
}
}
}
if($_GET["id"]){
$sql="select * from tbl_products where id='".$_GET['id']."'";
$result=executeQuery($sql,$conn);
$num= $result->num_rows;
$row =$result->fetch_assoc();
@extract($row);
$result->free();
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add | Products :: <?php echo SITE_ADMIN_TITLE;?></title>
<link href="codelibrary/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php include("header.inc.php");?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="180" valign="top" class="rightBorder">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><?php include("left_menu.inc.php");?></td>
</tr>
<tr>
<td width="23"> </td>
</tr>
</table>
<br />
<br /></td>
<td width="1"><img src="images/spacer.gif" width="1" height="1" /></td>
<td width="1"><img src="images/spacer.gif" width="1" height="1" /></td>
<td height="400" align="center" valign="top">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="21" align="left" class="txt">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="title">
<tr>
<td width="76%" height="25"><img src="images/heading_icon.gif" width="16" height="16" hspace="5">Manage Products </td>
<td width="24%" align="right"><input name="b1" type="button" class="button1" id="b1" value="Manage Products" onClick="location.href='products_list.php'">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="400" align="center" valign="top"><br>
<form action="" method="post" enctype="multipart/form-data" name="frm" onSubmit="return validate(this)">
<table width="94%" border="0" align="center" cellpadding="4" cellspacing="0" class="greyBorder">
<input type="hidden" name="submitForm" value="yes">
<input type="hidden" name="id" class="txtfld" value="<?php echo $_GET['id'];?>">
<tr align="left">
<td height="25" colspan="2" class="blueBackground">
<?php if($_GET['id']){?>Edit <?php }else{?> Add <?php }?> Products </td>
</tr>
<?php if($_SESSION['sess_msg']){?>
<tr>
<td colspan="2" align="center" class="warning"><?php print $_SESSION['sess_msg']; $_SESSION['sess_msg']="";?></td>
</tr>
<?php }?>
<tr class="evenRow">
<td class="txt" align="right" colspan="2"><span class="warning">*</span> - Required Fields</td>
</tr> <?php //some dynamic fields from database;?><tr class="oddRow">
<td class="bldTxt" align="right" width="25%">Product Code :</td>
<td align="left" width="75%" class="txt"> <input name="product_code" type="text" class="txtfld" id="product_code" value="<?php echo $product_code;?>" size="40" maxlength="60"><span class="warning">*</span></td>
</tr><tr class="evenRow">
<td class="bldTxt" align="right" width="25%">Product Name :</td>
<td align="left" width="75%" class="txt"> <input name="product_name" type="text" class="txtfld" id="product_name" value="<?php echo $product_name;?>" size="40" maxlength="60"><span class="warning">*</span></td>
</tr><tr class="oddRow">
<td class="bldTxt" align="right" width="25%">Product Desc :</td>
<td align="left" width="75%" class="txt"> <textarea name="product_desc" class="txtfld editor" id="product_desc" rows="6" cols="65"><?php echo $product_desc;?></textarea></td>
</tr><tr class="evenRow">
<td class="bldTxt" align="right" width="25%">Image :</td>
<td align="left" width="75%" class="txt"> <input name="image" type="file" class="txtfld image" id="image" size="40"></td>
</tr><tr class="oddRow">
<td class="bldTxt" align="right" width="25%">Price :</td>
<td align="left" width="75%" class="txt"> <input name="price" type="text" class="txtfld date" id="price" value="<?php echo $price;?>" onblur="extractNumber(this,2,true);" onkeyup="extractNumber(this,2,true);" onkeypress="return blockNonNumbers(this, event, true, true);" size="30"><span class="warning">*</span></td>
</tr><tr class="evenRow">
<td class="bldTxt" align="right" width="25%">Added By :</td>
<td align="left" width="75%" class="txt"><select name="added_by" class="txtfld" id="added_by">
<option value="">Select Added By</option>
<?php //as we guess you have used a foreign key, here set here the name of column from referenced table to be showed in dropdown;
$column_Name = "";//add comma before column name to be fetch from db eg. , title
$column_show = "id";//replace with column name to be shown eg. title
$sqlQuery = "select id $column_Name from tbl_admin where 1 =1 order by id asc ";
$resultQuery = executeQuery($sqlQuery,$conn);//$conn is connection object
$numResults = $resultQuery->num_rows;
if( $numResults ){
while( $getResults = $resultQuery->fetch_assoc()){?>
<option value="<?php echo $getResults[id];?>" <?php if($added_by == $getResults["id"]) echo "selected='selected'";?>><?php echo $getResults[$column_show];?></option>
<?php }
}?>
</select></td>
</tr><tr class="oddRow">
<td class="bldTxt" align="right" width="25%">Added Date :</td>
<td align="left" width="75%" class="txt"> <input name="added_date" type="text" class="txtfld datepicker" id="added_date" value="<?php echo user_date($added_date);?>" size="20"></td>
</tr><tr class="evenRow">
<td class="bldTxt" align="right" width="25%">Datetime :</td>
<td align="left" width="75%" class="txt"> <input name="datetime" type="text" class="txtfld datetimepicker" id="datetime" value="<?php echo format_date_time($datetime,1);?>" size="30"></td>
</tr><tr class="oddRow">
<td class="bldTxt" align="right" width="25%">Status :</td>
<td align="left" width="75%" class="txt"> <select name="status" class="txtfld" id="status">
<option value="1" <?php if($status == 1) echo "selected='selected'";?>>Activate</option>
<option value="0" <?php if($status == 0) echo "selected='selected'";?>>Deactivate</option>
</select></td>
</tr><tr class="evenRow">
<td align=center colspan=100%>
<input type="submit" name="submit" class="button1" value="Submit" id="submitbtn1"/>
<input type="reset" name="reset" class="button1" value="Reset" id="submitbtn3"/>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
<?php include("footer.inc.php");?>
<script src="codelibrary/js/script_tmt_validator.js" type="text/javascript"></script>
<script type="text/javascript">
function validate(obj){
if(obj.product_code.value==""){
alert("Please enter Product Code");
obj.product_code.focus();
return false;
}
else if(obj.product_name.value==""){
alert("Please enter Product Name");
obj.product_name.focus();
return false;
}
else if(obj.price.value==""){
alert("Please enter Price");
obj.price.focus();
return false;
}
else {
document.getElementById("submitbtn1").value="Sending..";
document.getElementById("submitbtn1").setAttribute("disabled","disabled");
return true;
}
}
function extractNumber(obj, decimalPlaces, allowNegative){
var temp = obj.value;
// avoid changing things if already formatted correctly
var reg0Str = '[0-9]*';
if (decimalPlaces > 0) {
reg0Str += '\.?[0-9]{0,' + decimalPlaces + '}';
} else if (decimalPlaces < 0) {
reg0Str += '\.?[0-9]*';
}
reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
reg0Str = reg0Str + '$';
var reg0 = new RegExp(reg0Str);
if (reg0.test(temp)) return true;
// first replace all non numbers
var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
var reg1 = new RegExp(reg1Str, 'g');
temp = temp.replace(reg1, '');
if (allowNegative) {
// replace extra negative
var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
var reg2 = /-/g;
temp = temp.replace(reg2, '');
if (hasNegative) temp = '-' + temp;
}
if (decimalPlaces != 0) {
var reg3 = /\./g;
var reg3Array = reg3.exec(temp);
if (reg3Array != null) {
// keep only first occurrence of .
// and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
reg3Right = reg3Right.replace(reg3, '');
reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
}
}
obj.value = temp;
}
function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
var key;
var isCtrl = false;
var keychar;
var reg;
if(window.event) {
key = e.keyCode;
isCtrl = window.event.ctrlKey
}
else if(e.which) {
key = e.which;
isCtrl = e.ctrlKey;
}
if (isNaN(key)) return true;
keychar = String.fromCharCode(key);
// check for backspace or delete, or if Ctrl was pressed
if (key == 8 || isCtrl)
{
return true;
}
reg = /\d/;
var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
return isFirstN || isFirstD || reg.test(keychar);
}
</script>
<!----- this code is for datetime picker, change it if you do not want to use it -->
<link rel="stylesheet" media="all" type="text/css" href="codelibrary/js/dateandtime/jquery-ui.css" />
<link rel="stylesheet" media="all" type="text/css" href="codelibrary/js/dateandtime/jquery-ui-timepicker-addon.css" />
<script type="text/javascript" src="codelibrary/js/dateandtime/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="codelibrary/js/dateandtime/jquery-ui.min.js"></script>
<script type="text/javascript" src="codelibrary/js/dateandtime/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="codelibrary/js/dateandtime/jquery-ui-sliderAccess.js"></script>
<script type="text/javascript">
$jq=jQuery.noConflict();
$jq(".datepicker").datetimepicker({
timeFormat:"",
showTime:false,
changeMonth:true,
onSelect:function(){
$jq("#ui-datepicker-div").hide();
},
changeYear:true,
dateFormat: "dd/mm/yy"
})
$jq(".datetimepicker").datetimepicker({
timeFormat: "HH:mm",
changeMonth:true,
changeYear:true,
dateFormat: "dd/mm/yy"
})
</script>
<!---- this is for jce editor, change it if you do not want to use it ---->
<script type="text/javascript" src="codelibrary/js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="codelibrary/js/tiny_mce/include_mce.js"> </script>
<h1 class="editable">RN</h1>
</body>
</html>