Jump to content

Array partial insert


gmc1103

Recommended Posts

Hi

 

I have this code and the strange is that all the array is not inserted into database, just a the values sent from the form

<?php
header('Content-Type: application/json');
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
$mysqli = new mysqli("localhost", "xxxxx", "xxxxx", "xxxx");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$num = $_POST['num'];
$dia = $_POST['dia'];
$sala = $_POST['sala'];
$hora = $_POST['hora'];
$a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
$c = array_fill_keys(array_keys(array_diff($a, $hora)), null) + $a;
ksort($c);
$query = "SELECT COUNT(`id_dia`) FROM sala_ocupacao WHERE `id_dia` = ? AND `id_sala`= ?" or trigger_error($mysqli->error);
if ($stmt = $mysqli->prepare($query)) {
    $stmt->bind_param('ss', $dia, $sala);
    $stmt->execute();
    $stmt->bind_result($existe);
    $stmt->fetch();
    printf("Error: %d.\n", $stmt->errno);
    $stmt->close();
}
if ($existe != 0) {
    $response =  "Este dia já está registado na base de dados, use update";
    echo json_encode(array('status' => 'error', 'message' => 'Este dia já está registado na base de dados, use update'));
} else {
    for ($i = 0; $i < 17; $i++) {
        $horas = $c[$i];
        $stmt = $mysqli->prepare("INSERT INTO `ebspma_paad_ebspma`.`sala_ocupacao` (id_dia, id_sala, id_tempo) VALUES (?, ?, ?);")or die(json_encode(mysqli_error($mysqli)));
        $stmt->bind_param('sss', $dia, $sala, $horas);
        if (!$stmt->execute()) {
            echo json_encode(array('status' => 'error', 'message' => 'Opppss...Os Registo(s) não foram gravado(s)'));
        }
         echo json_encode(array('status' => 'success','message'=> 'Os Registo(s) foram gravado(s)'));
    }
    $stmt->close();
}

Anything wrong with this code for that issue?

 

Link to comment
Share on other sites

Hi Barand

 

It was, but i make some changes and put again

ALTER TABLE  `sala_ocupacao` CHANGE  `id_ocup`  `id_ocup` INT( 11 ) NOT NULL AUTO_INCREMENT ,
CHANGE  `id_dia`  `id_dia` INT( 11 ) NOT NULL ,
CHANGE  `id_sala`  `id_sala` INT( 11 ) NOT NULL ,
CHANGE  `id_tempo`  `id_tempo` INT( 11 ) NOT NULL ;

But it still insert only the value selected in the form

Link to comment
Share on other sites

Just to add some information more

the json received is really strange

1{"status":"success","message":"Os Registo(s) foram gravado(s)"}
2{"status":"success","message":"Os Registo(s) foram gravado(s)"}
3{"status":"success","message":"Os Registo(s) foram gravado(s)"}
4{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"
}{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"
}{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}
{"status":"error","message":"Opppss...Os Registo(s) n\u00e3o foram gravado(s)"}
{"status":"success","message":"Os Registo(s) foram gravado(s)"}

As you can see...only 4 are correct...then...it says success and error in the same time...

 

Whats wrong?

Link to comment
Share on other sites

Instead of your error message, try outputting $mysql->error to see why it failed.

 

You need to able to write NULL values to id_tempo so it should not be "NOT NULL"

 

I ran your code on my table and all 17 were inserted. Those not in the $hora array had null values as expected.

Link to comment
Share on other sites

Hi

 

It's now fixed...i receive the json

 

{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}{"status":"success","message":"Os Registo(s) foram gravado(s)"}

 

But in my ajax function is not receiving the result

 

this is my function

 function postData(){
                var dia = document.getElementById('dia').value;
                var sala = document.getElementById('sala').value;
                var tempos = [];
                var s = document.getElementById('hora');
                for (var i = 0; i < s.options.length; i++) {
                    if (s.options[i].selected == true) {
                        var valores = s.options[i].value;
                        tempos.push(valores);
                    }
                }
                console.log(tempos);
                var num = document.getElementById('num').value;
                console.log(num);
                    $.ajax({ 
                            type: "POST",
                            dataType: 'json',
                            url: "registerBd.php",
                            data : {'dia': dia, 'sala': sala, 'hora': tempos, 'num': num},
                            success: function(data){
                                var result = data.status;
                                console.log(result);
                                if(result == 'success'){
                                    $('#ajaxDivOk').html(data.status +':' + data.message);   
                                    $("#ajaxDivOk").fadeIn(1500); 
                                }
                                else{
                                   $("#ajaxDivErro").html('Este dia já está registado na base de dados, use update');
                                   $("#ajaxDivErro").fadeIn(1500); 
                                } 
                            }
                       });
                       return false;
                }

and is was working before moving to the remote server...so i don't get the information if the records have been inserted or not

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.