doucai6663 2015-07-10 15:28
浏览 49

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

I'm new to this forum, and I'm having trouble with my project. I've used the same code for the other tables but this one won't work. Help! D:

This doesn't work:

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

But this works:

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

I don't know what went wrong D:

  • 写回答

2条回答 默认 最新

  • dongmei8760 2015-07-10 15:39
    关注
    $sql="INSERT INTO soldprod(sp_name, sp_quantity, prod_quant,
    sp_cli_name, Date_sold) VALUES     
    ('$sprodname','$spquant','$scli','$spds')";
    

    You specify 5 columns then only enter 4 values.

    You should be checking the results of database calls and checking mysql_error to find out what the issue is.

    Please consider moving from mysql_* to mysqli or PDO. The mysql_* functions are deprecated and will be removed in the future.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀