gmc1103 Posted May 6, 2015 Share Posted May 6, 2015 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? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted May 6, 2015 Share Posted May 6, 2015 I think you want array_replace() instead of array_diff() Quote Link to comment Share on other sites More sharing options...
Barand Posted May 6, 2015 Share Posted May 6, 2015 Have you specified id_tempo NOT NULL in the table defiition? Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted May 6, 2015 Author Share Posted May 6, 2015 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 Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted May 6, 2015 Author Share Posted May 6, 2015 I think you want array_replace() instead of array_diff() Hi, no even worst, nothing is inserted into database Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted May 6, 2015 Author Share Posted May 6, 2015 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 6, 2015 Share Posted May 6, 2015 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. Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted May 6, 2015 Author Share Posted May 6, 2015 Thanks for your help So my table field id_tempo must be set to null to accept? Where do i must put $mysql->error? Thanks again Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted May 7, 2015 Author Share Posted May 7, 2015 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.