Question

not working insert to linux

Good afternoon, I have a small problem with my digital ocean server, when I upload my script to run it, the inserts from my site do not work, I have done everything but I don’t know what to do now, I show a screenshot of the small test code that have

<?php 

 $dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "PassowrdHere";
 $db = "DBHere";


 $con = new mysqli($dbhost, $dbuser, $dbpass,$db, 3306);


	echo "<pre>";
	echo $con->host_info . "\n";
	echo $con->connect_errno . "\n";

			/*	$query = "INSERT INTO `empleados`(`cedula`) VALUES ('123')";
				$insert = mysqli_query($con, $query);*/

				$query = "INSERT INTO `empleados`(`cedula`) VALUES ('123')";

				$insert = $con->query($query);

				var_dump($insert);

				if (!$insert) {
					$response = array(
						'icon' => 'error',
						'mensaje' => 'No fue posible guardar el nombre del rol.', 
					);
				}else{
					$response = array(
						'icon' => 'success',
						'mensaje' => 'El usuario se creo correctamente.', 
					);
				}
		
		
	echo json_encode($response);
?>

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

It is already solved, it was that in the database table, all the information of the insert had to be inserted, it did not accept null values, so I have to put it to accept null values

KFSys
Site Moderator
Site Moderator badge
August 10, 2021

Hi @sistemasalianzamei,

There are multiple reasons why you won’t be able to insert into or connect to a database at all. Your Database user might not have the proper permissions for your database, your DB password might be wrong, your script might not have the proper permissions, or you don’t have proper permissions to connect to MySQL outside of your Droplet.

In this case, you are using the user Root so the DB permission automatically is not applicable as the root user has access to everything.

What I’ll recommend you is to try and connect using the same credentials from CLI - command line interface. Basically, SSH to your Droplet and type in

mysql -u root -p votaciones_sas

Then enter your password and see if you were able to connect.

As I’m looking at your script, it seems you are connecting externally, not from inside the droplet, correct? If that’s the case, you may need to allow your IP Address on your Droplet on port 3306 to be able to connect.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more