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.

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

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应