我的PHP代码中出现了一个SQL语句问题。
我想取回我的Indice的ID,我的名字='myname'。 这是我的代码:
<?php
include 'Connection.php';
try {
$db = new PDO("$server:host=$host;dbname=$base", $user, $passwd);
//Statement = INSERT INTO indice
$stmtInd = $db->prepare("INSERT INTO indice(ID, Name, IDFormation)
VALUES (:ID, :Name, :IDFormation)");
$stmtInd->bindParam(':ID', $id);
$stmtInd->bindParam(':Name', $name);
$stmtInd->bindParam(':IDFormation', $idformation);
//Statement = INSERT INTO note
$stmtNote = $db->prepare("INSERT INTO note(ID, Valeur, Valeurtext, IDIndice)
VALUES (:ID, :Valeur, :Valeurtext, :IDIndice)");
$stmtNote->bindParam(':ID', $ID);
$stmtNote->bindParam(':Valeur', $valeur);
$stmtNote->bindParam(':Valeurtext', $valeurtext);
$stmtNote->bindParam(':IDIndice', $IDindice);
$noteIdindice = $db->prepare("SELECT ID FROM indice WHERE Name = :Name");
$noteIdindice->bindParam(':Name', $name);
$noteIdindice->execute();
$resultat = $noteIdindice->fetch(\PDO::FETCH_ASSOC);
var_dump($resultat);
//Indice 1
$name = "Equilibre theorie / pratique";
$idformation = "1";
$stmtInd->execute();
$valeur = $_POST["indice1"];
$valeurtext = "";
$IDindice = $resultat['ID'];
$stmtNote->execute();
echo "Success";
}
catch (PDOException $e) {
die("Impossible de se connecter a la source de donnees...");
}
?>
还有其他Indice,但你不需要它,因为它与“// Indice 1”相同。
一切正常,我没有失败。但我的查询给了我错误的回报。它返回“0”而不是我想要的ID。
你们知道为什么吗?
答案 0 :(得分:2)
您的预准备语句永远不会被执行,您应该添加执行:
AppRegistry.registerComponent('XYZ', () => XYZ);
编辑:
您正在尝试使用空值绑定params。
$noteIdindice = $db->prepare("SELECT ID FROM indice WHERE Name = :Name");
$noteIdindice->bindParam(':Name', $name);
$noteIdindice->execute();//Add this row
$resultat = $noteIdindice->fetch();
尝试这样做:
$stmtInd = $db->prepare("INSERT INTO indice(ID, Name, IDFormation)
VALUES (:ID, :Name, :IDFormation)");
$stmtInd->bindParam(':ID', $id);
$stmtInd->bindParam(':Name', $name); //$name variable not exists
$stmtInd->bindParam(':IDFormation', $idformation);//$idformation variable not exists