0

I have a weird issue that's driving me crazy. The first time I insert data into a mysql table, it works. It fails silently after that. This is the table:

$students =
    'CREATE TABLE IF NOT EXISTS students (
         student_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
         nombre VARCHAR(255) NOT NULL,
         apellido VARCHAR(255) NOT NULL,
         natalicio INT NOT NULL,
         cuil INT UNIQUE NOT NULL,
         trabajador_teatro TINYINT NOT NULL, 
         ocupacion VARCHAR(255),
         derivado_ministerio TINYINT NOT NULL,
         ex_alumno TINYINT NOT NULL, 
         intereses VARCHAR(255),
         matricula VARCHAR(255)  NOT NULL,
         estudios_formales VARCHAR(255),
         domicilio VARCHAR(255),
         mail VARCHAR(255) NOT NULL,
         mail_confirmation_key VARCHAR(255) ,
         created INT,
         tel INT NOT NULL,
         active TINYINT NOT NULL
         )';

This is the method that inserts the data, remember, first insert works well:

/

/Insert user data
    public function insertStudent($options, $db_object) {

    $sql='INSERT INTO students (nombre, apellido, natalicio, cuil, trabajador_teatro, ocupacion,
        derivado_ministerio, ex_alumno, intereses, matricula, estudios_formales, domicilio, mail, mail_confirmation_key,
        created, tel, active)
         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';


            if (is_array($options) ) {
                $nombre = $options['nombre'];
                $apellido = $options['apellido'];
                $natalicio = $options['natalicio'];
                $cuil = $options['cuil'];
                $trabajador_teatro = $options['trabajador_teatro'];
                $ocupacion = $options['ocupacion'];
                $derivado_ministerio = $options['derivado_ministerio'];
                $ex_alumno = $options['ex_alumno'];
                $intereses = $options['intereses'];
                $estudios_formales = $options['estudios_formales'];
                $domicilio = $options['address'];
                $mail = $options['mail'];
                $tel = $options['tel'];
            }//End if
                $mail_confirmation_key = $this->mailConfKey();
                $date =  new DateTime();
                $created = $date->getTimestamp();
                $active = 0;
                $matricula = 'TAE'."-".ceil((substr($cuil, 4)+$created)/5);
        /* Prepare statement */
        $stmt = $db_object->prepare($sql);
        if($stmt === false) {
          trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $db_object->error, E_USER_ERROR);
        }//End if

        /* Bind parameters. TYpes: s = string, i = integer, d = double,  b = blob */
        $stmt->bind_param('ssiiisiissssssiii',$nombre, $apellido, $natalicio, $cuil, $trabajador_teatro, $ocupacion, $derivado_ministerio,
            $ex_alumno, $intereses, $matricula, $estudios_formales, $domicilio, $mail, $mail_confirmation_key, $created, $tel, $active);

        /* Execute statement */
        $stmt->execute();
        echo $stmt->insert_id;
            echo $stmt->affected_rows;               
        $stmt->close();

    }//End insertStudent

There are no apache2 log errors. These are mysql logs, the first query that works, and the subsequent one that does not work.

First Query
61 Prepare   INSERT INTO students (nombre, apellido, natalicio, cuil, trabajador_teatro, ocupacion,
                derivado_ministerio, ex_alumno, intereses, matricula, estudios_formales, domicilio, mail, mail_confirmation_key,
                created, tel, active)
                 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
                   61 Execute   INSERT INTO students (nombre, apellido, natalicio, cuil, trabajador_teatro, ocupacion,
                derivado_ministerio, ex_alumno, intereses, matricula, estudios_formales, domicilio, mail, mail_confirmation_key,
                created, tel, active)
                 VALUES ('Diego','Lopez',1395975600,20218976543,1,'Surfer',1,0,'Volar','TAE-280617849','primario','20 1122','test@test.com','jtvSRLYfU8uyiHVh6gPp',1394$
                  61 Close stmt


Second query
72 Prepare   INSERT INTO students (nombre, apellido, natalicio, cuil, trabajador_teatro, ocupacion,
                derivado_ministerio, ex_alumno, intereses, matricula, estudios_formales, domicilio, mail, mail_confirmation_key,
                created, tel, active)
                 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
                   72 Close stmt
                   72 Quit

I fetch data from $_POST and use it. It's just a beta, I know it's not properly sanitized.

$nombre =  stripcslashes($_POST['nombre']);
$apellido = stripcslashes($_POST['apellido']);
$natalicio = $core->formatTimeToUs($_POST['birthday']);
$mail = $_POST['mail'];
$address = stripcslashes($_POST['address']);
$tel = stripcslashes($_POST['tel']);
$cuil = stripcslashes($_POST['cuil']);
$curso = stripcslashes($_POST['curso']);
$niv_educ = stripcslashes($_POST['niv_educ']);
$trabajador_teatro = stripcslashes($_POST['trabajador_teatro']);
$ocupacion = stripcslashes($_POST['ocupacion']);
$derivado_ministerio = stripcslashes($_POST['derivado_ministerio']);
$ex_alumno  = stripcslashes($_POST['ex_alumno']);
$intereses  = stripcslashes($_POST['intereses']);
$estudios_formales = stripcslashes($_POST['niv_educ']);

$student = new student();


$student_user_array = array('nombre'=>$nombre, 'apellido'=>$apellido ,  'natalicio'  => $natalicio,  'cuil'=>$cuil, 'mail' => $mail, 'address' =>  $address, 'tel' => $tel,
'curso'=>$curso, 'niv_educ'=>$niv_educ, 'trabajador_teatro' => $trabajador_teatro, 'ocupacion' => $ocupacion, 
'ex_alumno' => $ex_alumno, 'intereses'=>$intereses, 'estudios_formales'=> $estudios_formales, 'derivado_ministerio'=>$derivado_ministerio);


$student->insertStudent($student_user_array, $conn);    

Hope someone can point me in the right direction. Thanks in advance. Sebastian

sms
  • 393
  • 6
  • 20
  • 1
    Put a mysql error on every sql function and see where it stops and what the error is. Since you're not showing any of the error messages or error code with what you have right now. – Prix Mar 06 '14 at 13:53
  • That's problem. It produces no errors. It just doesn't execute statements after the first. I left the logs from the queries. – sms Mar 06 '14 at 14:07
  • The code that you're showing only only hits the prepare/bind/execute once. Are you calling `insertStudent()` within a loop? Please show the code where `insertStudent()` is called. – Patrick Q Mar 06 '14 at 14:14
  • @sms [try setting the error for each function like this just to make sure it is not failing at any of it](http://stackoverflow.com/a/18971788/342740) – Prix Mar 06 '14 at 14:18
  • 1
    Why are you using `stripcslashes` if you're already using prepared statements? – Prix Mar 06 '14 at 14:24
  • 1
    `cuil INT UNIQUE NOT NULL` Is it possible that you're not providing a unique value for this? – Patrick Q Mar 06 '14 at 14:25
  • I have a method that checks that cuil field doesn't exists prior to insert. Apart from that I tested tons of different unique data. I don't know why I was using stripcslashes. I'll take it out. Good point – sms Mar 06 '14 at 14:27
  • Try doing what I have previously mentioned about setting a error handle for each mysql function to see if anything will be found. – Prix Mar 06 '14 at 15:17

0 Answers0