简体   繁体   中英

Ajax/PHP: Updating a Database

I am using Ajax with PHP to insert values taken from input into a table in phpmyadmin. I am working with camp, but I have a problem getting the value of input.

Here is my HTML:

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <title>inscription client</title>
    <script >
      function test() {
        r=document.getElementById('1');
        if (r.value.length !==9)
          document.getElementById('demo').innerHTML="verifer le mot de passe";
        else  document.getElementById('demo').innerHTML="";
      }
      //////////////////////////////////////////
      function ajouter()
      {
        var cin=document.getElementById("cin").value;
        var nom=document.getElementById("nom").value;
        var mot_de_passe=document.getElementById("mot_de_passe").value;
        var xmlhttp = new XMLHttpRequest();

        var url = "http://localhost/amir/inscrireClient.php?cin="+cin+"&nom="+nom+"&mot_de_passe="+mot_de_passe;
        xmlhttp.onreadystatechange=function() {

          if (this.readyState == 4 && this.status == 200)
          {

            if(this.responseText =="ok")
            {
              document.getElementById("2").innerHTML ="it woeks !";
              document.getElementById("2").style.backgroundColor="green";
            }
            else{
              document.getElementById("2").innerHTML="no";
              document.getElementById("2").style.backgroundColor="red";

            }
          };

          xmlhttp.open("GET", url, true);
          xmlhttp.send();
        }
    </script>
  </head>
  <body>

    <input   id ="cin" type="number" name="cin" value="123654789"  onblur="test()" required  >  
    <p style="color: red"   id="demo"></p> 
    <input type="text" id="nom" name="nom" value="aaa" placeholder="donner votre nom" >  <br> <br>
    <input type="password" id="mot_de_passe" name="mot_de_passe" value="aaa" placeholder="donner votre mot de passe" required=>  <br> <br>
    <button type="submit" onclick="ajouter()">s'inscrire </button> 
    <p id="2" ></p>
  </body>
</html>

And this the PHP file:

<?php
include 'param.php'; 
header("Access-Control-Allow-Origin: *"); 


$cin=$_GET['cin'];
$nom=$_GET['nom'];
$mot_de_passe=$_GET['mot_de_passe'];

try
{

  $bdd = new PDO('mysql:host='.$server.';dbname='.$database.';charset=utf8', $user, $passwd);
  $bdd->exec("SET CHARACTER SET utf8");
}

catch(Exception $e)
{
  die('Erreur : '.$e->getMessage());
}
$reponse = $bdd->exec( "insert into client(cin,nom,mot_de_passe) values ($cin,'$nom','$mot_de_passe')" );
if ($reponse->rowCount()>0) 
  echo "ok";
else        echo "non";
?>

It does not change in the database: 替代文字

error

Fix your sql statement:

$reponse = $bdd->exec( "insert into client(cin,nom,mot_de_passe) values ($cin,'$nom','$mot_de_passe)'" );

'$mot_de_passe)' part. You should put single quote before closing the parenthesis. And assuming $cin is numeric.

// $reponse = $bdd->exec( "insert into client(cin,nom,mot_de_passe) values ($cin,'$nom','$mot_de_passe')" );

$sql = "insert into client(cin, nom, mot_de_passe) values (?,?,?)";
$stmt = $bdd->prepare($sql);

$stmt->bindParam(1, $cin);
$stmt->bindParam(2, $nom);
$stmt->bindParam(3, $mot_de_passe);

$stmt->execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM