输入数据不会出现在数据库中

时间:2015-07-10 15:28:09

标签: php mysql

我是这个论坛的新手,我的项目遇到了麻烦。我已经为其他表使用了相同的代码,但是这个代码不起作用。救命! d:

这不起作用:

<?php
$host="127.0.0.1"; 
$username="root"; 
$password=""; 
$db_name="nadel"; 
$tbl_name="soldprod"; 

mysql_connect("$host", "$username", "$password")or
die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$sprodname=$_POST['sprodname'];
$spquant=$_POST['spquant'];
$scli=$_POST['scli'];
$spds=$_POST['spds'];

if(empty($sprodname) || empty($spquant) || empty($scli) || empty($spds))
{
echo "<script> alert('You did not fill out the required fields. ');
 window.location.href='addsprod.php';</script> ";
}
else{

$sprodname = stripslashes($sprodname);
$spquant = stripslashes($spquant);
$scli = stripslashes($scli);
$spds = stripslashes($spds);

$sprodname = mysql_real_escape_string($sprodname);
$spquant = mysql_real_escape_string($spquant);
$scli = mysql_real_escape_string($scli);
$spds = mysql_real_escape_string($spds);


$sql="INSERT INTO soldprod(sp_name, sp_quantity, sp_cli_name, Date_sold)
 VALUES ('$sprodname','$spquant','$scli','$spds')";
$result=mysql_query($sql);

echo "<script> alert('Successfully Added Sold Product!');    
window.location.href='sprod.php';</script> ";
}
?>

但这有效:

<?php
$host="127.0.0.1"; 
$username="root"; 
$password=""; 
$db_name="nadel"; 
$tbl_name="products"; 


mysql_connect("$host", "$username", "$password")or
die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$prodname=$_POST['prodname'];
$pquant=$_POST['pquant'];
$pprice=$_POST['pprice'];
$pdman=$_POST['pdman'];
$pdex=$_POST['pdex'];



if(empty($prodname) || empty($pquant) || empty($pprice) || 
empty($pdman)|| empty($pdex))
    {
        echo "<script> alert('You did not fill out the required fields. '); 
     window.location.href='addprod.php';</script> ";
    }
    else{


    $prodname = stripslashes($prodname);
    $pquant = stripslashes($pquant);
    $pprice = stripslashes($pprice);
    $pdman = stripslashes($pdman);
    $pdex = stripslashes($pdex);

    $prodname = mysql_real_escape_string($prodname);
    $pquant = mysql_real_escape_string($pquant);
    $pprice = mysql_real_escape_string($pprice);
    $pdman = mysql_real_escape_string($pdman);
    $pdex = mysql_real_escape_string($pdex);

    $sql="INSERT INTO products(`product_name`, `prod_quantity`,
    `prod_price`, `prod_manD`, `prod_expD`) VALUES 
   ('$prodname','$pquant','$pprice','$pdman','$pdex')";
    $result=mysql_query($sql);


    echo "<script> alert('Successfully Added Product! ');
    window.location.href='prod.php';</script> ";
    }
    ?>

我不知道出了什么问题D:

2 个答案:

答案 0 :(得分:2)

$sql="INSERT INTO soldprod(sp_name, sp_quantity, prod_quant,
sp_cli_name, Date_sold) VALUES     
('$sprodname','$spquant','$scli','$spds')";

您指定5列,然后只输入4个值。

您应该检查数据库调用的结果并检查mysql_error以找出问题所在。

请考虑从mysql_ *迁移到mysqli或PDO。不推荐使用mysql_ *函数,将来会删除它们。

答案 1 :(得分:0)

这里有5个cols但是4个值:

INSERT INTO soldprod(sp_name, sp_quantity, prod_quant,
sp_cli_name, Date_sold) VALUES     
('$sprodname','$spquant','$scli','$spds')