Syntax error mysql when inserting record [closed]

1

I have a data insertion problem,

This is my Code

<?php

if ($_POST['MM_insert']) {

    $nombre = $_POST['nombre_alumno'];
    $sa = $_POST['sexo'];
    $dt = $_POST['documento_tipo'];
    $nd = $_POST['num_documento'];
    $fr = $_POST['Fecha_Registro'] = date('Y-m-d');
    $cea = $_POST['correo_electronico'];
    $da = $_POST['direccion_alumno'];
    $ta = $_POST['telefono_alumno'];
    $ta2 = $_POST['telefono_alumno2'];
    $ma = $_POST['movil_alumno'];
    $rt = $_POST['responsable_tipo'];
    $rt2 = $_POST['responsable_tipo2'];
    $nr = $_POST['nombre_responsable'];
    $nr2 = $_POST['nombre_responsable2'];
    $cr = $_POST['cedula_responsable'];
    $cr2 = $_POST['cedula_responsable2'];
    $mr = $_POST['movil_responsable'];
    $mr2 = $_POST['movil_responsable2'];
    $cer = $_POST['correo_electronico_resp'];
    $cer2 = $_POST['correo_electronico_resp2'];
    $cm = $_POST['comentario'];
    $mes = $_POST['mes'];
    $dia = $_POST['dia'];
    $ano = $_POST['ano'];
    $grupo = $_POST['grupo'];
    $aula = $_POST['aula'];
    $idpago = $_POST['id_pago'];
    $pago_nomb = $_POST['plan_nombre'];
    $edad = $_POST['edad_alumno'];
    $creducida = $_POST['creducida'];
    $inscr = $_POST['inscripcion'];
    $password = $_POST['password'];
    $nivel = '4';
    $tcliente = 'Alumno';

    $q = "INSERT into alumno ( nombre_alumno, sexo_alumno, documento_tipo, num_documento, Fecha_Registro, correo_electronico, direccion_alumno, telefono_alumno,telefono_alumno2, movil_alumno, responsable_tipo,responsable_tipo2, nombre_responsable, nombre_responsable2, cedula_responsable,cedula_responsable2, movil_responsable, movil_responsable2, correo_electronico_resp,correo_electronico_responsable2, comentario,mes, dia, ano, grupo, aula, id_pago, plan_nombre, edad_alumno, creducida,inscripcion, password, nivel,tipo_cliente) VALUES ('$nombre', '$sa', '$dt','$nd','$fr', '$cea', '$da', '$ta','$ta2','$ma', '$rt',$rt2','$nr','$nr2','$cr','$cr2','$mr', '$mr2','$cer','$cer2', '$cm','$mes','$dia','$ano','$grupo','$aula','$idpago','$pago_nomb','$edad','$creducida','$inscr','$password','$nivel','$tcliente')";
    $r = $conexion - > query($q) or die(mysqli_error($conexion));

}

?>

Launches the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '','aaaaaa','cccccc','1234567890','0987654321','1234567890', '0987654321','aaa@em' at line 1

But I understand that everything is correct, any suggestions? Thanks

    
asked by Alexander Quiroz 10.06.2017 в 18:21
source

1 answer

2

Problem

The error you have in your code, is presented in the line 40 :

In the query:

'$rt',$rt2','$nr'

You are leaving the quotes open. Which gives way to an error, since the MySQL will interpret the query and there will find that a quote is missing, which will prevent the other data from being inserted.

Possible Solution

You could solve it by doing something like this:

'$rt', '$rt2','$nr'

Adding quotation marks where they are missing. But this does not mean a very good solution to the problem.

What you could do, to simplify plus your code, is to use the sprintf () from PHP.

How does it work?

According to the PHP documentation :

  

Returns a string produced according to the format string given by format.

How could I use it?

You can do something like this:

$dato   = "Hola";
$numero = 40;

$q = sprintf("INSERT INTO tabla (dato, numero) VALUES ('%s', %d)", $dato, $numero);

What sprintf () does is search for each of the wildcards > and replace them with a value, which will be added later in the parameters of the function.

    
answered by 10.06.2017 / 18:27
source