当我尝试将我的 prdocutPrice 字符串或股票字符串转换为十进制和整数值时,我在将其插入我的数据库时遇到问题。我很确定我正在做其余的事情,有人可以为我确认吗?
<?php
if (isset($_POST['addSubmitted'])) {
$errors = array();
require_once ('mysql_connect.php');
//This gets all the other information from the form
$name=$_POST['productName'];
$description=$_POST['productDescription'];
$price= floatval($_POST['productPrice']);
$stock= intval($_POST['productStock']);
if (empty($errors)) {
//Writes the information to the database
mysql_query("INSERT INTO products (name, description, price, stock) VALUES ($name, $description, $price, $stock)");
$result = mysql_query($query);
if (mysql_affected_rows() == 1) {
// Show thank you message
echo '<span style="color:green;">Your product has been added.</span>';
} else {
echo '<font color="red">We were unable to add your product to the database.</font>';
}
} else {
echo '<font color="red"><h3>Error!</h3>
The following error(s) occured:<br /></font>';
foreach ($errors as $msg) {
echo " - <font color=\"red\">$msg</font><br />\n";
}
}
}
?>