Jump to content

Data are not inserted into database where is the problem in this code?


AhmedLazrak
Go to solution Solved by benanamen,

Recommended Posts

Hello everyone!
For a home project I have to create a database and a table inside.
Then let users insert data into table via simple html form.
This form uses a php file to do the inserting, the form is just to get data.
I'm using MyphpAdmin in wampserver to create database and table manually.

here is my code:

in the first file for connecting to server and selecting database

<?php
$server="localhost";
$user= "root";
$password="";

$connect=@MySQL_Connect($server,$user,$password) or die ("connection failed!");

echo "Server Access granted";

$base= "Mabase";
MySQL_Select_db($base);

echo "<br>Mabase was selected"
?>

===================
the second file is a simple html form
===================
 

<html>
<head>
<title>Formulaire de Saisie</title>
</head>
<body>
<form action = "traiter.php" method = "POST">
Enter Code : <input type = "text" name = "Code"></input><br>
Enter Designation : <input type = "text" name = "Des"></input><br>
Entrez Prix Unitaire : <input type = "text" name = "PU"></input><br>
Entrez Quantite en stock : <input type = "text" name = "QS"></input><br>
<input type = "submit" value = "valider">
</form>
</body>
</html>

====================
the third file is traiter.php that you see in the html form
====================
 

<?php
require ("choixBD.php");

$code = $_POST["Code"]; // product code
$des = $_POST["Des"]; // meaning
$pu = $_POST["PU"]; // unie price
$qs = $_POST["QS"]; // quantity
$pttc = $pu*1.2; // TAX

$nomtable = "Produit";
$req = "INSERT INTO $nomtable (Code,Designation,Quantite,PrixUnitaire,PTTC) VALUES('$code','$des,'$qs','$pu','$pttc')";
$result = @MySQL_Query($req);

if (!$result) {
echo "<br> impossible to execute ($req) in db".@MySQL_error();
exit;
}
echo"<br>Values have been added";
?>


=====================
the idea is that the user will open the form file and enter data and click submit and data should be added
the problem is that data are not added and I don't know why. here is the error message

impossible to execute (INSERT INTO Produit (Code,Designation,Quantite,PrixUnitaire,PTTC) VALUES('s425','gaming mouses,'190','1300','1560')) in dbYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '190','1300','1560')' at line 1

Thanks in advance :)

Link to comment
Share on other sites

  • Solution

First, you are using obsolete Mysql code that will not work at all in the latest version of Php. You need to use PDO with prepared statements.

 

Second, get rid of all the @'s. DO NOT SUPPRESS ERRORS. Errors are your friend, they tell you when something is wrong.

 

And lets not forget about you jumping case all over the place. Always use lower case names.

 

Why is your table name a variable? Are you going to insert those exact column names into more than one table?

 

In your error message there is a missing quote.

Edited by benanamen
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.