Why can't I send this data to the database?

Why i can´t?

Register.php

<?php
require_once "config.php";
?>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cadastro</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    
    <div id="Register">
        <form method="post" action="?go=cadastrar">
            <table id="cad_table">
                <tr>
                    <td>Nome</td>
                    <td><input type="text" name="nome" id="nome" class="txt"></td>
                </tr>
                <tr>
                    <td>Login</td>
                    <td><input type="text" name="login" id="login" class="txt" maxlength="30"></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><input type="text" name="email" id="email" class="txt"></td>
                </tr>
                <tr>
                    <td>Senha</td>
                    <td><input type="password" name="senha id="senha" class="txt" maxlength="30"></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Registar" id="btnCad"></td>
                </tr>
            </table>
        </form>
        
    </div>


</body>
</html>

<?php
if(@_GET['go'] == 'cadastrar'){
    $nome = $_POST['nome'];
    $login = $_POST['login'];
    $email = $_POST['email'];
    $senha = $_POST['senha'];

    if(empty($nome)){
        echo "<script>alert(Preencha todos os campos!'); history.back();</script>";
    }elseif(empty($email)){
        echo "<script>alert('Preencha todos os campos!'); history.back();</script>";
    }elseif(empty($login)){
        echo "<script>alert('Preencha todos os campos!'); history.back();</script>";
    }elseif(empty($senha)){
        echo "<script>alert('Preencha todos os campos!'); history.back();</script>";
    }else{
        $query1 = mysqli_fetch_row(mysqli_query("SELECT * FROM reglog WHERE reglog = '$user'"));
        if ($query1 == 1){
            echo "<script>alert('Login ja existe'); history.back();</script>";
        }else{
            mysqli_query("insert into reglog (nome, email, login, senha) values ('$nome','$login','$email','$senha')");
            echo "<script>alert('Registrado com sucesso');</script>";
            header("location: cadastro.php");
        }
    }   
}
?>

config.php

<?php
$con = mysqli_connect("localhost", "root", "") or die("Não foi possível conectar com o servidor de dados!");
mysqli_select_db($con, "reglog") or die("banco de dados nao localizado");
?>

database image
image

image

What happens when you try?

nothing

Nothing at all? How far through your code does it get? Do you get any error messages?

(Also can you format the code so it’s readable please, use three back-ticks either side of the block of code or the </> button in the editor. Can’t read it at the moment to see what the issue might be.)

Also, typo here:

<input type="password" name="senha id="senha" class="txt" maxlength="30">

which would alter things if your password cannot be blank.

when I click to register goes to http://localhost/login%20cadastro/register.php?go=cadastrar but then when I go to the database there is nothing

And have you added some debug “echo” statements through to code to see which lines of code it executes and where it stops? If you run the query from phpmyadmin with the same data, does it work?

Also here:

 $query1 = mysqli_fetch_row(mysqli_query("SELECT * FROM reglog WHERE reglog = '$user'"));
        if ($query1 == 1){

Where does $user come from? And do you have a column called reglog? You don’t populate that in your insert query. And why would the return from that query be 1? Surely it would be an array?

No you haven’t done any of those things, or no, you have tried but none of the echo statements appear? Or no, the query doesn’t work in phpmyadmin either?

no nothing

I’m not very good at php and html. so I do not understand much, but I went to get this code to youtube. this in Portugues.

To be frank, you need to be careful where you learn from as a beginner. This video is teaching some very bad coding habits, it is not up to modern standards and opens huge security holes.

3 Likes

Oh, here’s another problem:

if(@_GET['go'] == 'cadastrar'){

that @ symbol is wrong, it should read:

if($_GET['go'] == 'cadastrar'){

and that would stop any of the stuff inside your if clause from working. Surprised you don’t get an error message though.

the errors work but give an error, and not in to make the registration, says to fill in all the fields but I fill

OK, if you’re getting an error that says you haven’t filled in the fields, that will stop your queries working. Did you fix the typo (incorrect quotes) that I mentioned in post #4? That typo would alter the name of your password field, and mean that when you check the intended name, it will be empty.

Before you use this code anywhere, you appear to be storing plain-text passwords in your database - you really don’t want to do that. Use password_hash() to store them, and password_verify() to verify when the user is logging in.

No, because I couldn’t figure it out, and I forgot to say I didn’t.
can explain better?

You don’t close the quotes after you specify the name. Look here:

<input type="password" name="senha id="senha" class="txt" maxlength="30">
                                  ^

So the result is that field is called “senha id”, and will produce `$_POST[‘senha id’]. That is, I think it will - I’ve never tried field names with a space in them.

I’d have expected a parse error unless you have a similar problem somewhere else to balance it out. Or is it a typo in the post, not in your actual code? Either way, you can var_dump($_POST) to see what you are receiving from the form when it is submitted.

Not still giving error | Notice: Undefined index: go in C:xampphtdocslogin registerregister.php on line 46

Which is line 46?

yes it’s line 46

No, @Gandalf means can you show us which line of your code is line 46? Highlight it in the original post, or post it separately.

But, I think I know which line it is, presumably you’ve now fixed it to read

if ($_GET['go'] == 'cadastrar'){

and this error means that you’re trying to access an array element called “go” which does not exist. You should read up on the isset() function to see how to get rid of it. I presume you get the error when you first open the page, not when you submit the form.

I must say the way you decide whether the form is submitted is rather strange, to my relatively inexperienced eyes at least. Most people look at whether the button is set, but the more recommended way these days seems to be

if ($_SERVER['REQUEST_METHOD'] == "POST") {

Normally I’d see that before you draw the form, rather than afterwards, unless the form handling code is in a separate file. It’s difficult to tell from the original post.

I don’t see why you are sending a form with both POST and GET variables, unless that’s needed somewhere else too.

Maybe I missed something but looking at your code you seem to be connecting to a database called reglog and updating a table called reglog. Is this correct or are you confusing the table and the database?