2016-12-25 15 views
1

PHPからAjaxを使用して、入力から取得した値をphpmyadminのテーブルに挿入しています。私はキャンプで働いていますが、私は入力の価値を得ることに問題があります。ここでAjax/PHP:データベースの更新

は私の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

+1

'id'には入力要素がありません: 'var cin = document.getElementById( "cin")。value; '。 'id'ではなく 'name'を設定しました – Jeff

+0

クエリテキストに構文エラーがあります。 –

+0

私はIDを変更しましたが、まだ動作しません –

答えて

0

はあなたのを修正しますSQLステートメント:

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

'$ mot_de_passe' '部分。かっこを閉じる前に一重引用符を付ける必要があります。 $ cinが数値であると仮定します。

+0

SQL文を変更しましたが動作しません リンクの画像を見てください –

+0

XMLHttpRequestはhttp://localhost/amir/inscrireClient.phpを読み込めません?cin = 123654789&nom = aaa&mot_de_passe = aaa。要求されたリソースに「Access-Control-Allow-Origin」ヘッダーが存在しません。したがって、原点「ヌル」はアクセスが許可されません。 –

+0

メソッドを許可しようとしましたか?特にあなたのケースではGET:ヘッダー( 'アクセス制御許可メソッド'、 'PUT、GET、POST、DELETE、OPTIONS');そして、ヘッダー関数をPHPスクリプトの先頭に置く - すべての前に。 – smozgur

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(); 
+0

コード専用の回答はおそらく最適ではありません。コードが何をしているのか/問題を解決するためにどのように役立つのか説明してください。ありがとう。 – Striezel

関連する問題