vincej Posted May 2, 2012 Share Posted May 2, 2012 Hi - To me, this doesn't make sense. My Order form uploads product weights in an array . If I echo out those weights they are fine ie 25.25 However, when I try to update the DB for some reason, the for loop strips out all but the first digit, so 25.25 becomes '2' or indeed if I had entered 9999999 it just '9' is entered in to the DB. I have tried using number_format to pre-process the decimals, - but I get an error saying you can not use it in a write format. Many Thanks to anyone who can help with this !! PS - I have abbreviated the below code for clarity: $weight = $_POST['weight']; for ($i = 0; $i < $numloops ; $i++) { $sql =" UPDATE `confirmedorder` SET orderid = '$orderid[$i]', weight='$weight[$i]',status='finished', ordervalue='$ordervalue' WHERE customerid = $customerid "; $this->db->query($sql); } Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted May 2, 2012 Share Posted May 2, 2012 LIkely the column value type you've selected is not `float` or the length is too short. What is the structure of the table? Quote Link to comment Share on other sites More sharing options...
vincej Posted May 2, 2012 Author Share Posted May 2, 2012 Hi Thanks - it was set to decimal 5,2 so I changed it to float also 5,2 and it made no difference. Bummer Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 2, 2012 Share Posted May 2, 2012 You're sending it to the DB as a string. Remove the single quotes around the variable. Edit: Or, your weight is not an array of numbers but a string, so you're getting each digit separately. print_r($weight); before the loop, what do you get? Quote Link to comment Share on other sites More sharing options...
vincej Posted May 2, 2012 Author Share Posted May 2, 2012 Thanks jesirose , Nope unfortunately it made no difference. Here is the whole code unabbreviated. You will notice that I have a couple of tests. Test One where there is no [$i] echos out with the full decimals. Test two with [$i] strips the decimals. function finishedorder(){ $prodname = $_POST['prodname']; $prodid =$_POST['prodid']; $quantity = $_POST['quantity']; $pricelb = $_POST['pricelb']; $customerid = $_POST['customerid']; $price = $_POST['price']; $weight = $_POST['weight']; $customerid = $_POST['customerid']; $orderid = $_POST['orderid']; $numloops = count($_POST['prodid']); for ($i = 0; $i < $numloops ; $i++) { $pricelb = $pricelb[$i]; $quantity = $quantity[$i]; $weight = $weight[$i]; echo "weight test One ". $weight ."<br/>"; //TESTS echo "weight Test Two " . $weight[$i] . "<br/>"; echo "ordervalue ". $ordervalue = $pricelb * $weight . "<br/>"; $sql =" UPDATE `confirmedorder` SET orderid = '$orderid[$i]', weight=$weight[$i],status='finished', ordervalue='$ordervalue' WHERE customerid = $customerid "; $this->db->query($sql); } } Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 2, 2012 Share Posted May 2, 2012 $weight = $weight[$i]; ... $sql =" UPDATE `confirmedorder` SET orderid = '$orderid[$i]', weight=$weight[$i],status='finished', ordervalue='$ordervalue' WHERE customerid = $customerid "; This explains your error. You didn't answer my question but the problem is that you are taking your array and getting the value, then treating that value like an array. Quote Link to comment Share on other sites More sharing options...
xyph Posted May 2, 2012 Share Posted May 2, 2012 To elaborate, PHP allows you to use array notation in strings to get a specific character, starting at 0. <?php $string = 'abcde'; echo $string[2]; // outputs 'c' echo $string[4]; // outputs 'e' ?> http://php.net/manual/en/language.types.string.php String access and modification by character Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. The functions substr() and substr_replace() can be used when you want to extract or replace more than 1 character. Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. Quote Link to comment Share on other sites More sharing options...
vincej Posted May 2, 2012 Author Share Posted May 2, 2012 jesirose Thank you for your help ! sorry - I didn't see your question. It works !! the problem is that you are taking your array and getting the value, then treating that value like an array. So I removed the array brackets off $weight from the Update statement - I case of Male Onset Blindness ! Many Thanks Thanks xyph - I didn't know that ! But in my case all seems to be working well - it is grabbing the whole string .... I wonder why ? Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 2, 2012 Share Posted May 2, 2012 But in my case all seems to be working well - it is grabbing the whole string .... I wonder why ? No, it's not...that's the entire thing you're complaining about, and you just said that when you removed the brackets, it works. Before it was converting your number to a string and getting one character of it, which is what he described. Now, it's not... Quote Link to comment Share on other sites More sharing options...
xyph Posted May 2, 2012 Share Posted May 2, 2012 It was always a string. Using square brackets on an integer or float returns nothing. You should also change your column back to decimal data type. It's more accurate when dealing with currencies. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 2, 2012 Share Posted May 2, 2012 True, I was thinking too much about php's automatic type conversions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.