Click here to Skip to main content
15,035,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my php code :
PHP
  1  <?php
  2   print_r($_POST);
  3   print_r($_FILES);
  4  include("conn.php");
  5  
  6  function corectez($sir) {
  7    $sir = trim($sir);
  8    $sir = stripslashes($sir);
  9    $sir = htmlspecialchars($sir);
 10    return $sir;
 11  }
 12  
 13  $compania=corectez($_POST["combo"]);
 14  $tara=corectez($_POST["tara"]);
 15  $prez=corectez($_POST["prez"]);
 16  $nr=corectez($_POST["nr"]);
 17  $pret=corectez($_POST["pret"]);
 18  //  Inserez in tabelul util. Pentru poza folosesc un nume generic (a.png)
 19  //  Apelez prepare, apoi bind
 20  $stmt = mysqli_prepare($cnx, "INSERT INTO util zboruri (fisier_img,id_companie,tara,prezentare,nr_locuri,pret) VALUES(?,?,?,?,?,'a.png')");
 21  mysqli_stmt_bind_param($stmt,'sissis',$img,$compania,$tara,$prez,$nr,$pret);
 22  
 23  mysqli_stmt_execute($stmt);
 24  
 25  $id = mysqli_insert_id($cnx);  //  ID-ul ultimului articol introdus
 26  // Preiau datele fisierului care contine poza
 27  $mesajEroare = '';
 28  if ($_FILES["poza"]["error"] > 0) {
 29      $mesajEroare .= "<p>Eroare: " . $_FILES["fisier"]["error"] . "</p>";
 30  } else {
 31      $nm = $_FILES["fisier"]["name"]; //  nume fisier
 32      $nmtmp = $_FILES["fisier"]["tmp_name"]; //  nume fisier temporar
 33      $extensie = pathinfo($nm,PATHINFO_EXTENSION);
 34      if(strtolower($extensie) != "jpg" && strtolower($extensie) != "png" && strtolower($extensie) != "jpeg") {
 35          $mesajEroare .= "<p>Atentie, se acceptă doar fișiere JPG și PNG!</p>";
 36      }
 37      // Generez un nume dependent de id si transfer fisierul cu poza in directorul 
 38      $nume = 'p_' . (string)$id . '.' . strtolower($extensie);
 39      //  Inlocuiesc numele implicit cu cel real
 40      $cda = "UPDATE zboruri SET fisier_img = '$nume "." '  WHERE id_zbor = $id";
 41      mysqli_query($cnx, $cda);
 42      //  Mut fisierul din directorul temporar
 43      $cale = 'img/'.$nume;
 44      $rezultat = move_uploaded_file($nmtmp, $cale);
 45      $raspuns['mesaj'] ='da';
 46      $raspuns['tara'] = $tara;
 47      if (!$rezultat)
 48      {
 49          $raspuns['mesaj'] ='nu';
 50          $raspuns['tara'] = $tara;
 51         }
 52  //  Inchid $stmt si $cnx
 53  mysqli_stmt_close($stmt); 
 54  mysqli_close($cnx);
 55  $raspuns['mesaj'] ='da';
 56  $raspuns['tara'] = $tara;
 57  echo json_encode($raspuns);
 58  
 59  ?>

And my js code :
JavaScript
document.querySelector("#adauga").onclick = function (event) {
  var formElement = document.querySelector("form");
  var formData = new FormData(formElement);
  var xhtp = new XMLHttpRequest();
  xhtp.open("POST", "./php/adg.php");
  xhtp.onload = function () {
    var raspunsobiect = JSON.parse(this.responseText);
    var blocraspuns = document.querySelector("#verificat");

    if (raspunsobiect.mesaj == "da") {
      var continutbloc = '<p class="msg">da</p>';
    } else {
      var continutbloc = '<p class="msg">nu</p>';
    }
    continutbloc = continutbloc.replace("{tara}", raspunsobiect.tara);
    //ascund #blocform
    document.querySelector("form").style.display = "none";
    //scriu
    blocraspuns.innerHTML = continutbloc;
  };

  // S-a produs o eroare
  xhtp.onerror = function () {
    alert("Hopa! Ceva n-a mers!");
  };

  xhtp.send(formData);
};


What I have tried:

Hello,
I'm having a problem with my php code ...the error is Parse error: syntax error, unexpected end of file in C:\wamp64\www\simulare_aeroport\php\adg.php on line 59
Posted
Updated 13-May-20 23:30pm
v3

That error is almost always an incomplete block. I took a quick look at your PHP code, and I can't see the } to close the big else block
   
The else at line 30 opens a { , but it is never closed.
You probably missing a } at line 52 (a guess from code indentation).
   
v3
Comments
Patrice T 14-May-20 15:38pm
   
Just curious, why the solution deserves a downvote, what is so wrong ?
Richard MacCutchan 15-May-20 3:07am
   
It wasn't me, but I suspect someone feels you are merely repeating what Peter already wrote.
Patrice T 15-May-20 3:14am
   
Ok, I see your point.
So my main mistake is that I didn't refresh the page before posting the solution.
Richard MacCutchan 15-May-20 4:06am
   
Well maybe, but we all do it from time to time. I would not worry about it, the downvoters are not people who you need to care about. Those of us who know you are well aware of your contribution to the site.
Patrice T 15-May-20 6:40am
   
What upset me is that downvoters usually don't have courage to tell the reason of downvote. You know how it can be difficult to come up with a sensible solution.
Richard MacCutchan 15-May-20 6:48am
   
I agree, but in reality they cannot harm you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900