1

Hello, I try to insert values into my database (in phpmyadmin) hosted by a server (gandi) but this error appear and I don't know how to resolve it My error :

jquery.min.js:4 POST 

http://95.... 500 (Internal Server Error)
send    @   jquery.min.js:4
ajax    @   jquery.min.js:4
(anonymous) @   (index):65
dispatch    @   jquery.min.js:3
q.handle    @   jquery.min.js:3
triggerMouseEvent   @   ionic.bundle.js:2953
tapClick    @   ionic.bundle.js:2942
tapMouseUp  @   ionic.bundle.js:3018

My code :

app.controller('ajoutUtilisateurCtrl', function($scope)
    {
      $('#submit').click(function(){
        var nom = $scope.nom;
        var prenom = $scope.prenom;
        var email = $scope.email;

        var advertisement = $('#nom').val() + $('#prenom').val() + $('#email').val();

        var datastring = {nom: nom, prenom: prenom, email: email};
        if(advertisement=='')
        {
          $('#response').html('Veuillez saisir les données');
        }
        else{
          $.ajax({
            type: "POST",
            url: "http://95.../ajoutUtilisateur.php",
            data: datastring,
            cache: false,
            success: function(result){
              $('#response').html(result);
              var advertisement = $('#nom').val("") + $('#prenom').val("") + $('#email').val("");
            }
          });
          return false;
        }
        })
      });

ajoutUtilisateur.php

<?php
    include_once('config.php');
    $nom = $_POST['nom'];
    $prenom = $_POST['prenom'];
    $email = $_POST['email'];
    $sql = "INSERT INTO utilisateur(nom, prenom, email) VALUES('$nom', '$prenom', '$email')";

    if($connexion->query($sql) === TRUE){
        echo 'Utilisateur ajouté';
    }
    else
    {
        echo 'Erreur ajout utilisateur';
    }
?>

config.php

<?php

    define('DB_HOST', 'localhost');
    define('DB_NAME', 'db');
    define('DB_USERNAME', 'user');
    define('DB_PASSWORD', '');

    $connexion = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME) or die("Could not connect to database");


    if(mysqli_connect_errno()){
        echo('Connexion non établie'.mysqli_connect_error());
        exit();
    }

?>

How can I resolve this problem ?

Caroline
  • 61
  • 1
  • 9
  • HTTP 500 (Internal Server Error). Something is wrong with your PHP script. I do not see any errors in the php code above. However you should enable errors, add this code to the top of your php script: http://stackoverflow.com/a/21429652/2906013 and btw, you should also add an `error` function to your ajax request, where you get the error response if any errors should occure (http://api.jquery.com/jquery.ajax/). – Andrew Larsen Apr 12 '17 at 09:44
  • Btw the url is not `"http://95.../ajoutUtilisateur.php"` the url you are trying to use, this has just been removed before you pasted the code here, correct? – Andrew Larsen Apr 12 '17 at 09:47
  • 1
    Also, please use prepared statements. That PHP script is wide open for abuse. It seems you're already using mysqli_ so making this a prepared statement should take about 1 minute. – junkfoodjunkie Apr 12 '17 at 09:49
  • Ok thanks I will try this. – Caroline Apr 12 '17 at 09:49
  • and what does console show for js ? – OldPadawan Apr 12 '17 at 09:50
  • Correct I removed this just before publishing my code – Caroline Apr 12 '17 at 09:51
  • is it possible that a missing space would throw 500 ? `INSERT INTO utilisateur(nom` or is your file/folder '755' chmoded or other ? what's in config.php (removing credentials) – OldPadawan Apr 12 '17 at 09:58
  • If I can give you an advice: use the $.ajax 'error' function (http://api.jquery.com/jquery.ajax/) in addition to the 'success' function in order to obtain more detailed messages when your request fails. – Hulothe Apr 12 '17 at 09:59
  • @Hulothe : please use english – OldPadawan Apr 12 '17 at 10:00
  • @OldPadawan the syntax is correct – Hulothe Apr 12 '17 at 10:00
  • Ok I have another problem right now... the error is "Warning : mysqli::query() : couldn't fetch mysqli in var/www/html/.../ajoutUtilisateur.php on line 15 Erreur ajout utilisateur"" – Caroline Apr 12 '17 at 11:00
  • Thank you for all your answers – Caroline Apr 12 '17 at 11:00
  • @Caroline : please check [THIS](https://pastebin.com/kY5AAhmD), test it and let us know if any error thrown again – OldPadawan Apr 12 '17 at 11:21
  • @OldPadawan There is the same error : jquery.min.js:4 POST http://95..../ajoutUtilisateur.php 500 (Internal Server Error) – Caroline Apr 12 '17 at 12:38
  • @Caroline : there can't be **this** error using this code, as there's no call to jQuery. Check 1st if PHP is executed, then we'll check for JS error :) – OldPadawan Apr 12 '17 at 13:21
  • @OldPadawan Ok so when I do "echo"[ $nom / $prenom / $email ]";" this displays well what I entered in the form. But I have a : "Fatal error : Uncaught Error : call to a member function bind_param() on boolean in .../ajoutUtilisateur.php Stack trace : #0 {main} thrown in ... ajoutUtilisateur.php " So the line "$stmt->bind_param("sss", $nom, $prenom, $email);" is problematic? – Caroline Apr 12 '17 at 13:37
  • @Caroline : how many columns in your DB beside `nom / prenom / email` auto-increment ID I guess, but any others ? – OldPadawan Apr 12 '17 at 13:49
  • @OldPadawan In my DB I have **10 columns** (numUtilisateur (= Auto-increment ID), login, nom, prenom, email, password, tel, civilite, adresse, profession) But when I inserted only "nom" "prenom" "email" in my DB in **local** this has worked.. – Caroline Apr 12 '17 at 13:52
  • @OldPadawan can we chat elsewhere to go faster ? – Caroline Apr 12 '17 at 14:01
  • @Caroline : I think you need to define all the columns in pre-prod – OldPadawan Apr 12 '17 at 14:03
  • @Caroline : chat should open, yes we can – OldPadawan Apr 12 '17 at 14:04
  • @OldPadawan What is the pre-prod ? – Caroline Apr 12 '17 at 14:11
  • you work locally, then online pre-prod, then prod, or should be that way ^^ I see you have URL like http://IP/page.php – OldPadawan Apr 12 '17 at 14:13
  • @OldPadawan "I have only 6 reputation, so it's not yet enough to chat" :/ – Caroline Apr 12 '17 at 14:13
  • give me 5/10mn, I'll set up a test script – OldPadawan Apr 12 '17 at 14:14
  • @OldPadawan yes I have an url and I worked locally, now I want to test it online... with a server which contain my database Ok no problem :) – Caroline Apr 12 '17 at 14:16
  • @OldPadawan Meanwhile I insert in my code all attributes but it still does not work (not surprising ^^) – Caroline Apr 12 '17 at 14:26
  • @Caroline : please check [THIS](https://pastebin.com/TN4JbMdL) -> example with simple code form + treatment. Works for me. If it works for you, you'll just have to modify/improve to your needs step-by-step :) let me know – OldPadawan Apr 12 '17 at 14:36
  • @OldPadawan Ok I will try this thank you – Caroline Apr 12 '17 at 14:39
  • @OldPadawan I try your code... but this again doesn't works I have an alert which say the same error : "
    Fatal error : Uncaught Error: call to a member function bind_param() on boolean in ... "
    – Caroline Apr 12 '17 at 14:47
  • @Caroline : no extra code ?! just the one I gave you ? Are table name and columns names OK ? no typo ? – OldPadawan Apr 12 '17 at 14:48
  • @Caroline : have you/can we check `phpinfo()` ? – OldPadawan Apr 12 '17 at 14:51
  • No extra code, only your code, no typo... I just commented all my code and try with your code... I just place my own credentials – Caroline Apr 12 '17 at 14:52
  • @OldPadawan Yes of course how can i send you this ? I have it on my server but I don't no if i can share you the link ^^ – Caroline Apr 12 '17 at 14:55
  • just create a page php.php with ` and give a link to the page (if IP/server is not a secret ^^) – OldPadawan Apr 12 '17 at 14:57
  • So, you saw a problem ? :/ – Caroline Apr 12 '17 at 15:05
  • @Caroline : php7, mysql 5+, everyrhing seems to be fine to me, almost identical to servers I use, but I'm no server guru :/ I'm stuck – OldPadawan Apr 12 '17 at 15:10
  • @OldPadawan ok nevermind I will find the problem and I would tell you :) Thank you for all your help ^^ – Caroline Apr 12 '17 at 15:12
  • @Caroline : I'm officially an idiot !!! have you checked for http://php.net/manual/en/mysqli.error-list.php -> The prepare() method can return false and you should check for that. And I did not verify *yours* as I used one of *my tables* for testing :/ – OldPadawan Apr 12 '17 at 15:13
  • @OldPadawan How can I test the prepare() method ? – Caroline Apr 12 '17 at 15:14
  • @OldPadawan it display : Array( [0] => Array ( [errno] => 1193 [sqlstate] => HY000 [error] => Unknown system variable 'a' ) ) – Caroline Apr 12 '17 at 15:28
  • @Caroline : are credentials OK ? because it seems that's what error reports – OldPadawan Apr 12 '17 at 16:02
  • @OldPadawan Yes but here I just copy the code [of the link that you share ](http://php.net/manual/en/mysqli.error-list.php) at the end of this page – Caroline Apr 12 '17 at 20:03
  • OK thought we had solved it :/ anyway you need to check the prepare method and what it returns so we make sure we can eliminate one possible option – OldPadawan Apr 12 '17 at 20:07
  • I don't understand how check the prepare method if not like that ? – Caroline Apr 12 '17 at 20:11
  • Can you please try and catch more information about errors ? Might be useful to get help from [HERE](http://php.net/manual/en/mysqli.error.php). You can also add column ID in the query, one thing we did not try... So, summary: you work on gandi server, online, all scripts in same folder at this time, credentials ok (no local/online mistake), no cross-domain scripting attempt : correct ? – OldPadawan Apr 13 '17 at 04:11
  • @OldPadawan Ok I will try... Yes I have only mistakes online and not local and the rest I think it's correct. – Caroline Apr 13 '17 at 09:29
  • @Caroline : I created a DB, same structure as yours, same columns, names, everything seems to be identical, and it works... I'll put another example in a few minutes, please test it and let me know. There's also a online example, using the script, I'll show you – OldPadawan Apr 13 '17 at 10:15
  • @Caroline : sample with all warnings/errors reporting [HERE](https://pastebin.com/u0g757gE) -> DB table is utf8 / 1st column is INT + AI / all others are VARCHAR 250 (test IRL is [here](http://www.at-hosting.com/oldPadawan/test-gandi.php) – OldPadawan Apr 13 '17 at 10:25
  • @OldPadawan Thank you. So, I copy your code and I obtain this error : **Fatal error: Uncaught Error: Call to a member function execute() on boolean in /var/www/html/App/www/index.php:59 Stack trace: #0 {main} thrown in /var/www/html/App/www/index.php on line 59** The line 59 is : **$results = $stmt1->execute();** – Caroline Apr 13 '17 at 13:48
  • @Caroline: on test ot test.exe ? – OldPadawan Apr 13 '17 at 13:58
  • @OldPadawan I don't understand ? – Caroline Apr 13 '17 at 14:01
  • @Caroline: gave you 2 pages to test [HERE](https://pastebin.com/u0g757gE) : test-gandi.php and test-gandi.exe.php. maybe you can run those 2 pages on your server and see what happens (just have to use your own credentials for your server). Which one returns error ? – OldPadawan Apr 13 '17 at 14:04
  • @OldPadawan test-gandi.php => **Fatal error: Uncaught Error: Call to a member function execute() on boolean in /var/www/html/App/www/index.php:59 Stack trace: #0 {main} thrown in /var/www/html/App/www/index.php on line 59 The line 59 is : $results = $stmt1->execute();** – Caroline Apr 13 '17 at 14:40
  • @Caroline : have you tried my online test ? as it works, with (as far as I know), same DB structure, same names and code, I'm stuck... I don't even know what this last error means :/ – OldPadawan Apr 13 '17 at 14:46
  • test-gandi.exe= > Notice: Undefined index: nom in /var/www/html/App/www/index.php on line 53 Notice: Undefined index: prenom in /var/www/html/App/www/index.php on line 54 Notice: Undefined index: email in /var/www/html/App/www/index.php on line 55 [ / / ] Notice: Trying to get property of non-object in /var/www/html/App/www/index.php on line 76 Fatal error: Uncaught Error: Call to a member function get_warnings() on boolean in /var/www/html/App/www/index.php:77 Stack trace: #0 {main} thrown in /var/www/html/App/www/index.php on line 77 – Caroline Apr 13 '17 at 15:03
  • no error on test.php but ajax doesn't pass data to 2nd page... unreal ! have you tired gandi support ? – OldPadawan Apr 13 '17 at 15:06
  • Your online test is : "http://www.at-hosting.com/oldPadawan/test-gandi.php" ? I try and this returns me my informations : [Caroline / Caroline / caroline@gmail.com] Array () true ... But its not with my db.. – Caroline Apr 13 '17 at 15:06
  • yes, so you can see DB and script are ok, therefore, ajax data isn't sent / received to/on 2nd page (I made DB with : utf8 / 1st column is INT + AI / all others are VARCHAR 250) what is your structure ? – OldPadawan Apr 13 '17 at 15:07
  • Yes, I called them several times but they said that I am admin of this server so they can not help me and they also said they can not fix my coding errors because they just solve problems about servers. – Caroline Apr 13 '17 at 15:12
  • I made DB with "latin1_swedish_ci" / 1st column INT + AI / all others are VARCHAR between 50 and 100 "latin1_swedish_ci" causes a problem ? – Caroline Apr 13 '17 at 15:16
  • we need a nice sysadmin or php guru now... :( because our DB structures look like sisters ! – OldPadawan Apr 13 '17 at 15:17
  • no way they made mistakes on credentials ? like "localhost" but it's not ? – OldPadawan Apr 13 '17 at 15:18
  • Yes I will try to found someone like that :/ – Caroline Apr 13 '17 at 15:19
  • No... my user have a hostname : localhost with all privileges / grant – Caroline Apr 13 '17 at 15:21
  • @Caroline : please this`$query = " INSERT INTO utilisateur (`numUtilisateur`, `login`, `nom`, `prenom`, `email`, `password`, `tel`, `civilite`, `adresse`, `profession`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) (with backticks around names, that don't show up here) ";` then `$id= ""; $stmt->bind_param("ssssssssss", $id, $login, $nom, $prenom, $email, $pwd, $tel, $civil, $address, $prof);` and prepare query. I wonder if *your* server does not require **all** of the column names. Then, try again, with no backtick this time – OldPadawan Apr 13 '17 at 17:11

0 Answers0