0

我在使用 mysqli_real_escape_string 时遇到语法错误。我使用的 IDE 没有显示任何语法问题,但是这是我第一次尝试将这个函数与多个 CASE 语句一起使用(事实上我根本没有使用过这个函数)。

插入代码在这里:

$query = "INSERT INTO `auctionwp_categories` 
         (`CategoryID`, `CategoryLevel`, `CategoryName`, `CategoryParentID`, `LeafCategory`, `AutoPayEnabled`, `BestOfferEnabled`, `Expired`, `IntlAutosFixedCat`, `Virtual`, `LSD`, `ORPA`)
  VALUES (
          '".mysqli_real_escape_string($link, $xmlCategoryID)."', '".mysqli_real_escape_string($link, $xmlCategoryLevel).", '".mysqli_real_escape_string($link, $xmlCategoryName).", '".mysqli_real_escape_string($link, $xmlCategoryParentID).",
          CASE '".mysqli_real_escape_string($link, $xmlLeafCategory)."' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '".mysqli_real_escape_string($link, $xmlAutoPayEnabled)."' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '".mysqli_real_escape_string($link, $xmlExpired)."' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '".mysqli_real_escape_string($link, $xmlBestOfferEnabled)."' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '".mysqli_real_escape_string($link, $xmlIntlAutosFixedCat)."' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '".mysqli_real_escape_string($link, $xmlVirtual)."' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '".mysqli_real_escape_string($link, $xmlLSD)."' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '".mysqli_real_escape_string($link, $xmlORPA)."' WHEN 'true' THEN 1 ELSE 0 END
          )";

if (mysqli_query($link, $query)) {
echo "Successfully inserted " . mysqli_affected_rows($link) . " row";
} else {
echo "Error occurred: " . mysqli_error($link);
}

通过配置文件中的此代码成功连接到 db:

$user="root";
$pass="";
$database="auctionwp";
$server="localhost";

$link = mysqli_connect($server, $user, $pass, $database);

if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}

echo 'Success... ' . mysqli_get_host_info($link) . "\n";

我的语法问题是由于 CASE 语句吗?我无法在网上或 php 手册中找到任何示例显示 mysqli_real_escape_string 在插入时使用此语句,

问候马丁

4

2 回答 2

0

如果我真的必须这样做,我仍然会使用SQL IF操作,如下所示:

IF [条件] THEN [语句] ELSE [语句] END IF

但是既然你已经在 php 工作了,为什么还要麻烦呢?!在 php 中查找 if 语句;)

$query = "INSERT INTO `auctionwp_categories` (`CategoryID`, `CategoryLevel`, `CategoryName`, `CategoryParentID`, `LeafCategory`, `AutoPayEnabled`, `BestOfferEnabled`, `Expired`, `IntlAutosFixedCat`, `Virtual`, `LSD`, `ORPA`) VALUES (   
    '".mysqli_real_escape_string($link, $xmlCategoryID)."',
    '".mysqli_real_escape_string($link, $xmlCategoryLevel).",
    '".mysqli_real_escape_string($link, $xmlCategoryName).",
    '".mysqli_real_escape_string($link, $xmlCategoryParentID).",
    ".mysqli_real_escape_string($link, $xmlLeafCategory) == 'true' ? 1 : 0.",
    ".mysqli_real_escape_string($link, $xmlAutoPayEnabled) == 'true' ? 1 : 0.",
    ".mysqli_real_escape_string($link, $xmlExpired) == 'true' ? 1 : 0.",
    ".mysqli_real_escape_string($link, $xmlBestOfferEnabled) == 'true' ? 1 : 0.",
    ".mysqli_real_escape_string($link, $xmlIntlAutosFixedCat) == 'true' ? 1 : 0.",
    ".mysqli_real_escape_string($link, $xmlVirtual) == 'true' ? 1 : 0.",
    ".mysqli_real_escape_string($link, $xmlLSD) == 'true' ? 1 : 0.",
    ".mysqli_real_escape_string($link, $xmlORPA) == 'true' ? 1 : 0."
)";
于 2012-11-18T13:30:57.440 回答
0

感谢您的回复,在我有机会看到有人回复之前,我设法让它工作保持 CASE 声明完整,一旦我明白了它实际上很容易。我所做的是:

// escape the returned values
$xmlCategoryName = mysqli_real_escape_string($link, $xmlCategoryName);
$xmlLeafCategory = mysqli_real_escape_string($link, $xmlLeafCategory);
$xmlBestOfferEnabled = mysqli_real_escape_string($link, $xmlBestOfferEnabled);
$xmlAutoPayEnabled = mysqli_real_escape_string($link, $xmlAutoPayEnabled);
$xmlExpired = mysqli_real_escape_string($link, $xmlExpired);
$xmlIntlAutosFixedCat = mysqli_real_escape_string($link, $xmlIntlAutosFixedCat);
$xmlVirtual = mysqli_real_escape_string($link, $xmlVirtual);
$xmlLSD = mysqli_real_escape_string($link, $xmllSD);
$xmlORPA = mysqli_real_escape_string($link, $xmlORPA);


// the insert query for the values
$query = "INSERT INTO `auctionwp_categories` 
         (`CategoryID`, `CategoryLevel`, `CategoryName`, `CategoryParentID`, `LeafCategory`, `AutoPayEnabled`, `BestOfferEnabled`, `Expired`, `IntlAutosFixedCat`, `Virtual`, `LSD`, `ORPA`)
  VALUES (
          '".$xmlCategoryID."', '".$xmlCategoryLevel."', '".$xmlCategoryName."', '".$xmlCategoryParentID."',
          CASE '$xmlLeafCategory' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '$xmlAutoPayEnabled' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '$xmlExpired' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '$xmlBestOfferEnabled' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '$xmlIntlAutosFixedCat' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '$xmlVirtual' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '$xmlLSD' WHEN 'true' THEN 1 ELSE 0 END,
          CASE '$xmlORPA' WHEN 'true' THEN 1 ELSE 0 END
          )";

一切似乎都按要求正常工作。

于 2012-11-19T08:35:15.130 回答