0

我想知道如何将这个 Dreamweaver 生成的代码转换为我需要在 WordPress 中用于 mySQL 表单的代码。如果我在空白页上使用我的代码(只是表单和 SQL)它工作正常。当我将其发布到模板中时,它不会。我想我在 wordPress 中使用了错误的方法。我已经看过并阅读了 wpdb 页面,但它缺乏将其连接到表单的深入细节。

此代码主要由 Dreamweaver 生成。我知道它需要更改,但不确定要换掉什么。我把这一切都包括在内是为了彻底。

<?php require_once('../../../Connections/LocalHost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
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;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
  $insertSQL = sprintf("INSERT INTO fyxt_characters (wp_userid, public_char, char_rank) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['wp_userid'], "int"),
                       GetSQLValueString($_POST['public_char'], "int"),
                       GetSQLValueString($_POST['char_rank'], "int"));

  mysql_select_db($database_LocalHost, $LocalHost);
  $Result1 = mysql_query($insertSQL, $LocalHost) or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
</form>
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Wp_userid:</td>
      <td><input type="text" name="wp_userid" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Public_char:</td>
      <td><input type="text" name="public_char" value="1" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Char_rank:</td>
      <td><input type="text" name="char_rank" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form2" />
</form>
<p>&nbsp;</p>
</body>
</html>

我怀疑 if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) { $insertSQL = sprintf("INSERT INTO fyxt_characters (wp_userid, public_char, char_rank) VALUES (%s, %s, %s)”, 是真正需要更改但不确定的内容。这是我的第一个插入表单。我在 WordPress 中完成了很多 SELECT 类型查询,但尚未使用 INSERT/UPDATE /删除东西。

非常感谢您的指导和帮助!

4

1 回答 1

0

好的,经过大量研究和一些实验,我找到了解决方案。基本上自动生成的代码不是您想要使用的,但是您可以使用它的零碎部分来快速构建自定义代码。例如为您构建表单域。这是 WordPress 的新代码,它与 Dreamweaver 代码的作用相同。最重要的是,WordPress API wpdb 会清理并执行您原本必须手动完成的许多其他工作。如果你问我,那就太酷了!

表单保持不变,因为我将使用 Dreamweaver 构建的内容,因为它既快速又简单。删除 Dreamweaver 创建的所有其他内容,并使用 WordPress 的 $wpdb 将其替换为以下代码。

<?php 
<!-- initialize wpdb -->
global $wpdb;
<!-- grab wordpress information -->
get_currentuserinfo();
$AccountID = $current_user->ID 
?>

<!-- THIS IS MY CUSTOME WP DB API CODE -->
<?php
$tableName = 'TableName';
$editFormAction = $wpdb->insert (
                        $tableName,
                        Array(
                            'wp_userid' => $AccountID, 
                            'public_char' => $_POST['public_char'], 
                            'char_rank' => $_POST['char_rank']
                            )
                    ); 
<!-- comment this out when not debugging -->
$wpdb->show_errors(); 
?>

这里的所有都是它的。我使用了 Dreamweaver 创建的 editFormAction 变量,然后重写了它之后的所有代码以使用 wpdb 方法。此表单仍需要验证、收紧表单字段等,但这就是我从 Dreamweaver 生成的 PHP SQL 插入转换为 WordPress SQL 插入的方式。它是漂亮、干净和简洁的代码,可以为您完成所有的清洁和消毒工作。我以为这会更复杂。我爱WordPress!

于 2013-09-14T22:34:29.010 回答