php - input field: store decimal values with comma instead of a point in mysql database -


i have input field price. entered value stored database, example 13,99.

in mysql database field has type decimal(10,2). happens price stored correctly database if user types 13.99 point instead of comma. if user types 13,99 stored 13.00.

what should change in code, user can type 13,99 , stored correctly database?

<?php ...     if ($valid) {         $pdo = database::connect();         $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception);         $sql = "update products  set price =? id = ?";         $q = $pdo->prepare($sql);         $q->execute(array($price,$id));         database::disconnect();         header("location: index.php");     } } else {     $pdo = database::connect();     $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception);     $sql = "select * products id = ?";     $q = $pdo->prepare($sql);     $q->execute(array($id));     $data = $q->fetch(pdo::fetch_assoc);     $price = $data['price'];      database::disconnect(); } ?> ...    <div class="control-group <?php echo !empty($priceerror)?'error':'';?>">                     <label class="control-label">preis</label>                     <div class="controls">                         <input class="form-control" name="price" type="text"  placeholder="price number" value="<?php echo !empty($price)?$price:'';?>">                         <?php if (!empty($priceerror)): ?>                             <span class="help-inline"><?php echo $priceerror;?></span>                         <?php endif;?>                     </div>                   </div> 

try

...  if ($valid) {         $price = str_replace(",",".",$price);         $pdo = database::connect();         $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception);         $sql = "update products  set price =? id = ?";         $q = $pdo->prepare($sql);         $q->execute(array($price,$id));         database::disconnect();         header("location: index.php");     } } else {     $pdo = database::connect();     $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception);     $sql = "select * products id = ?";     $q = $pdo->prepare($sql);     $q->execute(array($id));     $data = $q->fetch(pdo::fetch_assoc);     $price = $data['price'];      database::disconnect(); } ... 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -