Forms data empty

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
sjmotos
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Mar 20, 2017 9:58 am

Mon Mar 20, 2017 10:12 am

Hi, thanks for accepting me on your forum! I am self-taught and I really like learning new things.

I'm getting into this, never before in my life have I done something similar, I have much to learn, could you help me with this ?.

When I enter the empty data in the form it returns an error (and also saves the empty data).

What I want to do is: 1) I can not enter the empty data (that is not entered in the database), 2) Do not show me an error, in its default or message or another page.

What should I change or add to this?
Should I change some parameter of the columns in the database besides?
If someone were so kind, could you tell me the solution and tell me why.

Very thankful !! regards

Code: Select all

<?php
if (isset($_POST['usuario']) && !empty($_POST['usuario']) &&
	isset($_POST['clave']) && !empty($_POST['clave'])){
		
	$nameusuario = $_POST['usuario'];
	$nameclave = $_POST['clave'];
		
	}
	
			
	$conexion=new mysqli('localhost','root','','basep1');

	
	if($conexion->connect_errno)
		{
			echo "no conexion";
	}
		else 
		{
			echo "si conexion, ";		
	}
	
	$sql="INSERT INTO usuarios(usuario,clave) values ('$nameusuario','$nameclave')";
	
	if($conexion->query($sql))
		{
		echo "se inserto el dato";
	}
	else
		{
		echo "no se inserto el dato";
	}
	
?>
Image
AdoptiveSolution
php-forum Super User
php-forum Super User
Posts: 167
Joined: Wed Jun 15, 2016 8:35 am

Mon Mar 20, 2017 2:09 pm

Check the {}

The nesting is wrong.

With or without data, a connection is made with the database and a record is added.

If no connection is made, de INSERT statement will still be executed, with an error as a result.

So again, check the nesting of the code.
Post Reply