Error with PHP when saving data in mysql [closed]

6

I am trying to save mysql the data that you enter in the form but the php is giving me errors like this: Parse error: syntax error, unexpected '[' in C:\wamp\www\Tarea2\index.php on line 5

Here the code:

<?php
if(isset($_POST['btnGuardar'])){
$con = mysqli_connect("localhost","Elvisdt17","taveras17","personas");

    $nombre = _POST['nombre'];
    $apellido = _POST['apellido'];
    $tel = _POST['telefono'];
    $email = _POST['email'];
    $dir = _POST['direccion'];
    $sexo = _POST['sexo'];
    $sangre = _POST['tipoSangre'];
    $cedula = _POST['cedula'];

    mysqli_query($con, "INSERT INTO gente (Nombre, Apellido, Sexo, Telefono, Email, Cedula, Direccion, Tipo_sangre)
       VALUES('$nombre', '$apellido', '$sexo', '$tel', '$email', '$cedula', '$dir', '$sangre')");
    mysqli_close($con);
}?>

Could you help me?

    
asked by Elvis Taveras 16.09.2016 в 04:51
source

3 answers

8

The problem in your code is that you are missing the $ signs of the variables $_POST You have:

$nombre = _POST['nombre'];
$apellido = _POST['apellido'];
$tel = _POST['telefono'];
$email = _POST['email'];
$dir = _POST['direccion'];
$sexo = _POST['sexo'];
$sangre = _POST['tipoSangre'];
$cedula = _POST['cedula'];

Which should be:

    $nombre = $_POST['nombre'];
    $apellido = $_POST['apellido'];
    $tel = $_POST['telefono'];
    $email = $_POST['email'];
    $dir = $_POST['direccion'];
    $sexo = $_POST['sexo'];
    $sangre = $_POST['tipoSangre'];
    $cedula = $_POST['cedula'];
    
answered by 16.09.2016 / 05:11
source
3

Hello friend, you need the sign of the dollar and select the database. Try this way:

<?php
if (isset($_POST['btnGuardar'])) {

    $con = mysqli_connect("localhost", "Elvisdt17", "taveras17", "personas");

    mysqli_select_db($con, "personas");
    $nombre   = $_POST['nombre'];
    $apellido = $_POST['apellido'];
    $tel      = $_POST['telefono'];
    $email    = $_POST['email'];
    $dir      = $_POST['direccion'];
    $sexo     = $_POST['sexo'];
    $sangre   = $_POST['tipoSangre'];
    $cedula   = $_POST['cedula'];

    mysqli_query($con, "INSERT INTO gente (Nombre, Apellido, Sexo, Telefono, Email, Cedula, Direccion, Tipo_sangre) VALUES('" . $nombre . "', '" . $apellido . "', '" . $sexo . "', '" . $tel . "', '" . $email . "', '" . $cedula . "', '" . $dir . "', '" . $sangre . "')");
    mysqli_close($con);
}
?>
    
answered by 16.09.2016 в 05:19
1

You forgot the sign $ ... _POST['algo'] should be $_POST['algo'] :

$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$tel = $_POST['telefono'];
$email = $_POST['email'];
$dir = $_POST['direccion'];
$sexo = $_POST['sexo'];
$sangre = $_POST['tipoSangre'];
$cedula = $_POST['cedula'];

And on the other hand, VERY IMPORTANT , your code has security vulnerabilities that you should address. Please read:

How to avoid SQL injection in PHP?

    
answered by 16.09.2016 в 05:12