首先原諒我,如果代碼不是有組織的方式,我試圖以一種整潔的方式插入代碼,但當我修復它時,系統時間所以我不能發佈,並必須重新做一遍..所以這次我不會調整代碼..只是複製過去它進入論壇我的php代碼如何連接到數據庫..使用Xampp
我創建了一個從被調用的product_insert.html ..和一個名爲product_insert.php的php腳本。它們都位於名爲期末考試的子文件夾中,位於xampp的htdoc文件夾中。
將數據輸入表單後,下一個屏幕基本顯示了product_insert.php的腳本。我無法弄清楚爲什麼它沒有建立連接。該數據庫也稱爲final_exam。
我已經編輯我的代碼下面,我仍然得到錯誤
<html>
<head></head>
<body>
<?php
mysql_connect("localhost", "root", "Final exam")
or die(mysql_error());
//echo "We have successfully connect to our DB.<br/>";
mysql_select_db("final_exam") or die(mysql_error());
//echo "Successfully opened DB.<br/>";
//pull values from the URL and put them each in a variable
$Description = addslashes($_GET["Description"]);
$Quantity = addslashes($_GET["Quantity"]);
$Price = addslashes($_GET["Price"]);
$Vend_id = addslashes($_GET["Vend_id"]);
if($Description && $Quantity && $Price && $Vend_id)
{
echo "test1";
}
else
{
echo "test2";
}
if(isset($Description) && !empty($Description)
&& isset($Quantity) && !empty($Quantity)
&& isset($Price) && !empty($Price)
&& isset($Vend_id) && !empty($Vend_id))
{
$SQLstring = "INSERT INTO student (id, first_name,last_name,address, e_mail,
gpa)
VALUES (NULL, '$first', '$last', '$address', '$email', 0.0)";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("Insert Broke!!!");
echo "insert complete";
}
else
{
echo "You are missing some values...Please press the back button and retry!";
}
//redirect back to our list page since the insert worked
header("location: db_connect.php");
?>{/PHP]
<!--Insert Complete: click <a href="product_list.html">here</a> to go back to the
list!-->
</body>
</html>
我已經編輯我的代碼下面,我仍然得到錯誤
<html>
<head></head>
<body>
<?php
$host = "localhost"; // change this as required
$username = "root"; // change this as required
$password = "password"; // change this as required
$db = "final_exam"; // your DB
$DBConnect=mysql_connect("localhost", "root", "password")
or die("Could Not Connect");
//echo "We have successfully connect to our DB.<br/>";
mysql_select_db("final_exam")
or die(mysql_error());
//echo "Successfully opened DB.<br/>";
//pull values from the URL and put them each in a variable
$Description = addslashes($_GET["Description"]);
$Quantity = addslashes($_GET["Quantity"]);
$Price = addslashes($_GET["Price"]);
$Vend_id = addslashes($_GET["Vend_id"]);
if($Description && $Quantity && $Price && $Vend_id)
{
echo "test1";
}
else
{
echo "test2";
}
if(isset($Description) && !empty($Description)
&& isset($Quantity) && !empty($Quantity)
&& isset($Price) && !empty($Price)
&& isset($Vend_id) && !empty($Vend_id))
{
$SQLstring = "INSERT INTO student (id,
VALUES ('$Description', '$Quantity', '$Price', '$Vend_id')";
$QueryResult = @mysql_query($DBConnect, $SQLstring)
Or die("Insert Broke!!!");
echo "insert complete";
}
else
{
echo "You are missing some values...Please press the back
button and retry!";
}
//redirect back to our list page since the insert worked
header("location: product_list.php");
?>
<a a href="product_insert.html">Click here</a> to go back to the list!-->
</body>
</html>
爲什麼在你的代碼中使用'[PHP]'和'/ PHP]'?如果這是你的代碼,然後刪除這些。另外我注意到這個註釋'product_list.html'你的文件現在不會是'.html'而不是'.php'嗎? –
我看不到任何其他對$ DBConnect的引用。你的數據庫證書應該看起來像這樣:$ DBConnect = mysql_connect($ host,$ username,$ password)或者die(「Could not Connect To The Server」);'如果有任何東西而不是'mysql_connect(「localhost」,「root 「,」期末考試「)並使用'$ host =」localhost「; $ username =「username」; $ password =「password」; $ db =「your_db」;'---這是一個調試問題。 –
這是我迄今爲止的變化。請檢查$ DBConnect = mysql_connect(「localhost」,「root」,「」) \t \t \t \t或die(mysql_error()); \t \t \t echo「我們已成功連接到我們的數據庫。
」; \t \t \t \t \t mysql_select_db( 「final_exam」) \t \t \t \t或死亡(mysql_error()); \t \t \t echo「成功打開DB。
」; – Darius