我想知道如何将这个 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"> </td>
<td><input type="submit" value="Insert record" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form2" />
</form>
<p> </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 /删除东西。
非常感谢您的指导和帮助!