使用PHP插入mysql数据库的问题

时间:2011-01-16 00:20:43

标签: php mysql

我有一个奇怪的问题,我有一个用于将数据插入Mysql DB的PHP页面。 问题是,当我执行代码时,虽然我设置了显示错误代码

,但没有添加到db并且没有出现错误
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

关于这个问题的任何想法!

这是我用过的代码

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}


include("Connections/mzk_mdc.php");
                $ext = 1;
                $website = "mzk";
                $mzk_sql=sprintf("INSERT INTO downloads (image, `by`, `rapid_title`, title, `description`, category, div_id, topic_url, down_times, ext, `website`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                    GetSQLValueString($topic_thumb_image, "text"),
                     GetSQLValueString($topic_by, "text"),
                       GetSQLValueString($topic_des, "text"),
                       GetSQLValueString($topic_title, "text"),
                     GetSQLValueString($forum_content, "text"),
                     GetSQLValueString($topic_category, "text"),GetSQLValueString($topic_div, "text"),GetSQLValueString($forum_link, "text") ,GetSQLValueString($topic_down_times, "int"),GetSQLValueString($ext, "int"), GetSQLValueString($website, "text"));
                       mysql_select_db($database_mdc, $mdc);
                       $mzk_result = mysql_query($mzk_sql, $mdc) or die("can not do more");

                       mysql_close($mdc);

3 个答案:

答案 0 :(得分:0)

您是否尝试过使用mysql_error()?

mysql_query('SHOW TABLES')或死(mysql_error());

答案 1 :(得分:0)

  $theValue = ($theValue != "") ? intval($theValue) : "NULL";

如果$ theValue为0(零),则会插入NULL。 PHP类型转换0,“”,“',null和各种其他值,因为它们都相同。也许这就是你想要的,但是我很难看到如何将合法的'0'变成SQL NULL只是一个坏主意。

同样,在defined情况下,您没有转义$theDefinedValue$theUndefinedValue,因此这些仍可能导致SQL注入,除非您正在进行转义在函数调用之前。

您是否在代码中添加了一些调试回显?也许它甚至没有达到你的数据库操作,因为它在mzk_mdc.php文件中爆炸了。至少让代码在您进行query()调用之前回显最终查询,看看它是否正常生成。通过mysql监视器手动运行它,看看会发生什么。

答案 2 :(得分:0)

首先 - 简化它:

    if(''==$value) 
        $value = 'NULL'; //if you want that NULL in query
     else 
        $value = function_exists('mysql_real_escape_string') ? mysql_real_escape_string($value) :mysql_escape_string($value); //It's enough to run that

然后尝试打印出mysql_error()和var_dump()查询和查询结果。