The script gets subproducts out of a DB, lists them, and lets you update them. If the price of the subproduct is not in the right format, then it throws an error, here is the function I made to check the price
//function check price
function isprice($price){
if(preg_match("/^\d+.\d\d$/", $price)) {
return true;
} else {
return false;
}
}
now what happens is, if you put the price as 1, it quits due to an error, if you put it as 1.00, then it continues, which is right... but for some rason, the script below will convert 1.00 to 1, this it throws an error later. I am so confused!! It wont include the decimal or anything after it. If anyone sees an issue with it, please let me know, might it be because its in an array? Im not sure, here is the code<?php
if(empty($_GET['product'])){
echo '<div align="center">Product ID not set</div>';
} else {
$product = $_GET['product'];
$sql = "SELECT * FROM `products` WHERE `id`='$product'";
$mysql = mysql_query($sql);
if(!$mysql) { die(mysql_error()); }
$get = mysql_fetch_array($mysql);
$id = $get['id'];
if($id !== $product){
echo '<div align="center">That is not a known product</div>';
} else {
if((!empty($_POST['spName'])) && (!empty($_POST['spDesc'])) && (!empty($_POST['spPrice']))){
$spPrice = $_POST['spPrice'];
$spName = $_POST['spName'];
$spDesc = $_POST['spDesc'];
if(isset($_POST['spChecked'])){
$spChecked = $_POST['spChecked'];
} else {
$spChecked = 'no';
}
echo 'Adding Subproduct values..<br>';
//clear any subproducts out that are for $product
$sql1 = "DELETE FROM `subproducts` WHERE `product`='$product'";
$mysql1 = mysql_query($sql1);
if(!$mysql1){ die(mysql_error()); }
//Clear done
foreach($spName as $key => $value){
if($spChecked[$key] == "yes"){
$default = "yes";
} else {
$default = "no";
}
if(!isprice($spPrice[$key])){
echo 'Error adding '.$spName[$key].' - Price not correct value ('.$spPrice[$key].'), SKIPPED<br>';
} else {
$sql = "INSERT INTO `subproducts`
(`name`, `desc`, `price`, `product`, `default`)
VALUES
('$spName[$key]', '$spDesc[$key]', '$spPrice[$key]', '$product', '$default')";
$mysql = mysql_query($sql);
if(!$mysql){
echo "There was an error!". mysql_error()."<br>";
} else {
echo 'Added '.$spName[$key].'<br>';
}
}
}
echo "..done";
} else {
$sql="SELECT * FROM `computers`";
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
echo '<script language="javascript">
fields = 0;
function addProduct() {
if (fields != 100000000) {
document.getElementById(\'text\').innerHTML += \'<div align="center"><table width="95%" border="0" cellspacing="0" cellpadding="2" style="border-bottom-style:solid; border-bottom-color:#333333; border-bottom-width:thin; border-top-style:solid; border-top-color:#333333; border-top-width:thin"><tr><td >Name</td><td ><input name="spName[]" type="text" id="spName" style="width:100%"/></td><td >Price</td><td >$<input name="spPrice[]" type="text" id="spPrice" size="4" value="00.00" /></td></tr><tr><td>Description</td><td><input name="spDesc[]" type="text" style="width:100%" id="spDesc" /></td><td colspan="2">Check by default <input name="spChecked[]" type="radio" value="yes" /></td></tr></table></div>\';
fields += 1;
} else {
document.getElementById(\'text\').innerHTML += "<br />Only 100000000 upload fields allowed.";
document.form.add.disabled=true;
}
}
</script>';
echo '
<div align="center"><input type="button" value="Add SubProduct Field" onclick="addProduct()"></div><strong>NOTE:</strong> Please add all subproducts you are going to utilize
first before you start to fill them in. Once you add a new field, it will clear all the other fields!<br><br>
<form action="" method="POST">
<div id="text">'; //Break to get existing sub products
$sql2 = "SELECT * FROM `subproducts` WHERE `product`='$id' ORDER BY `price`";
$result2 = mysql_query($sql2);
$rows2 = mysql_num_rows($result2);
for($i2=0;$i2<$rows2;$i2++){
$spName = mysql_result($result2, $i2, 'name');
$spPrice = mysql_result($result2, $i2, 'price');
$spDesc = mysql_result($result2, $i2, 'desc');
$spDefault = mysql_result($result2, $i2, 'default');
echo '<div align="center"><table width="95%" border="0" cellspacing="0" cellpadding="2" style="border-bottom-style:solid; border-bottom-color:#333333; border-bottom-width:thin; border-top-style:solid; border-top-color:#333333; border-top-width:thin"><tr><td >Name</td><td ><input name="spName[]" type="text" value= "'.$spName.'"id="spName" style="width:100%"/></td><td >Price</td><td >$<input name="spPrice[]" type="text" id="spPrice" size="4" value="'.$spPrice.'" /></td></tr><tr><td>Description</td><td><input name="spDesc[]" type="text" style="width:100%" id="spDesc" value="'.$spDesc.'" /></td><td colspan="2">Check by default <input name="spChecked[]" type="radio" value="yes" ';
if($spDefault == "yes"){
echo 'checked="checked"';
}
echo '/></td></tr></table></div>';
}
//unbreak
echo '</div>
<br>
<input type="submit" value="Save">
</form>';
}
}
}
?>


Sign In
Create Account


Back to top









