Database does not receive querys by php script although it works

Asked

Viewed 25 times

0

Guys with a problem in receiving the form in the database, it seems that although the script is correct it simply ignores it, using xampp, tried by Cloud9 also and the same thing happens.

<?php
include 'connection.php';

$name = $sbname = $sex = $age = $nasc = $email = $site = "";

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

$name = $_POST['form_nm'];
$sbname = $_POST['form_sobnm'];
$sex = $_POST['form_sex'];
$age = $_POST['form_age'];
$nasc = $_POST['form_nasc'];
$email = $_POST['form_email'];
$site = $_POST['form_site'];

$query = "INSERT INTO pessoas(nome, sobrenome, sexo, idade, nascimento, 
email, website) VALUES ($name, $sbname, $sex, $age, $nasc, $email, $site)";
mysqli_query($link,$query);
}
header('location:../index.php');
?>

the Connection file :

    <?php

    $data = parse_ini_file("config.ini");

    $link = mysqli_connect($data['host'], $data['username'], 
    $data['password'], $data['database']);

    if(mysqli_connect_error()){
        die("Unable to connect to database! Error: " . 
    mysqli_connect_error());
    }

    ?>

and the config.ini :

    [SERVER_DATA]

    host = 'localhost';
    username = 'root';
    password = '';
    database = 'atividade_ecomp';

Everything is correct, I have already reviewed several times, the database, the table and the columns are with the correct names, I am using phpMyAdmin but even after sending the form the database does not add the information.

  • Appears to have a line break in $query and in $link should remove them.

  • Worse than not, the necklace here that was like this, in the sublime is normal.

  • The send button is with the right name ? enviar ?

  • Two things, (1) print $query and see the result, apparently the apostrophes/quotes are missing to separate the strings in values and (2) do something like if ( mysql_connect(...) ){ echo "Ok" } else { echo "Erro!" } to check whether the operation was really successful.

  • A third thing, worth reading about how to securely connect to the PHP database, the way you are doing is about to be removed from the language. -- http://br.phptherightway.com/#banco_de_dados

  • If it shows no error on the screen most likely it is not even entering the if, place several echo to see where the program flow is going and the values of the variables and the sql command

  • makes a test of error if (mysqli_query($link, $query)){echo "sucesso";} else {echo "Erro: " . $query. "<br>" . mysqli_error($link}

  • Really the mistake was the lack of the stakes in the values, thank you very much!!

Show 3 more comments
No answers

Browser other questions tagged

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