【发布时间】:2015-01-15 05:55:23
【问题描述】:
大家好,我使用的是 Dreamweaver,并且一直在学习本教程。 http://www.htmlgoodies.com/beyond/webmaster/projects/article.php/3530691
关于将 PHP 脚本放置在何处的详细信息不是很具体,所以我很困惑是否应该将它放在同一页面或不同页面中。我有 orfm.php,其中包括用于计算的订单和 javascript。效果很好。单击提交按钮后,该操作将导致提交的.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="",还是放在哪里?
EDIT 表单动作现在是 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); } ?>
【问题讨论】:
-
点击提交按钮后,该操作导致提交的.html。您的表单不应提交到 html 文档。它应该提交给处理
POST'ed 数据的 php 脚本 -
@MichaelColeman 那么我应该将php脚本粘贴到假设提交的.php上吗?
-
@MichaelColeman 现在正在提交的.php 我把 php 代码放在那里,它给了我很长的错误。我会编辑我的帖子
-
您能提供包含您的表单的 HTML 吗?谢谢
-
你给出的 php 脚本的文件名是什么?
标签: php mysql database insert dreamweaver