du4629 2018-06-11 15:32
浏览 93
已采纳

将十进制数存储到MySQL数据库中

Here is the return of AJAX, which display what value I am trying to store into database:

enter image description here

Here is what is really stored into database (58.00 is what I stored):

enter image description here

Here is the structure of table:

enter image description here

Here is the PHP:

$req = $bdd->prepare("INSERT INTO salon_histo(reference, designation, colour, size, type, price, qty, payment, date) VALUES(:reference, :designation, :colour, :size, :type, :price, :qty, :payment, NOW())");
    $req->execute(array(
        'reference' => $_POST['reference'],
        'designation' => $_POST['designation'],
        'colour' => $_POST['colour'],
        'size' => $_POST['size'],
        'type' => $_POST['type'],
        'price' => floatval($price[0]),
        'qty' => $_POST['soldQty'],
        'payment' => $_POST['payment']
    ));
    $req->closeCursor();
    echo json_encode($price[0]);

How can MySQL store a data with 2 decimals at 0 when I am trying to store 58,33? I tried in PHP to use floatval and indeed the number becomes 58.

  • 写回答

1条回答 默认 最新

  • duanhuanyou6478 2018-06-11 15:36
    关注

    Price appears to be an array:

    'price' => floatval($price[0]),
    

    Possibly the decimal part has key 1?

    'price' => (float) ($price[0] . '.' . $price[1]), 
    

    If that doesn't work, please var_dump($price) and paste me the result.

    The other thing to check is wether PHP is formatting those numbers with a comma or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化