【问题标题】:val converting to int value [closed]val 转换为 int 值
【发布时间】:2012-04-15 02:52:22
【问题描述】:

你好,我有一个表格可以将小数点转换为整数。哪里有问题

 <td>
    <input type='text' name='item[$i][Price]' id='Pquantity' value='".htmlspecialchars($row['Price'])."' readonly>

这是下面的表格,数据是从上面填写的表格中提取的

<?php
$submit = $_POST['Add'];

//form data
$Sname = mysql_real_escape_string(htmlentities(strip_tags($_POST['Sname'])));
$Pname = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pname'])));
$Pidno = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pidno'])));
$Psize = mysql_real_escape_string(htmlentities(strip_tags($_POST['Psize'])));
$Pcolour = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pcolour'])));
$Pquantity = $_POST['Pquantity'];
$Weblink = mysql_real_escape_string(htmlentities(strip_tags($_POST['Weblink'])));
$Price = mysql_real_escape_string(htmlentities(strip_tags($_POST['Price'])));
$date = date("Y-m-d");


//echo " ('','$Sname','$Pname','$Pidno','$Psize','$Pcolour','$Pquantity','$Weblink','$Price','$Uname')";
if('POST' === $_SERVER['REQUEST_METHOD']) 

{
if ($Sname&&$Pname&&$Pidno&&$Weblink&&$Price)
{
if (is_numeric($Price))
{
    $repeatheck = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}' AND Pidno ='$Pidno' AND Sname='$Sname'");
    $count = mysql_num_rows($repeatheck);
if($count!=0)
{
    die ('PRODUCT ALREADY IN BASKET YOU CAN INCREASE OR DECREASE QUANTITY');
}
else
//echo'$Price';
$tprice = $Price * $Pquantity;
//echo"$tprice";
$queryreg = mysql_query("
INSERT INTO repplac VALUES ('','$Sname','$Pname','$Pidno','$Psize','$Pcolour','$Pquantity','$Weblink','$Price','$tprice','$date','{$_SESSION['username']}','')
")or die(mysql_error());
}
else
echo 'price field requires numbers';
}
else
echo 'please fill in all required * fields ';
}
?>
<form action='youraccount.php' method='Post' class='ilistbar'>
    <!--<div>
    <label for='shoppinglist' class='fixedwidth'></label>
    <textarea type='text' name='shoppinglist' id='username' cols='100' rows='15'></textarea>
    </div> -->
    <div>
    <label for='Sname' class='fixedwidth'> * Shop name</label>
    <input type='text' name='Sname' id='Sname'/>
    </div>
    <div>
    <label for='Pname' class='fixedwidth'> * Product name</label>
    <input type='text' name='Pname' id='Pname'/>
    </div>
    <div>
    <label for='Pidno' class='fixedwidth'> * Product id no /ad reference</label>
    <input type='text' name='Pidno' id='Pidno'/>
    </div>
    <div>
    <label for='Psize' class='fixedwidth'>Product size</label>
    <input type='text' name='Psize' id='Psize'/>
    </div>
    <div>
    <label for='Pcolour' class='fixedwidth'>Product colour</label>
    <input type='text' name='Pcolour' id='Pcolour'/>
    </div>
    <div>
    <label for='Pquantity' class='fixedwidth'>Product quantity</label>
    <select name="Pquantity" id="Pquantity">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option> 
</select>
(You can update quantity in excess of 10 on the shopping list below)
    </div>
    <div>
    <label for='Weblink' class='fixedwidth'> * Web link</label>
    <input type='text' name='Weblink' id='Weblink'/>
    </div>
    <div>
    <label for='Price' class='fixedwidth'> * Price GBP</label>
    <input type='text' name='Price' id='Price'/>
    </div>
    <div>

    <div class='buttonarea'>
            <p>
            <input type='submit' name='submit' value='Add'>
            </p>
            </div>
            </p>
    </form>
</div>
</div>
</div>
<div class="primary">
<div class="action-box rounded">
<div class="titlebar">
<h2>Shopping List</h2>
<!--<a href='totalprice.php'>Update</a>-->
</div>
<div class="listbar">

    <form action='orderpplac.php' method='Post' class='shlistbar'>
    <table border='1'>
    <tr>
    <th>SHOP NAME</th>
    <th>PRODUCT NAME</th>
    <th>PRODUCT SIZE</th>
    <th>PRODUCT COLOUR</th>
    <th>PRODUCT QUANTITY</th>
    <th>PRICE</th>
    <th>TOTAL</th>
    <th></th>
    </tr>
    <?php
    // Get DB results and loop, outputting table rows with counter
    $pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'") or die(mysql_error());
   for ($i = 0; $row = mysql_fetch_assoc($pplresult); $i++) {
    echo "
    <tr>
    <td>".htmlspecialchars($row['Sname'])."</td>
    <td>".htmlspecialchars($row['Pname'])."</td>
    <td>".htmlspecialchars($row['Psize'])."</td>
    <td>".htmlspecialchars($row['Pcolour'])."</td>
    <td>
    <input type='text' name='item[$i][Pquantity]' id='Pquantity' value='".htmlspecialchars($row['Pquantity'])."' />
    <input type='hidden' name='item[$i][Pidno]' id='Pidno' value='".htmlspecialchars($row['Pidno'])."' />
    </td>
    <td>
    <input type='text' name='item[$i][Price]' id='Pquantity' value='".htmlspecialchars($row['Price'])."' readonly>
    </td>
    <td>".htmlspecialchars($row['Tprice'])."</td>
    <td><a href='deleteproduct.php?del=".htmlspecialchars($row['Pidno'])."'>delete</a></td>
    </tr>";
    }
   $pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'") or die(mysql_error());
  while ($row = mysql_fetch_assoc($pplresult)) 
   //echo $row['Pquantity'] * $row['Price'];
  {
  $totalprice += $row['Tprice'];
  }
  //echo "$totalprice";

         ?>
         <tr>
                <th>Total Price</th>
                <th><?php echo $totalprice; ?></th>

            </tr>
    <!--Close table and form-->

    </table>
    <input type='submit' name='submit1' value='UPDATE' />
    <input type='submit' name='submit2' value='SUBMIT' />
    </form>

【问题讨论】:

  • 你在用什么locale?变量$Price 来自哪里?你还在用register_globals吗?将浮点数转换为整数不会对浮点数进行四舍五入,因此 (int)3.85 == 3.
  • @Basti 我不希望脚本太长,但我已经编辑了它,它是从提交的表单中提取的
  • 所以你的代码中有$Price = $_POST["Price"]?价格是否正确插入表格?另请阅读stackoverflow.com/questions/7302834/…
  • 是的,有 $Price = mysql_real_escape_string(htmlentities(strip_tags($_POST['Price'])));
  • 哦,我得到了线索:保存价格的列不是整数类型吗?如果是这样,插入3.85 会将值四舍五入为4 并引发警告。

标签: php


【解决方案1】:

使用表格

CREATE TABLE `test` (
  `int` int(11) NOT NULL,
  `float` float NOT NULL,
  `decimal` decimal(2,1) NOT NULL
)

并在每列中插入值“3.85”

INSERT INTO `test` (`int`, `float`, `decimal`) VALUES ('3.85', '3.85', '3.85');

会导致输出

int   float   decimal
4     3.85    3.9

插入时会得到一个

Note: #1265 Data truncated for column 'decimal' at row 1

要修复此问题,请执行此操作

ALTER TABLE `repplac` CHANGE `Price` `Price` DECIMAL(12, 2) NOT NULL 

DECIMAL(12, 2) 类型最多可以存储 9999999999,99 的值。

更多信息请参见MySQL's manual on DECIMAL

注意输入“3,85”(用逗号代替点)会导致 MySQL 忽略任何小数位。在插入之前,您可能需要 str_replace(',', '.', $Price) 并将 , 转换为 .,或者向用户显示他使用无效格式的警告。

【讨论】:

  • 好的,我会阅读并回复,谢谢
  • 没有得到最后的评论。第一个数字 (12) 是数字的总长度,即 12.34 的长度为 4。第二个数字 (2) 是小数位数,即 2 代表 12.344 代表 1.2345
  • 太棒了!不要让那些反对票影响到你。在 stackoverflow 上似乎是一天中的错误时间。 ;-)
猜你喜欢
  • 2018-03-19
  • 1970-01-01
  • 2021-11-04
  • 2019-10-16
  • 2015-04-30
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多