PHP in conjunction with Mysql in user record

Asked

Viewed 35 times

0

I am making a user registration system and it has been presenting some errors if anyone can help me with them would be very grateful.

<?php 

include("conexao.php"); 

$Email = $_POST['Email'];

$sql_code ="SELECT * FROM usuario WHERE Email = '$Email'";

$confirma = $mysqli->query($sql_code) or die($mysqli->error);

$veri = mysql_num_rows($sql_code);


if (isset($_POST['botao'])){

if (isset($_SESSION)) 
    session_start();

foreach ($_POST as $chave => $valor) 
    $_SESSION[$chave] = $mysqli->real_escape_string($valor);


if (strlen($_SESSION['Nome']) == 0) 
    $erro[] = "Nome em branco.";

if (strlen($_SESSION['Sobrenome']) == 0) 
    $erro[] = "Sobrenome em branco.";

if (substr_count($_SESSION['Email'], '@') != 1 || substr_count($_SESSION['Email'], '.')< 1 ) 
    $erro[] = "Email invalido.";



if (strcmp($_SESSION['Senha'], $_SESSION['Confse'])) 
    $erro[] = "As senhas precisam ser iguais";


if ($veri > 0 ) 
    $erro[] = "Email já cadastrado.";


if (count($erro) == 0) {

$code = "INSERT INTO `usuario` (
`Id_Usuario`, 
`Nome_Usuario`,  
`Sobrenome_Usuario`, 
`Email`, 
`Senha`, 
`Idade`, 
`Sexo`)
VALUES (
NULL, 
'$_SESSION[Nome], 
'$_SESSION[Sobrenome]', 
'$_SESSION[Email]', 
'$_SESSION[Senha]', 
'$_SESSION[idade]', 
'$_SESSION[Sexo]')";

$con = $mysqli->query($code) or die($mysqli->error);}} if (count($erro) > 0 
) {
foreach ($erro as $valor) {
        echo "$valor <script>location('login.php')</script>";
}}?>

Follow the error image inserir a descrição da imagem aqui

1 answer

0


The mistake mysql_num_rows is because you are using mysqli. So the correct code is mysqli_num_rows. Also, for you to count the amount of record you need to use the variable of query thus:

$veri = mysqli_num_rows($confirma);

The error of the variable $erro is that it has not been set before. You can do this:

 $erro = array(); // aqui eu defini a variável em array
if (strlen($_SESSION['Nome']) == 0) 
    $erro[] = "Nome em branco.";

if (strlen($_SESSION['Sobrenome']) == 0) 
    $erro[] = "Sobrenome em branco.";

if (substr_count($_SESSION['Email'], '@') != 1 || substr_count($_SESSION['Email'], '.')< 1 ) 
    $erro[] = "Email invalido.";

 ... resto do código ...

The error to insert into the database is in the string, one is missing ('):

'$_SESSION[Nome], // <- bem aqui
'$_SESSION[Sobrenome]', 
'$_SESSION[Email]', 
'$_SESSION[Senha]', 
'$_SESSION[idade]', 
'$_SESSION[Sexo]')";

The right thing is:

'$_SESSION[Nome]', // <- bem aqui
'$_SESSION[Sobrenome]', 
'$_SESSION[Email]', 
'$_SESSION[Senha]', 
'$_SESSION[idade]', 
'$_SESSION[Sexo]')";

If the error persists somewhere comment here.

Note: If the Age is an integer this record '$_SESSION[idade]' has to be changed to $_SESSION[idade]. Quote-free.

  • It all worked out thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.