I created a table using php and it was created in my database (I've seen it), then I connected the code to the localhost and it also succeeded, however, when I try to use sql_query to instert variables in the table, it gives me "No database selected"!
The table declaration:
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 ';
The code itself:
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();
}
The ConnectToMySQL code:
$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 ';
All the echoed lines appear until 'performed matching (num rows)' and 'done' inside the table declaration, then it gives me "No database selected"!!
Any ideas
Connecting to the server doesn't mean conneting to the databases it contains. Your problem is most likely generated by the fact that you did connect to the SQL server, but since a server can contain multiple databases, you did not specify which one the query should act on. You can either select a database by using mysql_select_db
(php code), or run a USE *database name here*
query in the SQL server (before any other query).
There is another option. No matter what database you are using, adding the database name before the table name (with a dot in the middle) will select said database for that action. For instance, SELECT * FROM Alphabet.Numbers
will select from the table "Numbers" located inside the database "Alphabet", regardless of your previously selected DB.
I hope this answer will be useful.