gpt4 book ai didi

php - 从 $GET 将 ip 插入 mysql php

转载 作者:行者123 更新时间:2023-11-28 23:47:59 24 4
gpt4 key购买 nike

您好,我正在尝试将数据插入数据库,但在获取 IP 地址时出现错误,似乎是 IP 中的小数位导致了错误。

INSERT INTO farms (name, ip, amount) VALUES (somename, 123.32.32.1, 432432) SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.32.1, 432432)' at line 2

$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'myDB';
$table = 'myTable';

$name = $_GET['name'];
$ip = $_GET['ip'];
$amount = $_GET['amount'];

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO $table (name, ip, amount)
VALUES ($name, $ip, $amount)";
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;

最佳答案

$sql = "INSERT INTO $table (name, ip, amount)
VALUES ('$name', '$ip', $amount)";

您错过了 char/varchar 的引号。这可能是问题所在

关于php - 从 $GET 将 ip 插入 mysql php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33226852/

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