我使用php创建了一个表,它是在我的数据库中创建的(我已经看过了),然后我将代码连接到localhost,但是当我尝试使用sql_query来处理变量时,它也成功了在表格中,它给了我"没有选择数据库"!
表格声明:
require "E:/E-Commerce/OnlineStore/StoreScript(dynamic)/ConnectToMySQL.php";
//Won't exceed unless everything is fine with the "required"
$sqlCommand = "CREATE TABLE products (
id int(11) NOT NULL AUTO_INCREMENT,
productName varchar(255) NOT NULL,
productPrice varchar(16) NOT NULL,
productDetails text NOT NULL,
category varchar(16) NOT NULL,
subCategory varchar(16) NOT NULL,
dateAdded date NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY productName (productName)
) ";
echo 'done ';
代码本身:
if (isset($_POST['productName'])) {
$productName = mysql_real_escape_string($_POST['productName']);
echo 'name ';
$productPrice = mysql_real_escape_string($_POST['productPrice']);
echo 'price ';
$category = mysql_real_escape_string($_POST['category']);
echo 'category ';
$subCategory = mysql_real_escape_string($_POST['subCategory']);
echo 'subcategory ';
$productDetails = mysql_real_escape_string($_POST['productDetails']);
echo 'details ';
// See if that product name is an identical match to another product in the system
$sql = mysql_query("SELECT id FROM products WHERE productName='$productName' LIMIT 1");
echo ' $productName= '.$productName;
echo ' got the id from the table ';
$productMatch = mysql_num_rows($sql); // count the output amount
echo ' performed matching (num rows) ';
if ($productMatch > 0) {
echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
exit();
}
// Add this product into the database now
include_once "E:\E-Commerce\OnlineStore\StoreScript(dynamic)\CreateProductsTable.php";
$sql = mysql_query("INSERT INTO products (productName, productPrice, productDetails, category, subCategory, dateAdded)
VALUES('$productName','$productPrice','$productDetails','$category','$subCategory',now())",$con) or die (mysql_error());
echo ' Inserted the variables in the database ';
$pid = mysql_insert_id();
echo ' got the id ';
// Place image in the folder
$newname = "$pid.jpg";
echo ' saved the image in a variable ';
move_uploaded_file( $_FILES['FileField']['tmp_name'], "../InventoryImages/$newname");
echo ' saved the image in a folder ';
header("location: InventoryList.php");
echo ' refreshed ';
exit();
}
ConnectToMySQL代码:
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="store";
/*This is a function that connects the code to the online server with the host, username, and password given, or it will display the message associated with die()*/
$con=mysqli_connect($db_host,$db_username,$db_pass, $db_name)or die("Could not connect to mySQL");
echo ' connected ';
显示所有回显的行,直到执行匹配(num rows)&#39;并且&#39;完成&#39;在表声明中,它给了我&#34;没有选择数据库&#34; !!
任何想法