Greetings!

I have what I guess is a simple problem with a simple php code. What I want, is that with a simple form, the name and the password that are entered, are saved in a database. I have a form, and the action ="Process_Form.php" calls this file. Then, with $_POST array I can retrieve the data. With these data saved in variables, I want to move them to a database. Here's the problem. I'll show you both codes, and I hope you can see the mistake.


Code from the form:

<?
echo "<form id='form1' name='form1' method='post' action='Procesar_Orden.php'>
  <label>Usuario
    <input type='text' name='Usuario' id='Usuario' />
    Contraseña
    <input type='text' name='Contraseña' id='Contraseña' />
  </label>
  <label>
    <input type='submit' name='Submit' id='Submit' value='Submit' />
  </label>
</form>";
?>

Code from the php script that tries to save the data in a db:

<?
$Usuario = $_POST['Usuario'];
$Contraseña = $_POST['Contraseña'];
$dbname = "Freevids";
$usrname = "root";
$psswd = "asdfghjkl9";
$host = "localhost";
$Die_message = "No se ha podido conectar con la base de datos";
$Conexion = mysqli_connect($host,$usrname,$psswd,$dbname) or die($Die_message);
$Query = "INSERT INTO Usuarios VALUES ('Hola', 'Buuu')";

if (mysqli_query($Conexion,$Query))
	{
		echo "El usuario ha sido incluido exitosamente en la base de datos";
	}
else
	{
		echo "No se ha podido agregar el usuario a la base de datos.";
	}
?>

The result of all of this is "No se ha podido agregar el usuario a la base de datos." which is the message It displays when the info couldn't be added. Please help! Thanks!

At a guess, I assume your table `Usuarios` has more than just two columns. If so, on line 10, you need to specify the fields for which you're providing values.

E.g.

INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

If that still fails, run the query on line 10 in your MySQL console to see whether it results in an error.

R.

At a guess, I assume your table `Usuarios` has more than just two columns. If so, on line 10, you need to specify the fields for which you're providing values.

E.g.

INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

If that still fails, run the query on line 10 in your MySQL console to see whether it results in an error.

R.

I believe what the original poster has done is valid syntax although is bad practise to do so. So you are right that you *should* include the column names but mysql *can* recognize the syntax without column names. Conclusion to that is I would add the column names like blocblue suggested. However the real bug in this code is the following line.

if (mysqli_query($Conexion,$Query))

That is not a valid if statement. You need to assign mysqli_query to a variable and pass one of the variables methods through the if statement to be able to get a true/false statement out of it. I myself don't really use the mysqli module much but I'm sure it would all be in the manual at php.net

Just a note, if you have little knowledge in OOP, PDO is great!

I believe what the original poster has done is valid syntax although is bad practise to do so. So you are right that you *should* include the column names but mysql *can* recognize the syntax without column names. Conclusion to that is I would add the column names like blocblue suggested. However the real bug in this code is the following line.

if (mysqli_query($Conexion,$Query))

That is not a valid if statement. You need to assign mysqli_query to a variable and pass one of the variables methods through the if statement to be able to get a true/false statement out of it. I myself don't really use the mysqli module much but I'm sure it would all be in the manual at php.net

Alright, I think I understand what you mean. But there are other codes, where, without having to save the mysqli_query into a variable, I can do an If statement. Example:

<?

$db_query = "CREATE DATABASE Freevids";

$peliculas_table_query = "CREATE TABLE Peliculas 
(
 Nombre varchar(255) NOT NULL,
 Genero varchar(255) NOT NULL,
 Reparto varchar(255) NOT NULL,
 Sinopsis varchar(255) NOT NULL
)";

$insert_query = "INSERT INTO Peliculas (Nombre, Genero, Reparto, Sinopsis) VALUES ('Trecientos', 'Guerra', 'Brad Pitt', 'Guerra de los atenienses vs los persas y sus legendarios guerreros.')";

$alter_query = "ALTER TABLE Peliculas ADD Id Serial FIRST";

$usuarios_table_query = "CREATE TABLE Usuarios
(
 Id serial NOT NULL,
 Usuario varchar(255) NOT NULL,
 Contraseña varchar(255) NOT NULL,
 Email varchar(255) NOT NULL,
 Genero varchar(255) NOT NULL
)";

$insert_usuario = "INSERT INTO Usuarios (Id, Usuario) VALUES ('Hola', 'Buuu')";

$host = "localhost";
$dbname = "Freevids";
$pswd = "asdfghjkl9";
$acnt = "root";

$conexion = mysqli_connect($host,$acnt,$pswd) or die($die_message);

$die_message = "El servidor no ha podido ser comunicado. Porfavor intente mas tarde o asgurese de insertar bien los datos!";

if (mysqli_query($conexion,$db_query))
	{
		echo "Base de datos creada exitosamente";
	}
else
	{
		echo "No se ha podido crear la base de datos";
	}

if (mysqli_select_db($conexion,$dbname))
	{
		echo "Se ha conectado con la base de datos correcta";
		$conexion = mysqli_connect($host,$acnt,$pswd,$dbname);
	}
else
	{
		echo "No se ha conectado con la base de datos";
	}

if (mysqli_query($conexion,$peliculas_table_query))
	{
		echo "Se ha creado la tabla Peliculas correctamente";
	}
else
	{
		echo "No se ha podido crear la tabla Peliculas";
	}

if(mysqli_query($conexion,$insert_query))
	{
		echo "Se han agregado los valores correctamente";
	}
else
	{
		echo "No se han podido agregar los valores!";
	}
	
if (mysqli_query($conexion,$alter_query))
	{
		echo "Se ha modificado la tabla de Peliculas correctamente";
	}
else
	{
		echo "No se ha podido modificar la tabla";
	}
	
if (mysqli_query($conexion,$usuarios_table_query))
	{
		echo "La tabla usuarios se ha creado exitosamente";
	}
else
	{
		echo "No se ha podido crear la tabla Usuarios.";
	}
	
if (mysqli_query($conexion,$insert_usuario))
	{
		echo "Se ha insertado correctamente el usuario.";
	}
else
	{
		echo "No se ha podido agregar el usuario.";
	}

I know the if statements are not necessary, with a simple "Or die ($message) will do, but I did it for practice. Running that script, the if statementes work perfectly. So, what's the difference? Also, would you mind typing an example about the variable methods and what you meant before? It would be really apreciatted!

Thanks for the heads up on my bad coding practice, I'll fix the query right away! Hope I explained myself clearly.

According to the manual your if statement should be the following:

if (mysqli_query($Conexion,$Query)===TRUE)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.