创建和插入SQL Php表单?

时间:2018-04-19 04:17:04

标签: php mysql mysqli

我似乎遇到了代码问题,执行后,我收到了'未定义的变量:第60行'的mysqli_connect和 '在第60行'上的非对象上调用成员函数query()。

我是否可以在如何将其合并到我的代码中朝着正确的方向迈出一步?

<!-- Retrieve records from database -->
  <?php
  $db=mysqli_connect(null,null,null,'connection')
or die("Can't connect to DB:" . mysqli_connect_error());

// Create database


    // sql to create table
$sql = "CREATE TABLE state_t (
    state_abbr char(2) PRIMARY KEY,
    state_name char(20),
    state_zone integer)"

    if ($mysqli_connect->query($sql) === TRUE) {
    echo "TABLE state_t created successfully";
} else {
    echo "Error creating TABLE: " . $mysqli_connect>error;
}
$mysqli_connect->close();
    // sql to create table
$sql = "CREATE TABLE tool_t (
    tool_item_no char(10) PRIMARY KEY,
    tool_name char(20),
    tool_price numeric(6, 2),
    tool_weight numeric(4, 1),
    tool_picture char(30),
    tool_description varchar);"
    if ($mysqli_connect->query($sql) === TRUE) {
    echo "TABLE state_t created successfully";
} else {
    echo "Error creating TABLE: " . $mysqli_connect>error;
}

$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('1', 'Chisel', '4.00', '100', '/tools/chisels.jpg', 'Beautiful Chisel')";

$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('2', 'Tree Trimmers', '2.00', '50', '/tools/tree_trimmer.jpg', 'Cool Tree Trimmer')";

$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('3', 'Spanners', '2.00', '320', '/tools/spanners.jpg', 'Awesome Spanner')";

?>

2 个答案:

答案 0 :(得分:0)

尝试以下代码连接到db

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";

    // Create connection
    $conn = new mysqli($servername, $username, $password);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected successfully";
    ?>

答案 1 :(得分:0)

我已更新您的代码,请立即试用。 $mysqli_connect不是数据库连接的引用变量,因此它会给出错误Call to a member function query() on a non-object in on line 60。所以你应该建立数据库连接对象$conn

<!-- Retrieve records from database -->
 <?php
 // $db =mysqli_connect(null,null,null,'connection');
// Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


// sql to create table
$sql = "CREATE TABLE state_t (
    state_abbr char(2) PRIMARY KEY,
    state_name char(20),
    state_zone integer)"

    if ($conn->query($sql) === TRUE) {
    echo "TABLE state_t created successfully";
} else {
    echo "Error creating TABLE: " . $conn->error;
}
$mysqli_connect->close();
    // sql to create table
$sql = "CREATE TABLE tool_t (
    tool_item_no char(10) PRIMARY KEY,
    tool_name char(20),
    tool_price numeric(6, 2),
    tool_weight numeric(4, 1),
    tool_picture char(30),
    tool_description varchar);"
    if ($mysqli_connect->query($sql) === TRUE) {
    echo "TABLE state_t created successfully";
} else {
    echo "Error creating TABLE: " . $mysqli_connect>error;
}

$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('1', 'Chisel', '4.00', '100', '/tools/chisels.jpg', 'Beautiful Chisel')";

$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('2', 'Tree Trimmers', '2.00', '50', '/tools/tree_trimmer.jpg', 'Cool Tree Trimmer')";

$sql = "INSERT INTO tool_t (tool_item_no, tool_name, tool_price, tool_weight, tool_picture, tool_description)
VALUES ('3', 'Spanners', '2.00', '320', '/tools/spanners.jpg', 'Awesome Spanner')";

?>