gpt4 book ai didi

php - 我的查询出了什么问题?

转载 作者:行者123 更新时间:2023-11-29 10:39:43 24 4
gpt4 key购买 nike

我的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);

最佳答案

使用这个:

$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);

但请注意,您应该在临时文件中使用这些代码。仅供使用一次!如果您想将数据放入数据库,请创建一个像 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>

关于php - 我的查询出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45733450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com