dtqu72830 2015-09-22 15:41
浏览 70
已采纳

没有选择数据库phpmyAdmin [关闭]

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

  • 写回答

1条回答 默认 最新

  • dtvfshi5248 2015-09-22 16:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失