Mysql not creating the PHP content line

Asked

Viewed 105 times

-5

When I try to pass values to my database I don’t find them in it, I’m using php with MAMP.

Follows the code:

<?php
error_reporting(E_ALL);ini_set('display_errors', 1);
    $host = "localhost:8889";
    $user = "root";
    $pass = "root";
    $banco = "ArrayEnterprises";
    $conexao = mysqli_connect($host, $user, $pass, $banco) or die(mysqli_error());
    mysqli_select_db($conexao, $banco) or die(mysqli_error());

    $do = $_POST["env"];
    if($do == "Enviar") {
        $nome = $_POST["nome"];
        $email = $_POST["email"];
        $senha = $_POST["senha"];
        $img = $_POST["img"];
    } elseif($do == "Logar"){
        $email = $_POST["email"];
        $senha = $_POST["senha"];
    }

    echo "$nome    $email    $senha    $img";
    mysqli_query($conexao, "SELECT * FROM Usuario");
    $sql = mysqli_query($conexao, "INSERT INTO Usuario (nome, email, senha, img) VALUES('$nome', '$email', '$senha', '$img')");
    mysqli_close($conexao);
?>

I do not know how to see things in Phpmyadmin, but I believe that there is not there the information that I went through parameter, what can be wrong?

  • 1

    What is the error that appears?

  • 2

    Alexandre, here we focus on the problem; "guys", "thank you", "please" is all noisy and disturbs the understanding of the question. Please spend a little more time formulating the question, explaining the problem and spelling out the parts of code that don’t work. I’m sure you’ve been referred to [help] and the [Ask] guide before...

  • Hello, Alexandre. Looking at your code and your question, you can’t guess what your question is. It would be nice if you could [Dit] the question and explain what you are trying to do, what did not come out as expected, and how you came to this finding. If you can post one Minimum, Complete and Verifiable Example would also help. Help us help you.

1 answer

2


The problems there are basic. In recommendation, to start working with queries to SQL databases, I would recommend a search on CRUD, acronym for:

  • CREATE (INSERT)
  • READ (SELECT)
  • UPDATE (UPDATE)
  • DELETE (DELETE)

These are basically the 4 main operations that can be executed in an SQL query.

Like any other good and "functional" script that makes use of a database, one must first create a connection to that database. In PHP the main function responsible for this is the mysqli_connect (procedural).

mysqli_connect('localhost', 'utilizador', 'senha', 'banco' [, outros])

The host standard is usually the localhost(Unix Socket) or 127.0.0.1 (TCP/IP), and there is no need to specify the port unless required.

After establishing the connection to the database, one can start reading, modifying, deleting, or entering values in the database.

Create

<?php
$proced_con = mysqli_connect('localhost', 'root', '', 'exemplo');

# @Create
# "INSERT INTO tabela (campos) VALUES (valores)";

$titulo = 'SóS';
$insert = "INSERT INTO exemplo (titulo) VALUES ('{$titulo}')";

if($query = mysqli_query($proced_con, $insert)){
    if(mysqli_affected_rows($proced_con) > 0){
        print "Inserido";
    }
} else {
    print "Não inserido @insert: " . mysqli_error($proced_con);
}

?>

Read

<?php

$proced_con = mysqli_connect('localhost', 'root', '', 'exemplo');

# @Read
# "SELECT campos | * FROM tabela [WHERE]";

$select = "SELECT titulo FROM exemplo";

if($query = mysqli_query($proced_con, $select)){
    while($resultados = mysqli_fetch_assoc($query)){
        print $resultados['titulo'] . "<br/>";
    }
} else {
    print "Consulta não efectuada @select: " . mysqli_error($proced_con);
}

?>

Update

<?php
$proced_con = mysqli_connect('localhost', 'root', '', 'exemplo');

# @Update
# "UPDATE tabela SET [campos=valores] WHERE [campo=valor]";

$titulo = 'SóS';
$update = "UPDATE exemplo SET titulo='{$titulo}' WHERE id=117";

# @ Update
if($query = mysqli_query($proced_con, $update)){
    if(mysqli_affected_rows($proced_con) > 0){
        print "Campos actualizados";
    } else {
        print "Nada modificado";
    }
} else {
    print "Consulta não efectuada @update: " . mysqli_error($proced_con);
}

?>

Delete

<?php
$proced_con = mysqli_connect('localhost', 'root', '', 'exemplo');

# @Delete
# "DELETE FROM tabela WHERE [campo=valor]";

$delete = "DELETE FROM exemplo WHERE id=117";

# @Delete
if($query = mysqli_query($proced_con, $delete)){
    if(mysqli_affected_rows($proced_con) > 0){
        print "Removido";
    } else {
        print "Não foi removido";
    }
} else {
    print "Consulta não efectuada @delete: " . mysqli_error($proced_con);
}

?>

In order of each consultation, one can relieve the memory using mysqli_free_result, and you must also close the connection to the database using the function mysqli_close, after the query has been executed or values have been returned.

These are simple examples of a CRUD functional, if you want to know more about the SQL commands, I recommend that you visit the oracle, and for more Mysqli or other PHP functions, visit the php.net.

Now focusing on the existing problem(s) (s) in your script, I start with the first line, where you set the mistakes for the configuration file and the errors in itself.

error_reporting(E_ALL);
ini_set('display_errors', 1);

Unless it is in a production environment, this configuration does not compensate at all, it is always better to make these changes directly in the php.ini, so as to leave them permanent and without the need to always enable them at the beginning of each script.

$host = "localhost:8889";
$user = "root";
$pass = "root";
$banco = "ArrayEnterprises";
$conexao = mysqli_connect($host, $user, $pass, $banco) or die(mysqli_error());
mysqli_select_db($conexao, $banco) or die(mysqli_error());

In this part, the configuration defined in the variable $conexao was enough, except for the return in case this connection fails. Where do you have mysqli_error, the correct would be mysqli_connect_error().

The function mysqli_error is only used when you already have a database connection, and returns only function errors MySQLi.

The use of mysqli_select_db is unnecessary since the database has already been specified in the function mysqli_connect there is no need to re-select the database. This function is useful when you want to perform a query in different databases without having to close the connection, or start another.

 $do = $_POST["env"];
    if($do == "Enviar") {
        $nome = $_POST["nome"];
        $email = $_POST["email"];
        $senha = $_POST["senha"];
        $img = $_POST["img"];
    } elseif($do == "Logar"){
        $email = $_POST["email"];
        $senha = $_POST["senha"];
    }

Back then there was a question whose main question was, creating new variables to store values, there were very good answers, you can find this question by searching for the tag php. What happens is that when new variables are created when we can simply use the input variable, we do practically nothing different, of course, besides wasting time and increasing more lines of code to our script, and that’s exactly what happened to the variable $_POST in your script. It’s okay to leave this part as it is, but creating a variable was not preoved in any way.

Another problem is whether $_POST['img'] is a field of the kind text or file, 'cause if you’re the type file, correct would be to use $_FILES['img'] instead of $_POST. The variable $_FILES is usually an array containing all details of the selected file.

    mysqli_query($conexao, "SELECT * FROM Usuario");

The function mysqli_query returns a content object the result of that query, for queries like SELECT, SHOW, DESCRIBE, EXPLAIN executed without errors, so it should normally be assigned to a variable, so that we can the result of this query.

I think that’s all there was to say about your question, if you still have doubts, and what I just answered is not enlightening enough, use the search bar on the site, and look for questions with similar problems.

  • Thank you very much indeed, both for your willingness to write what you wrote and for the fact that what you said worked.

Browser other questions tagged

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