2016-12-25 147 views
1

我正在使用Ajax和PHP將從輸入中取得的值插入到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' – 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」標題。因此不允許原產地'null'訪問。 –

+0

你有沒有試過允許的方法 - 特別是在你的情況GET:頭('訪問控制允許的方法','放,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