我们有一个用 WordPress 构建的漫画书店网站,店主每周都会去一个私人页面,用一个表格提交所有新版本。此表单将所有项目发送到数据库中名为 shiplist 的表中,然后该表呈现在网站公共页面上的表单上,客户可以在该页面上选择项目并提交他们想要的内容。我们最近搬到了一个新主机,现在表格不起作用,面向客户的页面也不起作用。以前没有任何东西被添加到表中。
流程如下: 第 1 页:/pull-list-form/ - 带有表单的页面,所有者添加内容并点击提交
页面上的表格:
<form name="form1" method="post" action="/pull-list-form-created/">
<p align="center"><input name="category" id="category-Shipping List" type="radio" value="shipList" checked>Shipping List <input name="category" id="category-Bagged Sets" type="radio" value="baggedSets">Bagged Sets <input name="category" id="category-Trade Paperbacks" type="radio" value="tradePaperbacks">Trade Paperbacks <input name="category" id="category-Toys" type="radio" value="toys">Toys</p>
<p align="center"><textarea name="uploadList" cols="90" rows="25" id="uploadList"></textarea></p>
<p align="center"><input type="button" onClick="confirmList()" name="Submit" value=" Submit List "> </p>
</form>
第 2 页:/pull-list-form-created/ - 提交后,它会转到此页面,其中包含用于解析、定义表单信息并将其排序到 SQL 表中的 PHP 文件,其中还包含 PHP连接到数据库的文件。
本页的PHP:
<?php include 'classes/class.shipList.php'; ?>
<?php
$sl = new shiplist;
$tab = chr(9);
$crlf = chr(13).chr(10);
$ulist = $_POST['uploadList'];
$prodType = trim($_POST['category']);
//echo "POST['category'] = $prodType<br>";
$items = explode($crlf,$ulist);
//echo (is_object($sl)) ? "sl IS an object<P>" : "sl IS NOT an object<P>";
$iCt = 1;
if (isset($_POST['category'])) {echo "The selected stuff was $prodType<P>"; }
if ($prodType=="shipList") {
$sl->deleteAll("shiplist");
foreach ($items as $item) {
$item = trim($item);
//echo "$iCt: $item<br>";
$iCt++;
$pieces = array();
$thing = $price = "";
if (substr_count($item,$tab) && (substr_count($item,"$") || substr_count($item,$tab."PI") || substr_count($item,$tab."FREE"))) {
$pieces = explode($tab,$item);
$thing = trim($pieces[0]);
$price = trim($pieces[1]);
$whichList = (isset($pieces[2])) ? trim($pieces[2]) : "A";
} else {
$thing = trim($item);
$price = "NA";
$whichList = "A";
}
if ($thing != "") $sl->insert($thing,$price,$whichList);
}
} elseif ($prodType=="baggedSets") {
$sl->deleteAll($prodType);
$tab = chr(9);
foreach ($items as $item) {
$item = trim($item);
$pieces = array();
$title = $issues = $numberIssues = $guidePrice = $price = "";
/*if ((substr_count($item,$tab) > 1) || (substr_count($item,$tab."PI") || substr_count($item,$tab."FREE"))) {
$pieces = explode($tab,$item);
$title = trim($pieces[0]);
$issues = trim($pieces[1]);
$numberIssues = trim($pieces[2]);
$guidePrice = trim($pieces[3]);
$price = trim($pieces[4]);
} elseif (substr($item,-2,2)=="NA") {
$pieces = explode($tab,$item);
$title = trim($pieces[0]);
$issues = $numberIssues = $guidePrice = $price = "";
} else {
$title = trim($item);
$issues = $numberIssues = $guidePrice = $price = "";
}*/
//if ($title != "") $sl->insertBaggedSets($title,$issues,$numberIssues,$guidePrice,$price);
$pieces = explode($tab,$item);
$title = trim($pieces[0]);
$price = trim($pieces[1]);
if ($title != "") $sl->insertBaggedSets($title,$price);
}
} elseif ($prodType=="tradePaperbacks") {
$sl->deleteAll($prodType);
foreach ($items as $item) {
$item = trim($item);
$pieces = array();
$thing = $coverPrice = $price = "";
$catalogID = "A";
if ((substr_count($item,$tab) > 1) || (substr_count($item,$tab."PI") || substr_count($item,$tab."FREE"))) {
$pieces = explode($tab,$item);
$thing = trim($pieces[0]);
$coverPrice = trim($pieces[1]);
$price = trim($pieces[2]);
$catalogID = trim($pieces[3]);
} else {
$thing = trim($item);
$coverPrice = $price = $catalogID = "";
}
//echo "Found ".count($pieces).": $thing $coverPrice $price $catalogID<br>";
if ($thing != "") $sl->insertTrades($thing,$coverPrice,$price,$catalogID);
}
} elseif ($prodType=="toys") {
$sl->deleteAll($prodType);
foreach ($items as $item) {
$item = trim($item);
$pieces = array();
$thing = $price = "";
if (substr_count($item,$tab)) { //&& (substr_count($item,$tab."NA") || substr_count($item,$tab."PI") || substr_count($item,$tab."FREE"))) {
$pieces = explode($tab,$item);
$thing = trim($pieces[0]);
$price = trim($pieces[1]);
} else {
$thing = trim($item);
$price = "";
}
if ($thing != "") $sl->insertToys($thing,$price);
}
}
/**/
if ($prodType=="tradePaperbacks") {
$pt = "t";
} elseif ($prodType=="baggedSets") {
$pt = "b";
} elseif ($prodType=="toys") {
$pt = "y";
} else {
$pt = "s";
}
?>
This is the list that was uploaded. See the <a href="/reserve-your-comics/?pt=<?=$pt?>">updated form</a> here.<br />
<?php
//$sl->getAll($prodType);
?>
包含文件class.shipList.php:
<?php
include_once("./class.database.php");
// **********************
// CLASS DECLARATION
// **********************
class shipList
{ // class : begin
// **********************
// ATTRIBUTE DECLARATION
// **********************
var $lid; // KEY ATTR. WITH AUTOINCREMENT
var $item; // (normal Attribute)
var $price; // (normal Attribute)
var $database; // Instance of class database
// **********************
// CONSTRUCTOR METHOD
// **********************
function shipList()
{
$this->database = new Database();
}
// **********************
// GETTER METHODS
// **********************
function getlid()
{
return $this->lid;
}
function getitem()
{
return $this->item;
}
function getprice()
{
return $this->price;
}
// **********************
// SETTER METHODS
// **********************
function setlid($val)
{
$this->lid = $val;
}
function setitem($val)
{
$this->item = $val;
}
function setprice($val)
{
$this->price = $val;
}
// **********************
// SELECT METHOD / LOAD
// **********************
function select($id,$table)
{
if ($id > 0) {
$sql = "SELECT * FROM $table WHERE lid = $id;";
$result = $this->database->query($sql);
$result = $this->database->result;
$row = mysql_fetch_object($result);
$this->lid = $row->lid;
$this->item = $row->item;
if ($row->title != "") $this->item = $row->title;
$this->price = $row->price;
}
}
function getAll($table="shipList")
{
$sql = "SELECT * FROM $table ORDER BY lid ASC";
$result = $this->database->query($sql);
$result = $this->database->result;
echo "<table border=0 cellpadding=2 cellspacing=2 align=center>";
while ($row = mysql_fetch_object($result)) {
$this->lid = $row->lid;
$this->item = $row->item;
$this->price = $row->price;
echo "<tr>";
if (isset($row->item)) echo "<td>".stripslashes($row->item)."</td>";
if (isset($row->title)) echo "<td>".$row->title."</td>";
if (isset($row->issues) || $row->issues > 0) echo "<td>".$row->issues."</td>";
if (isset($row->numberIssues) && $row->numberIssues > 0) echo "<td>".$row->numberIssues."</td>";
if (isset($row->guidePrice)) echo "<td><strike>".$row->guidePrice."</strike></td>";
if (isset($row->coverPrice)) echo "<td><strike>".$row->coverPrice."</strike></td>";
if ($this->price == "NA" || $this->price=="") {
continue;
//echo "<tr><td colspan=3><P><B>".$fl.$this->item."</B></P></td></tr>\r\n";
//if ($fl == "") $fl="<BR />";
} else {
$pname = "p_".$this->lid;
$cname = "c_".$this->lid;
}
echo "<td>".$this->price."</td><td width=60><input type=checkbox name='$cname' id='$cname' value='1' onClick=chkboxQty('$cname')><input type=text name='$pname' id='$pname' size='2' maxlength='2' value='0'></td></tr>\n";
echo "</tr>";
}
echo "</table>";
}
function getAllForm($which="A")
{
$fl = "";
$cond = "WHERE ";
$cond .= ($which=="A") ? "1" : "whichList = '".$which."'";
$sql = "SELECT * FROM shipList $cond ORDER BY lid ASC";
//echo $sql;
$result = $this->database->query($sql);
$result = $this->database->result;
echo "<table border=0 cellpadding=2 cellspacing=2 align=center>";
echo "<tr><td><font size=+1>Item</font></td><td><font size=+1>Price</font></td><td><font size=+1>Quantity</font></td></tr>\r\n";
while ($row = mysql_fetch_object($result)) {
$this->lid = $row->lid;
$this->item = $row->item;
$this->price = $row->price;
if ($this->price == "NA" || $this->price=="") {
echo "<tr><td colspan=3><P><B>".$fl.$this->item."</B></P></td></tr>\r\n";
if ($fl == "") $fl="<BR />";
} else {
$pname = "p_".$this->lid;
$cname = "c_".$this->lid;
echo "<tr><td>".$this->item."</td><td>".$this->price."</td><td><input type=checkbox name='$cname' id='$cname' value='1' onClick=chkboxQty('$cname')><input type=text name='$pname' id='$pname' size='2' maxlength='2' value='0'></td></tr>\r\n";
}
}
echo "<tr><td colspan=3>Comments or Questions:</td></tr>";
echo "<tr><td colspan=3 align=center><textarea name=\"comments\" cols=\"60\" rows=\"5\"></textarea></td></tr>";
echo "</table>";
}
// **********************
// DELETE
// **********************
function delete($id)
{
$sql = "DELETE FROM shipList WHERE lid = $id;";
//$result = $this->database->query($sql);
}
function deleteAll($table)
{
$sql = "DELETE FROM $table WHERE lid > 0;";
//echo "$sql<br/>";
$result = $this->database->query($sql);
}
// **********************
// INSERT
// **********************
function insert($item,$price,$which="A")
{
$this->lid = ""; // clear key for autoincrement
$item = addslashes($item);
$price = addslashes($price);
$which = addslashes($which);
$sql = "INSERT INTO shipList ( item,price,whichList ) VALUES ( '$item','$price','$which' )";
//echo "$sql<br/>";
$result = $this->database->query($sql);
$this->lid = mysql_insert_id($this->database->link);
}
function insertB($prodType,$title,$issues,$numberIssues,$guidePrice,$price)
{
$this->lid = ""; // clear key for autoincrement
$sql = "INSERT INTO $prodType ( title,issues,numberIssues,guidePrice,price ) VALUES ( '$title','$issues','$numberIssues','$guidePrice','$price' )";
echo "$sql<br/>";
$result = $this->database->query($sql);
$this->lid = mysql_insert_id($this->database->link);
}
function insertT($prodType,$title,$coverPrice,$price)
{
$this->lid = ""; // clear key for autoincrement
$sql = "INSERT INTO $prodType ( title,coverPrice,price ) VALUES ( '$title','$coverPrice','$price' )";
echo "$sql<br/>";
$result = $this->database->query($sql);
$this->lid = mysql_insert_id($this->database->link);
}
function insertY($prodType,$title,$price)
{
$this->lid = ""; // clear key for autoincrement
$sql = "INSERT INTO $prodType ( title,price ) VALUES ( '$title','$price' )";
echo "$sql<br/>";
$result = $this->database->query($sql);
$this->lid = mysql_insert_id($this->database->link);
}
// **********************
// UPDATE
// **********************
function update($id)
{
$sql = " UPDATE shipList SET item = '$this->item',price = '$this->price' WHERE lid = $id ";
$result = $this->database->query($sql);
}
} // class : end
?>
<!-- end of generated class -->
连接数据库的class.database.php文件。
<?php
class Database
{ // Class : begin
var $host; //Hostname, Server
var $password; //Passwort MySQL
var $user; //User MySQL
var $database; //Datenbankname MySQL
var $link;
var $query;
var $result;
var $rows;
function Database()
{ // Method : begin
// ********** ADJUST THESE VALUES HERE **********
$this->host = "localhost";
$this->password = "password";
$this->user = "username";
$this->database = "database name";
$this->rows = 0;
// **********************************************
// **********************************************
} // Method : end
function OpenLink()
{ // Method : begin
$this->link = @mysql_connect($this->host,$this->user,$this->password) or die (print "Class Database: Error while connecting to DB (link)");
} // Method : end
function SelectDB()
{ // Method : begin
@mysql_select_db($this->database,$this->link) or die (print "Class Database: Error while selecting DB");
} // Method : end
function CloseDB()
{ // Method : begin
mysql_close();
} // Method : end
function Query($query)
{ // Method : begin
$this->OpenLink();
$this->SelectDB();
$this->query = $query;
$this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query");
// $rows=mysql_affected_rows();
if(ereg("SELECT",$query))
{
$this->rows = mysql_num_rows($this->result);
}
$this->CloseDB();
} // Method : end
} // Class : end
?>
第 3 页:/reserve-your-comics/ - 此页面从数据库中提取并呈现项目列表以在页面上创建一个表单,供客户选择项目并提交包含他们的选择的电子邮件:
header.php 中的代码:
<script language="javascript">
function confirmList() {
var whichButton;
var len = document.forms.form1.category.length;
for (var i=0; i < len; i++) {
if (document.form1.category[i].checked) {
whichButton = document.form1.category[i].value
}
}
var whichList;
if (whichButton == "shipList") { whichList = "Shipping"; }
if (whichButton == "baggedSets") { whichList = "Bagged Sets"; }
if (whichButton == "tradePaperbacks") { whichList = "Trade Paperbacks"; }
if (whichButton == "toys") { whichList = "Toys"; }
r = confirm("You are submitting a "+whichList+" list");
if (r) { document.forms.form1.submit(); }
}
</script>
<script language="JavaScript" type="text/JavaScript">
function chkboxQty(chkBox) {
var txtBox = chkBox.replace("c_","p_");
if (document.getElementById(chkBox).checked == true) {
document.getElementById(txtBox).value = '1';
}
if (document.getElementById(chkBox).checked == false) {
document.getElementById(txtBox).value = '0';
}
}
</script>
代码来自/reserve-your-comics/:
<?php
$which = "shiplist";
if (isset($_GET['pt'])) {
$pt = trim($_GET['pt']);
switch ($pt) {
case "":
$which = "shiplist";
break;
case "s":
$which = "shiplist";
break;
case "t":
$which = "tradepaperbacks";
break;
case "b":
$which = "baggedsets";
break;
case "y":
$which = "toys";
break;
}
}
?>
<div class="page-header">
<h1><?php the_title(); ?></h1>
</div>
<div class="container" id="posts">
<?php /* Start the Loop. */ ?>
<?php if(have_posts()) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<article>
<script language="JavaScript" type="text/JavaScript">
function chkboxQty(chkBox) {
var txtBox = chkBox.replace("c_","p_");
if (document.getElementById(chkBox).checked == true) {
document.getElementById(txtBox).value = '1';
}
if (document.getElementById(chkBox).checked == false) {
document.getElementById(txtBox).value = '0';
}
}
function lookupItem(id) {
var myBooks = "";
}
function validate_required(field,alerttxt) {
with (field) {
if (value==null||value=="") {
alert(alerttxt);return false}
else {return true}
}
}
function validate(thisform) {
with (thisform) {
if (validate_required(Cname,"To help assist completing your order, your first name must be entered.")==false) {
Cname.focus();
return false;
} else if (validate_required(Lname,"To help assist completing your order, your last name must be entered.")==false) {
Lname.focus();
return false;
} else if (validate_required(email,"To help assist completing your order, an email address must be entered.")==false) {
email.focus();
return false;
} else {
return true;
}
}
}
</script>
<?php if ($which=="shiplist") { ?>
(text)
<?php } ?>
<?php if ($which=="tradepaperbacks") { ?>
(text)
<?php } ?>
<?php if ($which=="baggedsets") { ?>
(text)
<?php } ?>
<?php if ($which=="shiplist") { ?>
(text)
<?php } ?>
(text)
<form name="form1" method="post" action="/pulllistaction/" onSubmit="return validate(this);" class="reserve_form">
<input type="hidden" name="list" value="<?=$which?>">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="50%" style='text-align:right;color:#000;'>Your First and Last Name: </td>
<td width="50%"><input name="Cname" type="text" id="Cname" size="16" maxlength="32"> <input name="Lname" type="text" id="Lname" size="16" maxlength="32"></td>
</tr>
<tr>
<td style='text-align:right;color:#000;'>Your Email Address: </td>
<td><input name="email" type="text" id="email" maxlength="75"></td>
</tr>
<tr>
<td style='text-align:right;color:#000;'>Join our Mailing List? </td>
<td><input name="mailingList" type="radio" value="Yes"> Yes
<input name="mailingList" type="radio" value="No" checked> No</td>
</tr>
</table>
<p align="center">
<input name="subBtn" type="submit" id="subBtn" value=" Email Your List "> <input name="subBtn3" type="submit" id="subBtn3" value=" Create Printable List ">
<br>
<?php //include 'classes/class.myDatabase.php'; ?>
<?php //include 'classes/class.test.php'; ?>
<?php include 'classes/class.shipList.php'; ?>
<?php $sl = new shipList; ?>
<?php
$aProducts = array('s','b','t','y','');
$sProdType = trim($_GET['pt']);
if (isset($_GET['pt'])) {
//if (!in_array($sProdType,$aProducts)) { echo "<P>sProdType = :$sProdType:<P>"; }
}
if ($sProdType=='') $sProdType = "s";
//$sl->getAll($sProdType); //"shiplist"
$sl->getByType($sProdType); //"shiplist"
//echo "<P>sProdType = :$sProdType:<P>";
//$which?>
<?php //echo (is_object($this->db)) ? "this->db IS an object" : "this->db IS NOT an object"; ?>
</p>
<p align="center">
</form>
<div class="clear"></div>
</article>
</div>
<?php endwhile; ?>
</div>
我知道很多 PHP/HTML 都非常旧,可能不符合标准,但我在 PHP 和 SQL 方面的专业知识非常有限,所以我没有做太多改变。
数据库信息(主机、数据库名称、登录名、密码)都是从 wp-config.php 中提取的,并在我尝试一些示例 PHP 代码时确认已连接。我尝试创建一个新的数据库用户/密码,并将数据库 URL 替换为本地主机,但是当我尝试提交给它时没有任何反应。我还发现了一些 PHP 代码,可以手动将内容添加到有效的 shiplist 表中,但它不会呈现到面向客户的页面上。