Click here to Skip to main content
15,346,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone! I have a problem in my Sistem, And I dont know why pass, the problem is when y send my datas to the database, and I enter in my table of the database, my data is not displayed, which I had just sent THIS IS MI CODE


PHP
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "clini";

$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
 
if(!$conn)
{
	die("No hay conexion:" .mysqli_connect_error());
}

?>

  

<?php 
 
 $nombre = $_POST['name'];
 $identi = $_POST['ci'];

//Login
if(isset($_POST["ingre"]))
{
	$query = mysqli_query($conn,"SELECT * FROM usuario WHERE nombre = '$nombre' AND identificacion='$identi'");
	$nr = mysqli_num_rows($query);
	
	if($nr==1)
	{
		echo "<script> alert('Bienvenido: $nombre,$appelidoC:'); window.location='home.php' </script>";
	}else
	{
		echo "<script> alert('Usuario no existe 😞'); window.location='index.php' </script>";
	}
}
  
  //Registrar
$name = $_POST["name"];
$apellido = $_POST["apec"];
$dad = $_POST["namep"];
$mom = $_POST["namem"];
$tipeCI = $_POST["tipeid"];
$ci = $_POST["ci"];
$phone = $_POST["te"];
$correo = $_POST["email"];
$add = $_POST["dirre"];
$day = $_POST["diana"];
$gradoI = $_POST["gradoin"];
$esC = $_POST["estado"];
$sex = $_POST["sex"];
$pass = $_POST["pass"];
  
if(isset($_POST["butt"]))
{
	$sqlgrabar = "INSERT INTO usuario(name,last,nameDad,nameMom,tipoIdentificacion,identificacion,telefono,email,address
	,born,rango,estadoC,sex,pass)
	values ('$name','$apellido','$dad','$mom','$tipeCI','$ci','$phone','$correo','$add','$day','$gradoI','$esC',
	'$sex','$pass')"; 
	
	if(mysqli_query($conn,$sqlgrabar))
	{
		echo "<script> alert('Felicidades el usuario fue registrado con exito!: $nombre'); window.location='index.php' </script>";
	}else 
	{
		echo "Error: ".$sqlgrabar."<br>".mysqli_error($conn);
	}
}
?>


What I have tried:

He intentado cosas de otros foros, pero nada, gracias por su ayuda
Posted
Updated 6-Nov-21 12:53pm
v3

So little code, so many huge mistakes ...

1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: this is web based so if you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
   
PHP
$sqlgrabar = "INSERT INTO usuario(name,last,nameDad,nameMom,tipoIdentificacion,identificacion,telefono,email,address
,born,rango,estadoC,sex,pass)
values ('$name','$apellido','$dad','$mom','$tipeCI','$ci','$phone','$correo','$add','$day','$gradoI','$esC',
'$sex','$pass')";

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900