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
Post a Comment