Ajax / PHP:更新数据库

时间:2016-12-25 13:12:20

标签: php mysql ajax

我正在使用Ajax和PHP将输入中的值插入到phpmyadmin中的表中。我在营地工作,但是我在获取输入值方面遇到了问题。

这是我的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>

这是PHP文件:

<?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";
?>

它在数据库中没有变化: alt text

error

2 个答案:

答案 0 :(得分:0)

修复你的sql语句:

public function slug(Request $request, $slug)

    {
        if (Auth::check()) {
          $fav = DB::table('post_user')->whereUserId(Auth::id())->pluck('post_id')->all();
        }
         /*get search query from slug page*/
        $query=$request->get('q');
        /*display the results of the search*/
        if ($query) {
            $posts = $query ? Post::search($query)
            ->orderBy('id','desc')
            ->paginate(7) : Post::all();
            return view('home', compact('posts','fav'));
        }
       /*the $url veriable is for share buttons*/
           else {
            $url = $request->url();
            $post = Post::where('slug', '=', $slug)->first();
            return view('b.s', compact('post','url'));

        }
    }

'$ mot_de_passe)'部分。在关闭括号之前,您应该放置单引号。假设$ cin是数字。

答案 1 :(得分:0)

// $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();