订单表格的 PHP 脚本放在哪里?
Where to put PHP script for order form?
大家好,我使用 Dreamweaver,并且一直在学习本教程。 http://www.htmlgoodies.com/beyond/webmaster/projects/article.php/3530691
关于放置 PHP 脚本的详细信息不是很具体,所以我很困惑是应该将它放在同一页面还是不同页面。我有包含订单的 orfm.php 和 calculation.That 的 javascript 工作正常。单击提交按钮后,该操作将导致 submitted.html。但是在同一页面上添加 PHP 脚本后,orfm.php,用于在 mysql 中插入数据,它无法正常工作。我应该把它放在一个单独的页面上还是我放在同一页面上的位置有误?
这是脚本:
<?
//uncomment for debugging
//print_r($_POST);
//most sites have magic quotes on
//but if they do not, this code simulates magic quotes
if( !get_magic_quotes_gpc() )
{
if( is_array($_POST) )
$_POST = array_map('addslashes', $_POST);
}
//make sure there is data in the name and email fields
if( empty($_POST["Name"]) )
{
$error["name"] = "Name is required.";
$Name = "";
}
else
$Name = $_POST["Name"];
if( empty($_POST["Email"]) )
{
$error["email"] = "Email is required.";
$Email = "";
}
else
$Email = $_POST["Email"];
if( empty($_POST["OtherInfo"]) )
{
$OtherInfo = "";
}
else
$OtherInfo = $_POST["OtherInfo"];
//check to make sure the qty fields are whole numbers
//but only check if there was data entered
if( !empty($_POST["qtyA"]) )
{
if( is_numeric($_POST["qtyA"]) && ( intval($_POST["qtyA"]) == floatval($_POST["qtyA"]) ) )
{
//we have a whole number
}
else
$error["qtyA"] = "Please enter a whole number for Class A Widgets.";
}
if( !empty($_POST["qtyB"]) )
{
if( is_numeric($_POST["qtyB"]) && ( intval($_POST["qtyB"]) == floatval($_POST["qtyB"]) ) )
{
//we have a whole number
}
else
$error["qtyB"] = "Please enter a whole number for Class B Widgets.";
}
if( !empty($_POST["qtyC"]) )
{
if( is_numeric($_POST["qtyC"]) && ( intval($_POST["qtyC"]) == floatval($_POST["qtyC"]) ) )
{
//we have a whole number
}
else
$error["qtyC"] = "Please enter a whole number for Class C Widgets.";
}
//we should have at least 1 item ordered in the form
if( empty($_POST["qtyA"]) && empty($_POST["qtyB"]) && empty($_POST["qtyC"]) )
$error["no_qty"] = "Please enter at least 1 item to order.";
if( is_array($error) )
{
echo "An error occurred while processing your order.";
echo "<br>\n";
echo "Please check the following error messages carefully, then click back in your browser.";
echo "<br>\n";
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
//stop everything as we have errors and should not continue
exit();
}
//we do not need the rest of the form fields as we can just calculate them from the whole numbers
if( !empty($_POST["qtyA"]) )
{
$qtyA = $_POST["qtyA"];
$totalA = $qtyA * 1.25;
}
else
{
$qtyA = 0;
$totalA = 0;
}
if( !empty($_POST["qtyB"]) )
{
$qtyB = $_POST["qtyB"];
$totalB = $qtyB * 2.35;
}
else
{
$qtyB = 0;
$totalB = 0;
}
if( !empty($_POST["qtyC"]) )
{
$qtyC = $_POST["qtyC"];
$totalC = $qtyC * 3.45;
}
else
{
$qtyC = 0;
$totalC = 0;
}
$GrandTotal = $totalA + $totalB + $totalC;
//we can store the order in a database as well
$link = @mysql_connect('localhost', 'root', 'password');
if (!$link)
{
echo "Could not connect: " . mysql_error();
}
else
{
mysql_select_db('admin');
$query = "INSERT INTO order_queue
( Name , Email , OtherInfo , qtyA ,
totalA , qtyB , totalB , qtyC , totalC , GrandTotal )";
$query .= " VALUES
('$Name', '$Email', '$OtherInfo', '$qtyA',
'$totalA', '$qtyB', '$totalB', '$qtyC', '$totalC', '$GrandTotal')";
//echo $query . "<br>\n";
$result = mysql_query($query);
mysql_free_result($result);
mysql_close($link);
}
?>
订单
<form method="POST" action="submitted.php" onsubmit="return Validate(this)" name="ofrm">
<p>Please tell us who you are (<font color="#FF0000">red</font> denotes required information):</p>
<table border="0" cellpadding="0" width="550" id="table1">
<tr>
<td width="340" align="right"><font color="#FF0000">Name</font></td>
<td width="10"> </td>
<td width="200"><input type="text" name="Name" size="30" tabindex="1"></td>
</tr>
<tr>
<td width="340" align="right"><font color="#FF0000">Email</font>
(Your confirmation will be sent here): </td>
<td width="10"> </td>
<td width="200"><input type="text" name="Email" size="30" tabindex="1"></td>
</tr>
<tr>
.......//more here
<td>
<input type="submit" value="Submit" name="subButton" tabindex="50">
<input type="reset" value="Reset" name="resetButton" tabindex="50">
</td>
</tr>
</table>
</form>
我只是想让这项工作插入到数据库中。我应该把它和表单放在同一个页面上,action=" ",还是放在哪里?
编辑 表单操作现在是 submitted.php 我把 php 脚本放在那里。它给我这个错误:
\n"; echo "Please check the following error messages carefully, then click back in your browser."; echo "
\n"; while(list($key, $val) = each($error)) { echo $val; echo "
\n"; } //stop everything as we have errors and should not continue exit(); } //we do not need the rest of the form fields as we can just calculate them from the whole numbers if( !empty($_POST["qtyA"]) ) { $qtyA = $_POST["qtyA"]; $totalA = $qtyA * 1.25; } else { $qtyA = 0; $totalA = 0; } if( !empty($_POST["qtyB"]) ) { $qtyB = $_POST["qtyB"]; $totalB = $qtyB * 2.35; } else { $qtyB = 0; $totalB = 0; } if( !empty($_POST["qtyC"]) ) { $qtyC = $_POST["qtyC"]; $totalC = $qtyC * 3.45; } else { $qtyC = 0; $totalC = 0; } $GrandTotal = $totalA + $totalB + $totalC; //we can store the order in a database as well $link = @mysql_connect('localhost', 'root', 'password'); if (!$link) { echo "Could not connect: " . mysql_error(); } else { mysql_select_db('admin'); $query = "INSERT INTO order_queue ( Name , Email , OtherInfo , qtyA , totalA , qtyB , totalB , qtyC , totalC , GrandTotal )"; $query .= " VALUES ('$Name', '$Email', '$OtherInfo', '$qtyA', '$totalA', '$qtyB', '$totalB', '$qtyC', '$totalC', '$GrandTotal')"; //echo $query . "
\n"; $result = mysql_query($query); mysql_free_result($result); mysql_close($link); } ?>
不看markup/html的一面很难做出判断,但这里有几点提示:
- 您可以使用标签上的 action 属性中的
htmlentities($_SERVER['PHP_SELF']);
将页面提交给自己或提交给另一个页面 action="submitted.php"
- 确保输入区域
name=
属性名称大小写匹配。如果在 post 之后的脚本中有 $_POST["test"]
,那么 <input name="test"
应该是相同的,而不是 <input name="Test"
- 如果您无法使用 IDE 进行调试,则在执行脚本时按顺序添加
echo "1"; echo "2"; echo "3";
以查看编号调试停止的位置。那将是开始故障排除的最佳位置。
echo "1";
if( empty($_POST["Name"]) )
{
echo "2";
$error["name"] = "Name is required.";
$Name = "";
}
else
{
echo "3";
$Name = $_POST["Name"];
echo "4";
}
不是最好的例子,但你明白了。我通常使用我尝试理解的大型脚本来执行此操作。
- 在执行
INSERT
之前将其作为文本回显以查看是否所有变量都包含数据。您还应该使用 mysql_query($sql) OR die(mysql_error());
输出数据库错误消息。请尝试使用 PDO
或 mysqli_query
。
- 确保 Javascript 验证不会阻止提交。删除验证并测试脚本以查看它是否提交。如果是,则问题出在客户端代码上。
希望对您有所帮助。
不是你想要的确切答案,但有几件事PHP初学者应该知道
- 您的代码包含一些已弃用的组件magic_quotes addslashes mysql*
- 您的代码缺少 input validation
- 您的代码缺少 separation of concerns。 HTML、CSS、业务逻辑、表示逻辑等所有内容都在一个文件中
- 开始使用好的IDE喜欢Komodo PDT
- 使用像 XDebug 这样好的调试工具
- 最后,开始使用像 Zend、CakePHP CodeIgniter Symphony 或数以千计的框架中的任何一个这样的好框架。几乎所有框架都在其入门指南中包含了表单到数据库
大家好,我使用 Dreamweaver,并且一直在学习本教程。 http://www.htmlgoodies.com/beyond/webmaster/projects/article.php/3530691
关于放置 PHP 脚本的详细信息不是很具体,所以我很困惑是应该将它放在同一页面还是不同页面。我有包含订单的 orfm.php 和 calculation.That 的 javascript 工作正常。单击提交按钮后,该操作将导致 submitted.html。但是在同一页面上添加 PHP 脚本后,orfm.php,用于在 mysql 中插入数据,它无法正常工作。我应该把它放在一个单独的页面上还是我放在同一页面上的位置有误?
这是脚本:
<?
//uncomment for debugging
//print_r($_POST);
//most sites have magic quotes on
//but if they do not, this code simulates magic quotes
if( !get_magic_quotes_gpc() )
{
if( is_array($_POST) )
$_POST = array_map('addslashes', $_POST);
}
//make sure there is data in the name and email fields
if( empty($_POST["Name"]) )
{
$error["name"] = "Name is required.";
$Name = "";
}
else
$Name = $_POST["Name"];
if( empty($_POST["Email"]) )
{
$error["email"] = "Email is required.";
$Email = "";
}
else
$Email = $_POST["Email"];
if( empty($_POST["OtherInfo"]) )
{
$OtherInfo = "";
}
else
$OtherInfo = $_POST["OtherInfo"];
//check to make sure the qty fields are whole numbers
//but only check if there was data entered
if( !empty($_POST["qtyA"]) )
{
if( is_numeric($_POST["qtyA"]) && ( intval($_POST["qtyA"]) == floatval($_POST["qtyA"]) ) )
{
//we have a whole number
}
else
$error["qtyA"] = "Please enter a whole number for Class A Widgets.";
}
if( !empty($_POST["qtyB"]) )
{
if( is_numeric($_POST["qtyB"]) && ( intval($_POST["qtyB"]) == floatval($_POST["qtyB"]) ) )
{
//we have a whole number
}
else
$error["qtyB"] = "Please enter a whole number for Class B Widgets.";
}
if( !empty($_POST["qtyC"]) )
{
if( is_numeric($_POST["qtyC"]) && ( intval($_POST["qtyC"]) == floatval($_POST["qtyC"]) ) )
{
//we have a whole number
}
else
$error["qtyC"] = "Please enter a whole number for Class C Widgets.";
}
//we should have at least 1 item ordered in the form
if( empty($_POST["qtyA"]) && empty($_POST["qtyB"]) && empty($_POST["qtyC"]) )
$error["no_qty"] = "Please enter at least 1 item to order.";
if( is_array($error) )
{
echo "An error occurred while processing your order.";
echo "<br>\n";
echo "Please check the following error messages carefully, then click back in your browser.";
echo "<br>\n";
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
//stop everything as we have errors and should not continue
exit();
}
//we do not need the rest of the form fields as we can just calculate them from the whole numbers
if( !empty($_POST["qtyA"]) )
{
$qtyA = $_POST["qtyA"];
$totalA = $qtyA * 1.25;
}
else
{
$qtyA = 0;
$totalA = 0;
}
if( !empty($_POST["qtyB"]) )
{
$qtyB = $_POST["qtyB"];
$totalB = $qtyB * 2.35;
}
else
{
$qtyB = 0;
$totalB = 0;
}
if( !empty($_POST["qtyC"]) )
{
$qtyC = $_POST["qtyC"];
$totalC = $qtyC * 3.45;
}
else
{
$qtyC = 0;
$totalC = 0;
}
$GrandTotal = $totalA + $totalB + $totalC;
//we can store the order in a database as well
$link = @mysql_connect('localhost', 'root', 'password');
if (!$link)
{
echo "Could not connect: " . mysql_error();
}
else
{
mysql_select_db('admin');
$query = "INSERT INTO order_queue
( Name , Email , OtherInfo , qtyA ,
totalA , qtyB , totalB , qtyC , totalC , GrandTotal )";
$query .= " VALUES
('$Name', '$Email', '$OtherInfo', '$qtyA',
'$totalA', '$qtyB', '$totalB', '$qtyC', '$totalC', '$GrandTotal')";
//echo $query . "<br>\n";
$result = mysql_query($query);
mysql_free_result($result);
mysql_close($link);
}
?>
订单
<form method="POST" action="submitted.php" onsubmit="return Validate(this)" name="ofrm">
<p>Please tell us who you are (<font color="#FF0000">red</font> denotes required information):</p>
<table border="0" cellpadding="0" width="550" id="table1">
<tr>
<td width="340" align="right"><font color="#FF0000">Name</font></td>
<td width="10"> </td>
<td width="200"><input type="text" name="Name" size="30" tabindex="1"></td>
</tr>
<tr>
<td width="340" align="right"><font color="#FF0000">Email</font>
(Your confirmation will be sent here): </td>
<td width="10"> </td>
<td width="200"><input type="text" name="Email" size="30" tabindex="1"></td>
</tr>
<tr>
.......//more here
<td>
<input type="submit" value="Submit" name="subButton" tabindex="50">
<input type="reset" value="Reset" name="resetButton" tabindex="50">
</td>
</tr>
</table>
</form>
我只是想让这项工作插入到数据库中。我应该把它和表单放在同一个页面上,action=" ",还是放在哪里?
编辑 表单操作现在是 submitted.php 我把 php 脚本放在那里。它给我这个错误:
\n"; echo "Please check the following error messages carefully, then click back in your browser."; echo "
\n"; while(list($key, $val) = each($error)) { echo $val; echo "
\n"; } //stop everything as we have errors and should not continue exit(); } //we do not need the rest of the form fields as we can just calculate them from the whole numbers if( !empty($_POST["qtyA"]) ) { $qtyA = $_POST["qtyA"]; $totalA = $qtyA * 1.25; } else { $qtyA = 0; $totalA = 0; } if( !empty($_POST["qtyB"]) ) { $qtyB = $_POST["qtyB"]; $totalB = $qtyB * 2.35; } else { $qtyB = 0; $totalB = 0; } if( !empty($_POST["qtyC"]) ) { $qtyC = $_POST["qtyC"]; $totalC = $qtyC * 3.45; } else { $qtyC = 0; $totalC = 0; } $GrandTotal = $totalA + $totalB + $totalC; //we can store the order in a database as well $link = @mysql_connect('localhost', 'root', 'password'); if (!$link) { echo "Could not connect: " . mysql_error(); } else { mysql_select_db('admin'); $query = "INSERT INTO order_queue ( Name , Email , OtherInfo , qtyA , totalA , qtyB , totalB , qtyC , totalC , GrandTotal )"; $query .= " VALUES ('$Name', '$Email', '$OtherInfo', '$qtyA', '$totalA', '$qtyB', '$totalB', '$qtyC', '$totalC', '$GrandTotal')"; //echo $query . "
\n"; $result = mysql_query($query); mysql_free_result($result); mysql_close($link); } ?>
不看markup/html的一面很难做出判断,但这里有几点提示:
- 您可以使用标签上的 action 属性中的
htmlentities($_SERVER['PHP_SELF']);
将页面提交给自己或提交给另一个页面action="submitted.php"
- 确保输入区域
name=
属性名称大小写匹配。如果在 post 之后的脚本中有$_POST["test"]
,那么<input name="test"
应该是相同的,而不是<input name="Test"
- 如果您无法使用 IDE 进行调试,则在执行脚本时按顺序添加
echo "1"; echo "2"; echo "3";
以查看编号调试停止的位置。那将是开始故障排除的最佳位置。
echo "1"; if( empty($_POST["Name"]) ) { echo "2"; $error["name"] = "Name is required."; $Name = ""; } else { echo "3"; $Name = $_POST["Name"]; echo "4"; }
不是最好的例子,但你明白了。我通常使用我尝试理解的大型脚本来执行此操作。
- 在执行
INSERT
之前将其作为文本回显以查看是否所有变量都包含数据。您还应该使用mysql_query($sql) OR die(mysql_error());
输出数据库错误消息。请尝试使用PDO
或mysqli_query
。 - 确保 Javascript 验证不会阻止提交。删除验证并测试脚本以查看它是否提交。如果是,则问题出在客户端代码上。
希望对您有所帮助。
不是你想要的确切答案,但有几件事PHP初学者应该知道
- 您的代码包含一些已弃用的组件magic_quotes addslashes mysql*
- 您的代码缺少 input validation
- 您的代码缺少 separation of concerns。 HTML、CSS、业务逻辑、表示逻辑等所有内容都在一个文件中
- 开始使用好的IDE喜欢Komodo PDT
- 使用像 XDebug 这样好的调试工具
- 最后,开始使用像 Zend、CakePHP CodeIgniter Symphony 或数以千计的框架中的任何一个这样的好框架。几乎所有框架都在其入门指南中包含了表单到数据库