Php I';i’’我得到了回报,我不知道为什么
我的PHP代码中有一个SQL语句问题 我想找回我的标识的ID,其中我的名字='myname'。 这是我的密码:Php I';i’’我得到了回报,我不知道为什么,php,sql,Php,Sql,我的PHP代码中有一个SQL语句问题 我想找回我的标识的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 IN
<?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...");
}
?>
还有其他标记,但您不需要它,因为它与“//标记1”相同
一切顺利,我没有失败。但我的查询给了我一个错误的回报。它返回“0”而不是我想要的ID
你们知道为什么吗?您准备的语句从未执行过,您应该添加执行:
$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
尝试这样做:
<?php
include 'Connection.php';
try {
$db = new PDO("$server:host=$host;dbname=$base", $user, $passwd);
//Indice 1
$name = "Equilibre theorie / pratique";
$idformation = "1";
$valeur = $_POST["indice1"];
$valeurtext = "";
//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);
$stmtInd->execute();
//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);
$stmtNote->execute();
$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);
$IDindice = $resultat['ID'];
echo "Success";
}
catch (PDOException $e) {
die("Impossible de se connecter a la source de donnees...");
}
?>
您使用了什么函数来打印结果?我不打印结果,我只是使用fetch()将结果放入我的变量$result中。如果不打印,您如何知道$result变量=0?完全正确!那你怎么知道?我在数据库里看到了。我做了一个insert,它将我插入0。对$resultatHow进行var\u dump
?哪里这不是js吗?在:$resultat=$noteiddice->fetch()之后
putvar\u dump($resultat)
C:\wamp64\www\Test\Insertion.php:55:boolean false看看这篇文章: