MySQL not creating the PHP content line [closed]

-5

When I try to pass the values to my database I do not find them in it, I'm using php with MAMP.

Follow 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 much about PhpMyAdmin, but I do not think the information I've passed by parameter is wrong, what could be wrong?

    
asked by anonymous 25.11.2015 / 02:54

1 answer

2

The problems that are there, are basic. On a 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)

They are basically the 4 main operations that can be executed in a 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 mysqli_connect (procedural).

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

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

After establishing the connection to the database, you can start reading, modifying, deleting, or inserting 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);
}

?>

For each query, memory can be alleviated using 127.0.0.1 , and you should also close the database connection using the mysqli_free_result function after the query has been executed or been returned values.

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

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

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

Unless it's in a production environment, this setting does not make up for anything, it's always better to make these changes directly in mysqli_close , so you can leave them permanently and without ever having to enable them in 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 php.ini was already sufficient, except the return in case this connection fails. Where you have $conexao , the correct would be mysqli_error .

The mysqli_connect_error() function is only used when you already have a connection to the database, and returns only mysqli_error function errors.

The use of MySQLi is unnecessary, since the database has already been specified in the mysqli_select_db function since there is no need to re-select the database. This function is useful when you want to query 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"];
    }

Even once there was a question whose main question was, the creation of new variables to store values, there were very good answers, you can find this question looking for the tag mysqli_connect . What happens is that when creating new variables 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 this is exactly what happened to the variable php in your script. There is no problem in leaving this part as it is, but creating a variable there was not preoveful at all.

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

    mysqli_query($conexao, "SELECT * FROM Usuario");
The $_FILES function returns an object containing the result of this query, for queries like mysqli_query , SELECT , SHOW , DESCRIBE executed without errors, so it should normally be assigned to a variable, so we can get the result of this query.

I think that's all there was to say about your question, if you continue with questions, and what I just answered is not enlightening enough, use the site's search bar, and look for questions with similar issues.     

25.11.2015 / 09:19