gpt4 book ai didi

php - 插入mysql不工作

转载 作者:行者123 更新时间:2023-11-29 01:27:19 25 4
gpt4 key购买 nike

第一次在这里发帖,是 php 编程的新手。

我正在尝试将 $val(2) 插入到 mysql 数据库中。我的代码如下:

$username = "xxx";
$password = "xxx";
$hostname = "xxx";
$val= "2";

$connection=mysql_connect ($hostname, $username, $password)
or die("Unable to connect to MySQL");

$selected = mysql_select_db("database",$connection)
or die("Could not select examples");

$url = "http:xxxxxxxxxxxxx.com"; //url here
$str = file_get_contents($url);

$sql = "INSERT INTO testtable (data)
VALUE ('".$val."')";

$ins= mysql_query($test);
if(! $ins )
{
die('Could not insert: ' . mysql_error());
}

当我运行代码时,没有打印出任何错误,但没有数据被插入到表中。有人可以指出问题。我将不胜感激。

附录:

我没有包括这些行:

$url = "http:xxxxxxxxxxxxx.com";    //url here
$str = file_get_contents($url);

在我的第一篇文章中。

真正的解决办法是改变:

$test to $sql

并注释掉下面两行

file_get_content($url) 
$str = file_get_contents($url);

这两行必须放在顶部(在 $username= "xxx"行之前)才能工作。为误导性问题道歉。

最佳答案

这里有一个 SQL 错误:

$sql = "INSERT INTO testtable (data)
VALUE ('".$val."')";

VALUE 应该是 VALUES

当某些事情没有按预期工作时,您应该始终检查错误(在本例中为 mysql_error)。

此外,您不应再使用mysql 扩展。它已被弃用多年。查看 mysqliPDO以获得更好、更安全的扩展。

关于php - 插入mysql不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36253221/

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