-1

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:

mikai
  • 1
  • 1
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 10 '15 at 15:31
  • What do you mean by "it doesn't work"? – Jay Blanchard Jul 10 '15 at 15:32
  • 1
    Sorry, I'll study and try using prepared statements. – mikai Jul 10 '15 at 15:38
  • When I try to save data from the form it doesn't go to the database. :( – mikai Jul 10 '15 at 15:39
  • Since you're doing zero error checking here, anything could be going wrong and you'd have no idea. PDO supports raising exceptions when something goes wrong so you can't ignore errors by mistake. – tadman Jul 10 '15 at 15:44

2 Answers2

1
$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.

Jim
  • 22,354
  • 6
  • 52
  • 80
  • I didn't know about PDO until now sorry, I'm studying it now. Thanks :D I've removed the excess column, but it still doesn't work :( – mikai Jul 10 '15 at 15:47
0

You have 5 cols but 4 values here:

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