如果代码没有井井有条,请先原谅我,我试图以一种整洁的方式插入代码,但是当我修复它时,系统超时,所以我不能发布,不得不重新做一遍..所以这次我不打算调整代码..只是将它复制到论坛中
我创建了一个名为 product_insert.html.. 和一个名为 product_insert.php 的 php 脚本。它们都位于 xampp 的 htdoc 文件夹中名为 Final Exam 的子文件夹中。
将数据输入表单后,下一个屏幕基本上显示了 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>