我已经测试了我与数据库的连接并且它很好,直到我尝试插入数据库...并且我收到错误No Database Selected。提示/帮助将不胜感激,因为我仍然是PHP的新手!
我的代码如下:
<?php
//connect to the database
$con = new mysqli("localhost", "admin", "quixtar1");
$con->select_db("attendance");
//display success or failure
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$ibonum = $_POST['num'];
$course = $_POST['course'];
$insert = "INSERT INTO tbltest (num, course) VALUES ($ibonum, $course)";
$result = mysql_query($insert);
if($result){
echo("Success!");
}
else{
echo ("Fail! <br/>" . mysql_error());
}
?>
答案 0 :(得分:2)
你的代码应该是
<?php
//connect to the database
$con = new mysqli("localhost", "admin", "quixtar1", "attendance");
//display success or failure
if ($con){
echo "Failed to connect to MySQL: " . $con->connect_error;
exit;
}
$ibonum = $con->real_escape_string(htmlspecialchars($_POST['num']));
$course = $con->real_escape_string(htmlspecialchars($_POST['course']));
$insert = "INSERT INTO tbltest (num, course) VALUES ('$ibonum', '$course')";
$result = $con->query($insert);
if($result){
echo("Success!");
}
else{
echo ("Fail! <br/>" . $con->error);
}