Forum Moderators: coopster

Message Too Old, No Replies

No insert in MySQL

Should be basic but....

         

henry007

7:00 am on Mar 28, 2025 (gmt 0)

Top Contributors Of The Month



Hello Coopster, I am back after so many years (Was henry0)
I can't feed my DB with the following script
I did echo every values, no problen there, an error is said to happen at
<<
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '-ingredient, nbrpers, ingredients, methode) values(''', '', '', '', '', '')' at line 1 in C:\xampp\htdocs\RCP\rcp-form\post-rcp.php:81 Stack trace: #0 C:\xampp\htdocs\RCP\rcp-form\post-rcp.php(81): mysqli_query(Object(mysqli), 'INSERT INTO rcp...') #1 {main} thrown in
>>
athough I connect sucessfully
CODE
$conn = mysqli_connect($servername, $username, $password, $db);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else
{
echo "Connected successfully";
}

$sql = "INSERT INTO rcp (cate, titre, main-ingredient, nbrpers, ingredients, methode)
values('$cate'', '$titre', '$mainingredient', '$nbrpers', '$ingredients', '$methode')";

XXX Here is said to be the problem
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

By advance thanks all

henry007

12:59 pm on Mar 28, 2025 (gmt 0)

Top Contributors Of The Month



EDIT:
I changed $conn to PDO for a broader reach
no problem there
I even change the var values in insert to plain text
everything looks fine, no error, if I query the DB insert il also looks fine
But nothind is entered in the table?
thanks

topr8

4:22 pm on Mar 28, 2025 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>>values('$cate'',

first you use a single quote: '
second you use double quote: "

henry007

4:45 pm on Mar 28, 2025 (gmt 0)

Top Contributors Of The Month



Thanks, I figured it out
but it didn't change anythyng
what did the trick was to replace the end of script by:
<<<
if (mysqli_query($conn, $sql)) {
echo "Nouveau enregistrement créé avec succès";
} else {
echo "Erreur : " . $sql . "<br>" . mysqli_error($conn);
}
>>>

henry007

5:12 am on Apr 8, 2025 (gmt 0)

Top Contributors Of The Month



Thanks but you have seen just a short, as I do among other stuffs
$cate = filter_input(INPUT_POST, 'cate', FILTER_SANITIZE_SPECIAL_CHARS);

henry007

5:05 pm on Apr 8, 2025 (gmt 0)

Top Contributors Of The Month



Thanks, I am already using PDO
and what's sanitized is turned only in HTML file
But I might if needed use m_r_e_s