我的查询中有什么问题?

时间:2022-06-10 00:03:53

My mysql db is empty, no table created and data written on it, help please, thanks! Here it is, sorry im a newbie:

我的mysql数据库是空的,没有创建表格,数据写在上面,请帮忙,谢谢!在这里,对不起,我是一个新手:

$servername = "server";
$username = "username";
$password = "pass";
$dbname = "db name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// sql to create table
$sql = "CREATE TABLE IF NOT EXISTS data (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
link text NOT NULL,
title text NOT NULL,
description text NOT NULL,
internal_link text NOT NULL,
 eg_date TIMESTAMP
)ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1";
$sql = "INSERT INTO data (title) 
VALUES 
('$title')";

mysqli_close($conn);

3 个解决方案

#1


1  

use this :

用这个 :

$servername = "server";
$username = "username";
$password = "pass";
$dbname = "db name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}else{
    // sql to create table
    $sql = "CREATE TABLE IF NOT EXISTS data (
id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
link text NOT NULL,
title text NOT NULL,
description text NOT NULL,
internal_link text NOT NULL,
eg_date TIMESTAMP) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1";
    if(mysqli_query($conn,$sql)){
        $sql = "INSERT INTO data (title) VALUES ('".$title."')";
        mysqli_query($conn,$sql);
    }else{
        echo "Error creating table: " . mysqli_error($conn)."<br/>";
    }

}
mysqli_close($conn);

But attention that you should use these codes in a temporary file. Use just for one time! if you want to put data on your db , then create a php file like yourpage.php to be your panel. then create a panel in that. so you can insert your data on your table , so easy. here is a simple sample :

但要注意你应该在临时文件中使用这些代码。只使用一次!如果你想在你的数据库上放置数据,那么创建一个像yourpage.php这样的php文件作为你的面板。然后在其中创建一个面板。所以你可以在桌面上插入数据,这很简单。这是一个简单的示例:

<?php
$server_name='';
$username='';
$password='';
$db_name='';
$conn=mysqli_connect($server_name,$username,$password,$db_name);
if(isset($_GET['add_record'])){
    if(isset($_GET['title']) && $_GET['title']!=NULL){
        if(isset($_GET['description']) && $_GET['description']){
            // TODO : for security reason I use these filters for getting strings
            $title=mysqli_real_escape_string($conn,htmlspecialchars($_GET['title']));
            $des=mysqli_real_escape_string($conn,htmlspecialchars($_GET['description']));
            $sql="INSERT INTO data(title,description) VALUES ('".$title."','".$des."')";
            mysqli_query($conn,$sql);
            header("location:yourpage.php");
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<form method="get">
    <input type="text" name="title" placeholder="title">
    <input type="text" name="description" placeholder="description">
    <input type="submit" name="add_record" value="Save it!">
</form>
</body>
</html>

#2


0  

You must execute the query via mysqli_query() which is nowhere in your code.

您必须通过代码中没有的mysqli_query()执行查询。

Docs: http://php.net/manual/pl/mysqli.query.php

文件:http://php.net/manual/pl/mysqli.query.php

#3


0  

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

 // Create connection using
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
 die("Connection failed: " . mysqli_connect_error());
 }

 // sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

 // for inserting the values

if (mysqli_query($conn, $sql)) {
 echo "Table MyGuests created successfully";
} else {
 echo "Error creating table: " . mysqli_error($conn);
}

    $sql_qry="INSERT INTO data (title)  VALUES ('$title')";


 if (mysqli_query($conn, $sql_qry)) {
 echo "values added successfully";
} else {
 echo "data can't be added";
}    


mysqli_close($conn);
?>

try this and change the connection code and everything with your code.. good luck.

试试这个并用你的代码更改连接代码和所有内容..祝你好运。

#1


1  

use this :

用这个 :

$servername = "server";
$username = "username";
$password = "pass";
$dbname = "db name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}else{
    // sql to create table
    $sql = "CREATE TABLE IF NOT EXISTS data (
id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
link text NOT NULL,
title text NOT NULL,
description text NOT NULL,
internal_link text NOT NULL,
eg_date TIMESTAMP) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1";
    if(mysqli_query($conn,$sql)){
        $sql = "INSERT INTO data (title) VALUES ('".$title."')";
        mysqli_query($conn,$sql);
    }else{
        echo "Error creating table: " . mysqli_error($conn)."<br/>";
    }

}
mysqli_close($conn);

But attention that you should use these codes in a temporary file. Use just for one time! if you want to put data on your db , then create a php file like yourpage.php to be your panel. then create a panel in that. so you can insert your data on your table , so easy. here is a simple sample :

但要注意你应该在临时文件中使用这些代码。只使用一次!如果你想在你的数据库上放置数据,那么创建一个像yourpage.php这样的php文件作为你的面板。然后在其中创建一个面板。所以你可以在桌面上插入数据,这很简单。这是一个简单的示例:

<?php
$server_name='';
$username='';
$password='';
$db_name='';
$conn=mysqli_connect($server_name,$username,$password,$db_name);
if(isset($_GET['add_record'])){
    if(isset($_GET['title']) && $_GET['title']!=NULL){
        if(isset($_GET['description']) && $_GET['description']){
            // TODO : for security reason I use these filters for getting strings
            $title=mysqli_real_escape_string($conn,htmlspecialchars($_GET['title']));
            $des=mysqli_real_escape_string($conn,htmlspecialchars($_GET['description']));
            $sql="INSERT INTO data(title,description) VALUES ('".$title."','".$des."')";
            mysqli_query($conn,$sql);
            header("location:yourpage.php");
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<form method="get">
    <input type="text" name="title" placeholder="title">
    <input type="text" name="description" placeholder="description">
    <input type="submit" name="add_record" value="Save it!">
</form>
</body>
</html>

#2


0  

You must execute the query via mysqli_query() which is nowhere in your code.

您必须通过代码中没有的mysqli_query()执行查询。

Docs: http://php.net/manual/pl/mysqli.query.php

文件:http://php.net/manual/pl/mysqli.query.php

#3


0  

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

 // Create connection using
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
 die("Connection failed: " . mysqli_connect_error());
 }

 // sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

 // for inserting the values

if (mysqli_query($conn, $sql)) {
 echo "Table MyGuests created successfully";
} else {
 echo "Error creating table: " . mysqli_error($conn);
}

    $sql_qry="INSERT INTO data (title)  VALUES ('$title')";


 if (mysqli_query($conn, $sql_qry)) {
 echo "values added successfully";
} else {
 echo "data can't be added";
}    


mysqli_close($conn);
?>

try this and change the connection code and everything with your code.. good luck.

试试这个并用你的代码更改连接代码和所有内容..祝你好运。