PHP - Storing Decimal Places In A Database
Hi I am using Zen-Cart to which I have made significant changes. Two years ago I managed to change the Products Sort Order from Integer to Decimal(5,2) to store books such as 1, 1.5, 1.75, 2 etc. Regrettably we had a major server issue and I also lost the new coding. Having restored everything else whenever we enter a book series number such as 1.5 it only stores as 1. To date the zen-Cart forums have been unable to offer a solution so I am reaching out to see if anyone else can advise me of how to get this working again please.
Thanks in advance Owen
Similar TutorialsHi, I'm trying to force 2 decimal places with the following function but it 's not working. Any suggestions? Code: [Select] function ShippingCost() { $shippingCost = 4.00; number_format($shippingCost,2); return $shippingCost; } Hi.. I encountered problem in rounding of numbers into two decimal places. here is my sample code: if($W4_STATUS == 1 AND $DEPENDENTS == 0 AND $TotEarn >= 7917 AND $TotEarn <= 12500) { $TAX = ($TotEarn - 7917); $TAX = (937.50 + ($TAX * .25)); $TAX = number_format($TAX, 2, '.', ''); } for example from this: $TAX = ($TotEarn - 7917); $TAX = (937.50 + ($TAX * .25)); the output is: 1417.615 using this: $TAX = number_format($TAX, 2, '.', ''); the output was : 1417.61 but it should be : 1417.62 Thank you Hey, just wondering what would be the best way of adding .1 to a string. For example: Code: [Select] $newversion = $currentversion + '0.0.1'; $newversion is taken from a db, lets say for this example it is 0.2.1 This code simply outputs 0.2 Any ideas? Thanks =) Bascially i add a couple numbers up and display the sum. sometimes the number is something like this: 234.237 i want it to display : 234.24 i realize its needs "round()" in it but i dont know how to use it.. any help? i am echoing out a value which is 1.2900 but i want it to echo out 1.29 how do i do this? cheers matt This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=311051.0 I just downloaded and installed IP2Location database and everything is smooth for IPv4 addresses. (If you want to get location from peoples IP the free version is he http://lite.ip2locat...ipcode-timezone) You convert the IPv4 address to an IP number and then do a database query. See he
$ip_split = split ("\.", "$ip"); $ip_num = $ip_split[3] + ($ip_split[2] * 256) + ($ip_split[1] * 256 * 256) + ($ip_split[0] * 256 * 256 * 256); $result = mysql_query("SELECT * FROM ip2location_db11 WHERE '$ip_num' BETWEEN ip_from AND ip_to");However I now need to do this for an IPv6 address but there is no documentation or anything I can find Googleing about how to do this for IPv6. Obviously the same method will not work as it has letters in the address as well as more numbers. There is obviously some difference to it. Anyone know what to do? Thanks! Quote i want to store a value from a database and use it as variable in php code can anyone help me out in this code i want to store value of copies in a php variable and want that it should be more than 0 (zero) for furthur calculations $update_book="update book set copies=copies-1 where bookid='$bookid'"; $result=mysql_query($update_book,$linkID1); if($result) { print "<html><body background=\"header.jpg\"> <p>book successfully subtracted from database</p></body></html>"; } else { print "<html><body background=\"header.jpg\"> <p>problem occured</p></body></html>"; } Hi, wondering if somebody can tell me where I'm going wrong (I'm new to all of this). I have the following php code which uploads an image file into my database: Code: [Select] //Connect to database include 'Resources/Include/db.inc.php'; $tmp=$_FILES['image']['tmp_name']; //get users IP $ip=$_SERVER['REMOTE_ADDR']; //Don't do anything if file wasn't selected if (!empty($tmp)) { //Copy file to temporary folder copy($tmp, "./temporary/".$ip.""); //open the copied image, ready to encode into text to go into the database $filename1 = "./temporary/".$ip; $fp1 = fopen($filename1, "rb"); //record the image contents into a variable $contents1 = fread($fp1, filesize($filename1)); $contents1 = addslashes($contents1); //close the file fclose($fp1); $ftype = $_FILES['image']['type']; //insert information into the database if(!mysql_query("INSERT INTO LetterImages (Data,Type,LetterID,Page)"." VALUES ( '$contents1', '$ftype',1,1)")){ echo mysql_error(); } //delete the temporary file we made unlink($filename1); } This seems to work ok, as when I go to the LetterImages table there is now an additional row with a file in the blob field. I then have the following code which is supposed to display the image: Code: [Select] $result=mysql_query("SELECT * FROM LetterImages WHERE LetterID=1 AND Page=1"); //fetch data from database $sqldata=mysql_fetch_array($result); $encoded=stripslashes($sqldata['Data']); $ftype=$sqldata['Type']; //tell the browser what type of image to display header("Content-type: $ftype"); //decode and echo the image data echo $encoded; Instead of displaying an image, however, this just displays pages and pages of incomprehensible data. Can anybody tell me where I'm going horribly wrong? Dear All, I am developing a like/dislike voting system for a scientific experiment in which the user will cast vote on a number of images. I have already written the code for it but the code is unable to store the data in the database. Kindly help me out in this issue. Below is my code for it: HTML Form for Like/Dislike Button: Code: [Select] <html> <head> <title> Like/Dislike buttons </title> </head> <body> <h1> Like/Dislike buttons </h1> <form name="form1" action="likedislike.php" method ="post"> <input type="submit" name="Like" value="Like"> <input type="submit" name="Dislike" value="dislike"> </form> </body> </html> PHP code for retrieving data and storing the vote in the database: Code: [Select] <?php session_start(); ob_start(); $host="localhost"; $username="computat_abhi"; $password="1123581321"; $databasename="computat_button"; $tbl_name="record"; $db=mysql_connect ("localhost","computat_abhi","1123581321")or die("cannot connect"); mysql_select_db($databasename,$db); if( isset($_POST['Like']) ) { $sql = 'INSERT INTO record(Image Number,Like Counter) VALUES ("b1.php",1)'; $result = mysql_query($sql); } elseif(isset($_POST['Dislike'])){ $sql = 'INSERT INTO record(Image Number,Dislike Counter) VALUES ("b1.php",1)'; $result = mysql_query($sql); } ob_end_flush(); ?> Thank you for your help. I need some advice, I'm not sure if this is the right section to post this or not but I'll give it a try. I am creating a website for a small group of people to be able to message back and forth. I ran into a problem when doing the whole reply to a message option. I dont want to have each individual message as a new entry in my database, and then look for each message in that conversation when loading the conversation, I thought about inserting something like +=+=+=+=+=+= in between messages in the conversation so that when i pull up that conversation i could just separate the messages into an array by looking for that +=+=+=+=+=+= in the string of text and separating all the messages out. I want to know if anyone knows a better way of doing this or if this is a good way to go. thanks for any suggestions. I've got an array like this: $firstquarter = array('January', 'February', 'March'); Then I'm adding it to a MySQL DB as follows: $insert = "INSERT INTO sometable (months) VALUES ('$firstquarter') "; $insertresult = mysql_query($insert) or die ('Error in Insert query: ' . mysql_error()); When I use PHPMyAdmin I see a this value stored in the DB: 'Array' Can I retrieve the values, by index, from this database field?? Okay I'm a newbie at PHP but was trying to write a counter for my website to count the amount of times a specific button is clicked. My desired outcome is as follows: If the form submit button "like" or "dislike" is clicked, get user's ip and check to see if it already exists in the database. If it exists, don't increment the count (of either button), otherwise log the ip and increment the count (of whichever button is clicked) by 1. Currently I've got the increment working (testing only the like button at the moment) and I've got the ip being stored into the database, but cannot get the comparison to work. Each time the button is clicked it adds the ip address into the database instead of disallowing it. Note: there are two different tables--one storing the ip (counter) and one storing the like clicks (Designs) (dunno if there's a better way?) Excuse my newbieness. The problem area: (like button only): if(isset($_POST['like'])) { $ip_address = $_SERVER['REMOTE_ADDR']; $query = mysql_query("SELECT ip FROM counter WHERE ip = '$ip_address'"); $num_rows = mysql_num_rows($query); if ($num_rows > 0) { die ("You've already clicked the button!"); } else { mysql_query("INSERT INTO counter VALUES ($ip_address)"); mysql_query("UPDATE Designs SET up = up + 1 WHERE ref = 'cataclysm'"); } } $select = mysql_query("SELECT up FROM Designs"); while($row = mysql_fetch_array($select)) { echo $row['up']; } For some reason my for loop only seems to be storing some data in my fields, most of the time the data is not true but simply duplicating one of the entries. So instead of having a string of 1,2,3,4 for instance it would store all of them as 4. The entires are selected using fields in a multiple select box, in theory of someone choses 10 unique entries from the "Systems" category all 10 should be made into rows in the MySQL table. Any idea why this is happening? The form, if ($type == "games") { echo "<tr><td>".$name."</td><td><select name='".$name."_".$i."'[] multiple='multiple'><option value='' selected>--------</option>"; $sqla = mysql_query("SELECT * FROM ".$pre."games ORDER BY `name` ASC") or die(mysql_error()); while($row2a = mysql_fetch_array($sqla)) { $system = mysql_fetch_array(mysql_query("SELECT * FROM ".$pre."systems WHERE id = '".$row2a[system]."'")); echo "<option value='".$row2a[id]."'>".$row2a[name]." - ".$system[name]."</option>"; } echo "</select></td></tr>"; } if ($type == "system") { echo "<tr><td>".$name."</td><td><select name='".$name."_".$i."'[] multiple='multiple'><option value='' selected>--------</option>"; $sqlb = mysql_query("SELECT * FROM ".$pre."systems ORDER BY `name` ASC") or die(mysql_error()); while($row2b = mysql_fetch_array($sqlb)) { echo "<option value='".$row2b[id]."'>".$row2b[name]."</option>"; } echo "</select></td></tr>"; } The PHP code, while($row = mysql_fetch_array($query)) { $name = "$row[name]"; for ($i = 0; $i < count($_POST["systems_$i"]); $i++) { mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'systems', '".$_POST["systems_$i"]."', '".$fetch[0]."', 'content')"); } for ($i = 0; $i < count($_POST["games_$i"]); $i++) { mysql_query("INSERT INTO ".$pre."fielddata VALUES (null, 'games', '".$_POST["games_$i"]."', '".$fetch[0]."', 'content')"); } I am working on a kind of CMS for my own website which no one else will be using but me as a way of improving my php skills, and am having problems with retrieving data from the database that holds both text and php code. I have searched the web and found that i should be using eval() for the code to be executed before it is send to the browser but cannot get it to work and can't find my mistake(s). the php code will always be the same, and is supposed to retrieve the id number of a page to use in a link (and works fine when tested by loading the code directly without retrieving it from the database) this is an example of data stored in the database Code: [Select] The <a href="page_builder.php?id=<?php echo $page->id('mines_DwarvenMines') ?>">Dwarven Mines</a> have a great selection of Ores,... Of course when I leave it like this, hovering over the link in my page will show exactly that and lead to nowhere Code: [Select] localhost/page_builder.php?id=<?php echo $page->id('mines_DwarvenMines') ?> Most of these links appear in tips given at the end of the page and are processed as followed Code: [Select] $questtips = $quest->getQuestTips(); $tips = ""; if ($questtips == "none") { $tips = "/"; } else { foreach($questtips as $tip) { $tips .= "<li>"; $tips .= $tip->getTip(); $tips .= "</li>"; } } and finally put on screen by the presentation layer as followed Code: [Select] <h2>Tips & Extra Info</h2> <div class="tipsList"> <ul> <?php echo $tips ?> </ul> </div> I have tried all sorts to get the code to be executed when retrieved from the database before being send to the browser so that this particular link would say "localhost/page_builder.php?id=57" but I cannot get it to work, though I suspect it is fairly easy. I suspect I would have to store the data in a different format in the database? And how exactly do I use the eval() function in my case? Could someone please adjust my code so that it does work? Thanks Hello,
I have a table with these columns (database)
table.JPG 34.95KB
0 downloads
and i have a static form through which user define the installments. Static 24 input boxes are there like this
<form name="installment" method="post" action="" enctype="multipart/form-data" onsubmit="return validate()"> <table id="dt_hScroll" class="table table-striped"> <thead> <tr> <th>SL No.</th> <th>Amount</th> <th>Due Date</th> </tr> <tr> <td> 1. </td> <td> <input type="text" name="installment1" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date1" class="tcal span4" value="<?php //echo date("Y-m-d"); ?>" /> </td> </tr> <tr> <td> 2. </td> <td> <input type="text" name="installment2" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date2" class="tcal span4" /> </td> </tr> <tr> <td> 3. </td> <td> <input type="text" name="installment3" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date3" class="tcal span4" /> </td></tr> <tr> <td> 4. </td> <td> <input type="text" name="installment4" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date4" class="tcal span4" /> </td></tr> <tr> <td> 5. </td> <td> <input type="text" name="installment5" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date5" class="tcal span4" /> </td></tr> <tr> <td> 6. </td> <td> <input type="text" name="installment6" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date6" class="tcal span4" /> </td></tr> <tr> <td> 7. </td> <td> <input type="text" name="installment7" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date7" class="tcal span4" /> </td></tr> <tr> <td> 8. </td> <td> <input type="text" name="installment8" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date8" class="tcal span4" /> </td></tr> <tr> <td> 9. </td> <td> <input type="text" name="installment9" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date9" class="tcal span4" /> </td></tr> <tr> <td> 10. </td> <td> <input type="text" name="installment10" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date10" class="tcal span4" /> </td></tr> <tr> <td> 11. </td> <td> <input type="text" name="installment11" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date11" class="tcal span4" /> </td></tr> <tr> <td> 12. </td> <td> <input type="text" name="installment12" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date12" class="tcal span4" /> </td></tr> <tr> <td> 13. </td> <td> <input type="text" name="installment13" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date13" class="tcal span4" /> </td></tr> <tr> <td> 14. </td> <td> <input type="text" name="installment14" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date14" class="tcal span4" /> </td></tr> <tr> <td> 15. </td> <td> <input type="text" name="installment15" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date15" class="tcal span4" /> </td></tr> <tr> <td> 16. </td> <td> <input type="text" name="installment16" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date16" class="tcal span4" /> </td></tr> <tr> <td> 17. </td> <td> <input type="text" name="installment17" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date17" class="tcal span4" /> </td></tr> <tr> <td> 18. </td> <td> <input type="text" name="installment18" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date18" class="tcal span4" /> </td></tr> <tr> <td> 19. </td> <td> <input type="text" name="installment19" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date19" class="tcal span4" /> </td></tr> <tr> <td> 20. </td> <td> <input type="text" name="installment20" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date20" class="tcal span4" /> </td></tr> <tr> <td> 21. </td> <td> <input type="text" name="installment21" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date21" class="tcal span4" /> </td></tr> <tr> <td> 22. </td> <td> <input type="text" name="installment22" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date22" class="tcal span4" /> </td></tr> <tr> <td>23. </td> <td> <input type="text" name="installment23" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date23" class="tcal span4" /> </td></tr> <tr> <td> 24. </td> <td> <input type="text" name="installment24" id="installment" class="span4" value="0" /> </td> <td> <input type="text" name="due_date24" class="tcal span4" /> </td></tr> <tr><td></td><td> <input type="submit" name="save" id="save" value="Save" class="btn btn-info span3" /> <input type="hidden" readonly="readonly" name="roll_no" value="<?php echo $roll_no; ?>" class="span5" /> </td><td></td></tr> </thead> </table> </form>Here i am not getting how to store it in that table. Actually all the 24 inputs are not mandatory. If user fills only 2 then also it should store it in the database table. I tried to do like this if(isset($_POST['save'])) { $roll_no = $_POST['roll_no']; $ins_amt1 = $_POST['installment1']; $due_date1 = $_POST['due_date1']; $ins_amt2 = $_POST['installment2']; $due_date2 = $_POST['due_date2']; $ins_amt3 = $_POST['installment3']; $due_date3 = $_POST['due_date3']; $ins_amt4 = $_POST['installment4']; $due_date4 = $_POST['due_date4']; $ins_amt5 = $_POST['installment5']; $due_date5 = $_POST['due_date5']; $ins_amt6 = $_POST['installment6']; $due_date6 = $_POST['due_date6']; $ins_amt7 = $_POST['installment7']; $due_date7 = $_POST['due_date7']; $ins_amt8 = $_POST['installment8']; $due_date8 = $_POST['due_date8']; $ins_amt9 = $_POST['installment9']; $due_date9 = $_POST['due_date9']; $ins_amt10 = $_POST['installment10']; $due_date10 = $_POST['due_date10']; $ins_amt11 = $_POST['installment11']; $due_date11 = $_POST['due_date11']; $ins_amt12 = $_POST['installment12']; $due_date12 = $_POST['due_date12']; $ins_amt13 = $_POST['installment13']; $due_date13 = $_POST['due_date13']; $ins_amt14 = $_POST['installment14']; $due_date14 = $_POST['due_date14']; $ins_amt15 = $_POST['installment15']; $due_date15 = $_POST['due_date15']; $ins_amt16 = $_POST['installment16']; $due_date16 = $_POST['due_date16']; $ins_amt17 = $_POST['installment17']; $due_date17 = $_POST['due_date17']; $ins_amt18 = $_POST['installment18']; $due_date18 = $_POST['due_date18']; $ins_amt19 = $_POST['installment19']; $due_date19 = $_POST['due_date19']; $ins_amt20 = $_POST['installment20']; $due_date20 = $_POST['due_date20']; $ins_amt21 = $_POST['installment21']; $due_date21 = $_POST['due_date21']; $ins_amt22 = $_POST['installment22']; $due_date22 = $_POST['due_date22']; $ins_amt23 = $_POST['installment23']; $due_date23 = $_POST['due_date23']; $ins_amt24 = $_POST['installment24']; $due_date24 = $_POST['due_date24']; $items = array(); $installment[] =array($ins_amt1, $ins_amt2, $ins_amt3, $ins_amt4, $ins_amt5, $ins_amt6, $ins_amt7, $ins_amt8, $ins_amt9, $ins_amt10, $ins_amt11, $ins_amt12, $ins_amt13, $ins_amt14, $ins_amt15, $ins_amt16, $ins_amt17, $ins_amt18, $ins_amt19, $ins_amt20, $ins_amt21, $ins_amt22, $ins_amt23, $ins_amt24); //$in_values= serialize($installment); $due_date[] = array($due_date1, $due_date2, $due_date3, $due_date4, $due_date5, $due_date6, $due_date7, $due_date8, $due_date9, $due_date10, $due_date11, $due_date12, $due_date13, $due_date14, $due_date15, $due_date16, $due_date17, $due_date18, $due_date19, $due_date20, $due_date21, $due_date22, $due_date23, $due_date24); //$in_dates= serialize($due_date); //$s1 = "insert into installment(id, fee_id, student_id, amount, due_date, paid_date, status, rec_no) values ('', ".$fee_id.", '".$roll_no."', ".$in_values.", '".$in_dates."', '', 'unpaid', ''"; foreach($installment as $row_key => $value) { $item = $value; $uom = $due_date[$row_key]; $items[] = sprintf("(%d, %d, '%s', %d, '%s', '%s', '%s', '%s')", '', $fee_id, mysql_real_escape_string($roll_no), intval($item), $uom, '', '', '' ); } $msql = 'INSERT INTO installment (id, fee_id, student_id, amount, due_date, paid_date, status, rec_no) VALUES '.implode(', ', $items);But it is taking only one data. Can somebody please suggest. The data has to be saved like this saveddata.JPG 70.92KB 0 downloads Somebody please help. Thank you To me it is logical to store config data such as site name, meta, etc in a php file rather than storing ina table in mysql database. This will reduce the number of calls to the mysql database, as these are key parameters and needed in every page. But, a well-developed CMS like Wordpress uses a mysql table to store such basic data. What is your idea? Ain't it better to reduce mysql loads? Hello everyone,
I have a card database and everything works perfectly besides one thing..
I can't store
'in my database via my form, i however do can store them into the database via PHPMyAdmin. I dont know what i've been doing wrong and it really bothers me. If any of you guys could help me out. Here's all the code you would need to find the issue. This is the form file <!doctype html> <html> <head> <meta charset="utf-8"> <title>Edit Page</title> </head> <body> <h1 align="center"> Add Cards</h1> <form action="insert.php" method="POST"> <input type="text" name="name" placeholder="Name" /> <input type="text" name="color" placeholder="Color" /> <input type="text" name="type" placeholder="Type" /> <input type="text" name="subtype" placeholder="Sub Type" /> <input type="text" name="power" placeholder="Power" /> <input type="text" name="toughness" placeholder="Toughness" /> <br> <input type="text" name="manacost" placeholder="Converted Mana Cost" /> <input type="text" name="rarity" placeholder="Rarity" /> <input type="text" name="expansion" placeholder="Expansion" /> <input type="text" name="foil" placeholder="Foil" /> <input type="text" name="stock" placeholder="Stock" /> <input type="submit" value="Save" /> </form> </body> </html>This inserts it into my database. <?php ($GLOBALS["___mysqli_ston"] = mysqli_connect("", "", "", , ))or die("cannot connect"); ((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE e_industries"))or die("cannot select DB"); $name = $_POST['name']; $color = $_POST['color']; $type = $_POST['type']; $subtype = $_POST['subtype']; $power = $_POST['power']; $toughness = $_POST['toughness']; $manacost = $_POST['manacost']; $rarity = $_POST['rarity']; $expansion = $_POST['expansion']; $foil = $_POST['foil']; $stock = $_POST['stock']; $sql="INSERT INTO Osiris (Name, Color, Type, Subtype, Power, Toughness, Manacost, Rarity, Expansion, Foil, Stock) VALUES ('$name', '$color', '$type', '$subtype', '$power', '$toughness', '$manacost', '$rarity', '$expansion', '$foil', '$stock')"; $result=mysqli_query($GLOBALS["___mysqli_ston"], $sql); if($result){ echo "Successful"; echo "<BR>"; echo "<a href='add.html'>Back to main page</a>"; } else { echo "ERROR"; } ?>If anyone could help me out that would be great! Edited by OsirisElKeleni, 05 October 2014 - 12:29 PM. |