我已经搜寻了几个小时,但仍未找到我不断收到的错误的答案。我有以下代码:
$varFirstName = $_POST['FirstName'];
$varPast = $varFirstName.' Past';
$varPresent = $varFirstName.' Present';
$varPastandPresent = $varFirstName.' Past and Present';
$CharacterID = "SELECT CharacterID FROM CHARINFO WHERE FirstName='$varFirstName'";
$sql="INSERT INTO CHARACTER_RELATIONS (RelationshipWith, CharacterID)
VALUES('$varPast', '$CharacterID'),
('$varPresent', '$CharacterID'),
('$varPastandPresent', '$CharacterID')";
mysqli_query($con,$sql)
or die('Error: ' . mysql_error());
每次运行此命令时,我都会不断收到“错误:”。如果我使$ CharacterID = 49;然后它就可以正常工作,所以我觉得“从CHARINFO WHERE FirstName ='$ varFirstName'中选择字符ID”出了点问题,但我不知道是什么。有什么建议吗?
最佳答案
$ CharacterID =“从CHARINFO WHERE firstName ='$ varFirstName'中选择CharacterID”; //这将返回一个数组对象。因此,您必须使用mysqli_fetch_array()检索每行的值,如下所示
// print_r($ CharacterID); exit(); //检查数组值,然后执行$ CharacterID。即mysqli_query($ con,$ CharacterID);
$varFirstName = $_POST['FirstName'];
$varPast = $varFirstName.' Past';
$varPresent = $varFirstName.' Present';
$varPastandPresent = $varFirstName.' Past and Present';
$Character= "SELECT CharacterID FROM CHARINFO WHERE FirstName='$varFirstName'";// this returns an array object
$CharacterID= mysqli_query($con,$Character);
while($row=mysqli_fetch_array($CharacterID)){
$id=$row['CharacterID '];
$sql="INSERT INTO CHARACTER_RELATIONS (RelationshipWith, CharacterID)
VALUES('$varPast', '$id')";
mysqli_query($con,$sql)
or die('Error: ' . mysqli_error($con));
}