makaka
makaka

Reputation: 11

posting data into two database tables

I am trying to post data into two databases namely add_product and ledger but I keep getting the following error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, amount)values('mnbvx', 'opening stock', '5')' at line 1

Below is my PHP code:

   <?php
include ('session.php');
if($_GET["name"] && $_GET["opening"]&& $_GET["re"])
{

$servername="localhost";
$username="root";
$db="multiple";
$pass="";
mysql_connect($servername,$username)or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$name=$_GET["name"];
$op=$_GET["opening"];
$re=$_GET["re"];
mysql_query("insert into add_product (product, opening, current,     reorder)values('$name', '$op', '$op', '$re')");

mysql_query("insert into ledger (product, desc, amount)values('$name',   'opening stock', '$op')")or die(mysql_error());


print "<h4>product added to database</h4>";
print "<p><a href='add_pdt.html'>add another?</a></p>";
print "<a href='index_admin.php'>home</a>";
}
else{
print "please fill all fields correctly";
print " ";
print "<p><a href='add_pdt.html'>back</a></p>";
print "<p><a href='index_admin.php'>home</a></p>";

}
?>



below is - Table structure for table `ledger`
CREATE TABLE IF NOT EXISTS `ledger` (
`product` varchar(60) NOT NULL,
`desc` varchar(60) NOT NULL,
`amount` varchar(15) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


below --
-- Table structure for table `add_product`

CREATE TABLE IF NOT EXISTS `add_product` (
`product` varchar(30) NOT NULL,
`id` int(30) NOT NULL,
`opening` int(100) NOT NULL,
`current` int(100) NOT NULL,
`reorder` int(6) NOT NULL,
`updateon` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=93 ;



--

Upvotes: 1

Views: 60

Answers (3)

Bhavin Shah
Bhavin Shah

Reputation: 90

desc is resevered ... and mysql is depricated... use

$con = ('localhoat','root','','database_name');
mysqli_query($con,"insert into ledger (`product`, `desc`, `amount`)values('$name','opening stock', '$op')")or die(mysql_error()");

Upvotes: 0

Ruban Kumar
Ruban Kumar

Reputation: 31

Use this query instead of your second query.

mysql_query("insert into ledger (`product`, `desc`, `amount`)values('$name',   'opening stock', '$op')")or die(mysql_error());

Upvotes: 1

Gerald Schneider
Gerald Schneider

Reputation: 17797

DESC is a reserved keyword. Either use backticks around it:

`desc`

or rename it.

Upvotes: 0

Related Questions