2014-10-10 105 views
0

當我試圖插入數據庫時​​,我不斷收到此錯誤。 「mysqli_select_db()預計2個參數,1給出」mySql錯誤連接DB

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="#test"; // Database name 
$tbl_name="appoint"; // Table name 

// Connect to server and select databse. 
$con = mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
if(! $con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

$customer=$_POST['Name']; 
$assist=$_POST['Assisstant']; 
$service=$_POST['Service']; 
$date=$_POST['date']; 
$time=$_POST['time']; 

$sql="INSERT INTO $tbl_name (cus_name, emp_name, date, time, service) 
VALUES ('$customer', '$assist', '$date', '$time', '$service');"; 

mysqli_select_db("$db_name")or die("cannot select DB"); 
$retval = mysql_query($sql, $con); 
if(! $retval) 
{ 
    die('Could not enter data: ' . mysql_error()); 
} 
echo "Entered data successfully\n"; 
mysql_close($con); 
+0

**附註:**您已*開放*到MySQL注入。驗證和消毒你的輸入,並使用[binding](http://php.net/manual/en/mysqli-stmt.bind-param.php) – 2014-10-10 09:04:44

回答

3

你在程序上使用它。

From the docs;

bool mysqli_select_db (mysqli $link , string $dbname) 

所以;

mysqli_select_db($con, $db_name)or die("cannot select DB");