Hello, I have a problem on a php page with an INSERT query

1

I have my php page in the first part of the page I have the declaration of the variables and their storage with the "name" that places them on the html page

<?php
include 'cn.php';
//recir los datos y almacenarlos en las variables
$nombre = $_POST["nombre"];
$apellidos = $_POST["apellidos"];
$correo = $_POST["correo"];
$usuario = $_POST["usuario"];
$clave = $_POST["clave"];
$telefono = $_POST["telefono"];
$cuenta = $_POST["cuenta"];
$idsemgrupo = $_POST["idsemgrupo"];
$curp = $_POST["curp"];

después hice la consulta insertar que es esta
//consulta para insertar 
$insertar = "INSERT INTO alumnos( id, nombre, apellidos, correo, usuario, clave, telefono, cuenta, idsemgrupo, curp) VALUES (NULL, "$nombre", "$apellidos", "$correo", "$usuario", $clave, $telefono, "$cuenta", $idsemgrupo, "$curp")";

//ejecutar consulta
$resultado = mysqli_query($conexion, $insertar);
if(!$resultado){
    echo 'Error al insertar Registros';
}
else{
    'Registros insertdos correctamente';
}

//cerrar conexion
mysqli_close($conexion);

and it marks me error here

  

Parse error: syntax error, unexpected '$ name' (T_VARIABLE) in   C: \ xampp \ htdocs \ project \ register.php on line 16

If you can help me I would thank you

    
asked by Juan Carlos Olivera Gomez 27.05.2018 в 14:48
source

2 answers

0

I mention that among other details, the code is incorrect for the following reasons

  • You are not entering the variables correctly, some even put them in quotes and others without quotes, you are not even concatenating them
  • Just as I put it on you should work

    $insertar = "INSERT INTO alumnos(nombre, apellidos, correo, usuario, 
    clave, telefono, cuenta, idsemgrupo, curp) VALUES ('".$nombre."', 
    '".$apellidos."', '".$correo."', '".$usuario."', '".$clave."', 
    '".$telefono."', '".$cuenta."', '".$idsemgrupo."', '".$curp."')";
    
      

    I also tell you that if your column id, in your database it is auto   incremental and primary key, you do not need to put it anywhere in your   query, only if it meets those 2 characteristics

        
    answered by 27.05.2018 в 14:59
    0
    <?php
    try {
    $conn = new PDO('mysql:host=localhost;dbname=tu_db', 'root', 'tu_pass');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e){
        echo "ERROR: " . $e->getMessage();
    }
    
    
    
    if(isset($_POST['agregar'])){
    if(!empty($_POST['nombre']) && !empty($_POST['apellido'])){
        $add = $conn->prepare("INSERT INTO alumnos (nombre, apellido) VALUES (:nombre, :apellido)");
        $add->bindValue(':nombre', ($_POST['nombre']));
        $add->bindValue(':apellido', ($_POST['apellido']));
        $add->execute();
        echo "El alumno fue agregado <a href='../'>volver</a>";
    }
    else{
        echo "Ingresar datos <a href='../'>volver</a>";
    }
    }
    ?>
    
    <form action="" method="POST">
    <input type="text" name="nombre" autocomplete="off"><br>
    <input type="text" name="apellido" autocomplete="off"><br><br>
    <button name="agregar">Agregar</button>
    </form>
    

    Just as this should work. It's what I use to work every day. When you have to add something, you have to declare everything. Isset determines if the variable is defined or not, then empty is set if a variable is empty. Then I use prepared sentences: link

    I hope I help you

        
    answered by 27.05.2018 в 19:57