将项目插入数据库

时间:2012-04-28 16:23:18

标签: php mysql sql

我在将此插入我的数据库时遇到问题我认为当我尝试将prdocutPrice字符串或stock字符串转换为decimal和int值时。我很确定我正在休息,有人能为我确认吗?

<?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";
            }
        }
    }

?> 

5 个答案:

答案 0 :(得分:5)

INSERT语句中没有正确引用任何字符串值。用单引号括起来。

此外,第一件事是第一件事 - 确保在所有字符串输入值上调用mysql_real_escape_string(),因为它们目前容易受到SQL注入攻击。

$name = mysql_real_escape_string($_POST['productName']); 
$description= mysql_real_escape_string($_POST['productDescription']); 
$price= floatval($_POST['productPrice']); 
$stock= intval($_POST['productStock']);

此外,您正在调用mysql_query()两次而不是将SQL字符串存储到变量$query中。

// Quote the string values,
// store the SQL as a variable then pass it to mysql_query()
$query = "INSERT INTO products (name, description, price, stock) VALUES ('$name', '$description', $price, $stock)"; 
$result = mysql_query($query);

调用echo mysql_error();会有助于调试SQL语句的问题。

最后,我还要添加一个注意事项 - 除了在intval()传递的字符串上调用floatval()$_POST之外,通常最好先验证数字是否为if (is_numeric($_POST['productPrice'])) { $price = floatval($_POST['productPrice']); } else // non numeric value, don't do the insert with bad data 实际上数字。否则,如果它们是非数字值,它们将被强制转换为0,当您可能根本不应该插入它时,您将在数据库中获得零(因为它是无效数据)。

if (ctype_digit($_POST['productStock'])) {
  $stock = intval($_POST['productStock']);
}
else // bad input value, don't do insert

对于正整数或零整数,我喜欢使用ctype_digit()

{{1}}

答案 1 :(得分:1)

要回答你的问题,我相信你需要围绕值(尤其是字符串)引用:

mysql_query("INSERT INTO products (name, description, price, stock) VALUES ('$name', '$description', '$price', '$stock')");

但您还需要使用mysql_real_escape_string转义变量,或切换为mysqliPDO以使用参数化查询。

答案 2 :(得分:1)

1)你很容易受到SQL注入攻击。在继续使用任何代码之前,请仔细阅读http://bobby-tables.com 2)您在查询中忽略了引号,导致语法错误。您也没有任何错误处理,并假设您的查询成功:

mysql_query("INSERT INTO products (name, description, price, stock) VALUES ('$name', '$description', etc....) 
                                                                            ^     ^

请注意其他引号。至于错误处理,你应该总是使用的绝对最低限度是

 $result = mysql_query(...) or die(mysql_error());

即使您的查询字符串绝对完全有效(而您的查询字符串绝对不是),查询无法 NOT 检查成功/失败的原因还有很多其他原因。

答案 3 :(得分:0)

if(empty($ errors)){         //将信息写入数据库         mysql_query(“INSERT INTO产品(名称,描述,价格,库存)VALUES($ name,$ description,$ price,$ stock)”);         $ result = mysql_query($ query);

这不应该是:

if(empty($ errors)){         //将信息写入数据库          $ query =“INSERT INTO产品(名称,描述,价格,库存)VALUES($ name,$ description,$ price,$ stock”;         $ result = mysql_query($ query);

答案 4 :(得分:0)

  

mysql_query(“INSERT INTO产品(名称,描述,价格,库存)VALUES($ name,$ description,$ price,$ stock)”);

您没有为mysql_query声明变量。此外,迈克尔有一些关于逃避字符串值的好点。 尝试使用 $ query 变量和值周围的单引号。

$query = mysql_query("INSERT INTO products (name, description, price, stock) VALUES ('$name', '$description', '$price', '$stock')"); 
相关问题