Unable to insert information in MySql database

Hello everyone,
first of all, here is my username as it is recommended to share: epiz_25903923

Situation
I recently restarted to code in php for a website that I want to connect to a database. Everything was working perfectly on my local environment with WAMP Server.
I was able to create a database via the Infinityfree control panel, inputed some data inside, was able to connect this database to my php that I uploaded on my website hosted by Infinityfree. Everything was fine until I tried to INSERT data to my database.
I spent the last 3 days looking everywhere, but I canā€™t find the solution to my problem.

Database
Here is the structure of my database (I start with an easy one, also tried with id but this did not seem to matter here) :

CREATE TABLE test (name varchar(256) not null);

PHP code

<?php

$dbServername = "sql101.epizy.com";
$dbUsername = "epiz_xxxxxxx";
$dbPassword = "xxxxxxxxx";
$dbName = "epiz_xxxxxxx_test";

$conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);

$name="Bob";

$sql = "INSERT INTO test (name) VALUES ($name);";

$worked=mysqli_query($conn,$sql);

if (!$worked) 
{
    echo "Error : cannot add to database";
}
else
{
    echo "no problem";
}      
?>

Error message

I activated the Alter PHP config on the Control Panel, because it might help to debug in the future.
However, with this simple code I only get the "Error : cannot add to database" message and in fact no row is added to my databaseā€¦

This code and even the more complicated codes I prepared for my website do work on WAMP on my local environement, but this simple one does not work once on Infinityfree. However I can get data from the database ā€¦ I donā€™t get it :sweat_smile:

Thank you very much if you read all this and I pray for someone to help me find the answer, Iā€™m so sad right now ^^

Hello @Gyoumu! I solved your problem. Here is the final code:

<?php $dbServername = "sample"; $dbUsername = "sample"; $dbPassword = ""; $dbName = "sample";
 $conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName); $name="Bob"; 

$sql = "INSERT INTO test(name) VALUES ('$name');"; 

$worked= mysqli_query($conn,$sql); 

if (!$worked) { echo "Error : cannot add to database"; } else { echo "no problem"; } 

?>

Pls be careful to php syntax.

2 Likes

The problem is here in this line:

So I replaced them with:
$sql = ā€œINSERT INTO test(name) VALUES (ā€˜$nameā€™);ā€;

2 Likes

To see the error, it must be:

echo "Error : cannot add to database: ".mysqli_errno($conn);

I guess the code should look like this:

INSERT INTO `test` (name) VALUES ('".$name."');

And test table must exist.

5 Likes

@JavesPotato @anon19508339,

thank you so much ! It is now working perfectly, it turned out to be what you said : a simple syntax issue ā€¦ Anyway, thank you so much you saved my day !

Talk to you soon,

Gyoumu

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.