我一直在研究一个简单的项目,我正在尝试通过从HTML表单获取用户输入来将记录插入以及更新到数据库中。然后,我将通过PHP程序连接到SQL数据库,然后使用用户输入的变量将记录更新或插入到mysql数据库中。最初,这段代码很好,我能够将记录插入表中,但是由于某种原因导致了它的中断。
//create sql connection
$conn = mysqli_connect("localhost", $username, $password, $dbName) or
die("cannot connect"); //connects to $
//INSERT STATEMENT
$sql = "INSERT INTO UFO (id, month, day, year, time, location, count,
encounter) VALUES ('$idNum', '$stringMonth', '$stringDay',
'$stringYear',
'$stringTime', '$stringLocation', '$stringNumUFO', '$stringEncounter')";
if(mysqli_query($conn, $sql)){
echo "Records have been successfully added to the database";
}
else{
echo "There was an error creating the files";
}
//close connection
mysqli_close($conn);
我也有相同的问题,但有一个相同的问题,除了我用具有更新的insert语句代替。
$sql = "UPDATE UFO
SET
month = '$stringMonth', day = '$stringDay', year = '$stringYear', time
= '$stringTime',
location = '$stringLocation', count = '$stringCount', encounter =
'$stringEncounter'
WHERE
id = '$stringID'";
if(mysqli_query($conn, $sql)){
echo "This record has been successfully updated";
}
else{
echo "There was an error updating the records";
}
我不确定是什么原因导致程序中断,因为它最初完全可以更新数据库。我检查了所有变量,所有这些变量都可以通过POST数组进行调整。关于我的问题的任何建议将不胜感激。