【问题标题】:Troubles using mysql insert from php从 php 使用 mysql 插入的问题
【发布时间】:2016-05-31 07:27:59
【问题描述】:

所以我在 php 上使用一个小脚本来管理网页,并且我要进行项目注册,所以我获取所有参数并将它们发送到脚本以构建数据库 (mysql) 的 INSERT。这是我的一段代码:

//Getting the params
    $title = $_POST["title"];
    $date = $_POST["date"];
    $hour = $_POST["hour"];
    $description = $_POST["description"];
    $link = $_POST["link"]; 
    $speaker = $_POST["speaker"];
    $site = $_POST["site"];
    $file = $_POST["file"];
//Link and File are optional, so I'll be using NULL instead if they're empty
    $link = !empty($link) ? ("'".$link."'") : ("'". NULL ."'");
    $file = !empty($file) ? ("'".$file."'") : ("'". NULL ."'");
//Now I'm ready to build the query
    $query = "INSERT INTO ".$type;
    $query = $query . "(title,data,hour,description,link,speaker,site,file)";
    $query = $query . "VALUES (";
    $query = $query . "'" .$title."'";
    $query = $query . ",'".$date."'";
    $query = $query . ",'".$hour."'";
    $query = $query . ",'".$description."'";
    $query = $query . ",".$link;
    $query = $query . ",'".$speaker."'";
    $query = $query . ",'".$site."'";
    $query = $query . ",".$file.")";
//Finally, I'll be sending the INSERT as a query using:
    $result = mysql_query($query);
    if(!$result)
        echo "SQL Error"

所以,我总是进入错误语句。 我在同一网页上的其他脚本中插入了其他脚本,它们运行良好,这个模仿了它们。我已经检查过:

  1. mysql_connect() 和 mysql_select_db() 都可以
  2. 我使用的数据库用户有权执行 INSERT
  3. 数据库连接(使用 SELECT 查询检查)

任何提示将不胜感激。

[已解决] 字符串没有转义,所以引号破坏了查询。因此,如果您仍然存在这种问题并使用已弃用的 mysql _ API,您可能还需要 mysql_escape_string 方法(检查Escaping single quote in PHP when inserting into MySQL)。

【问题讨论】:

  • 输入你的表格结构以及表格的编码
  • 停止使用已弃用的mysql_ API。使用 mysqli_PDO 代替准备好的语句
  • 调用mysql_error()函数,找出错误所在。
  • $type 变量来自哪里。您的代码也可用于 sql 注入
  • @ytturi,你有没有在下面检查我的答案并尝试过。

标签: php mysql error-handling


【解决方案1】:

使用这些行。

$link = !empty($link) ? $link : NULL;
$file = !empty($file) ? $file  :NULL ;

PHP null 是NULL 不带引号也不要在$link 和$file 中使用额外的“”。

在运行query 之前尝试打印它并在PhpMyadmin 中运行

【讨论】:

  • 嘿,谢谢你的回答。正如对@Ms.Nehal 的回答,我错过了从我的原始代码转录,我没有为NULL 值使用引号,但我为连接字符串添加了引号。很抱歉,我现在正在编辑它。
【解决方案2】:

您遇到了串联问题。尝试使用以下代码更改您的代码行:

 //Link and File are optional, so I'll be using NULL instead if they're empty
  $link = !empty($link) ? ($link) : (NULL);
  $file = !empty($file) ? ($file) : (NULL);
//Now I'm ready to build the query
  $query = "INSERT INTO $type (title,data,hour,description,link,speaker,site,file) VALUES ('" .$title."','".$date."'
           ,'".$hour."','".$description."','".$link."','".$speaker."','".$site."','".$file."')";

此外,NULL 永远不会作为字符串传递。否则,将被视为 一个字符串。

【讨论】:

  • 是的,谢谢。我在此页面的其他实例中看到了同样的错误。我想我在从 cmets 中删除敏感数据时出现了一些转录错误,我的空链接/文件行是: $link = !empty($link) ? ("'".$link."'") : ("'".NULL ."'"); $file = !empty($file) ? ("'".$file."'") : ("'".NULL ."'");因此,在发送的查询中,如果您打印字符串,当值为 NULL 时,结果将显示为 ''。
  • @ytturi,很抱歉你想说的不清楚。请您详细说明一下
  • 我在写这篇文章时出错了。连接运行良好,NULL 值被正确发送。
  • @ytturi,所以你已经解决了你的问题。所以最好你应该发布并接受它,这显示了问题的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 2010-11-20
  • 1970-01-01
  • 2015-10-21
相关资源
最近更新 更多