订单的PHP脚本放在哪里?
大家好,我使用dreamweaver,我一直在学习本教程 关于PHP脚本放在哪里的细节不是很具体,所以我不知道应该放在同一页还是另一页。我有orfm.php,其中包括用于计算的订单和javascript。这很好。单击submit按钮后,该操作将导致submited.html。但是在同一页面上添加了用于在mysql中插入数据的PHP脚本orfm.PHP之后,它就不能正常工作了。我应该把它放在另一页上,还是我在同一页上的位置有误 以下是脚本:订单的PHP脚本放在哪里?,php,mysql,database,insert,dreamweaver,Php,Mysql,Database,Insert,Dreamweaver,大家好,我使用dreamweaver,我一直在学习本教程 关于PHP脚本放在哪里的细节不是很具体,所以我不知道应该放在同一页还是另一页。我有orfm.php,其中包括用于计算的订单和javascript。这很好。单击submit按钮后,该操作将导致submited.html。但是在同一页面上添加了用于在mysql中插入数据的PHP脚本orfm.PHP之后,它就不能正常工作了。我应该把它放在另一页上,还是我在同一页上的位置有误 以下是脚本: <? //uncomment for debug
<?
//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);
}
?>
在没有看到标记/html方面的情况下很难做出判断,但这里有几个要点:
- 您可以使用
在标签的action属性中,或转到另一个页面htmlentities($\u SERVER['PHP\u SELF'])将页面提交给自己
action=“submitted.php”
- 确保输入区域
属性名称与大小写匹配。如果您在文章后面的脚本中有name=
,那么$\u POST[“test”]
并不是您想要的确切答案,但是PHP初学者应该知道一些事情
- 您的代码包含一些不推荐使用的组件
- 您的代码缺少
- 您的代码缺少。HTML、CSS、业务逻辑、表示逻辑等都在一个文件中
- 开始使用一个好的IDE,比如
- 使用一个好的调试工具,比如XDebug
- 最后,开始使用一个好的框架,比如Zend、CakePHP CodeIgniter Symphony或其他数千个框架中的任何一个。几乎所有的框架都在其入门指南中包含表单到数据库
'eddata@MichaelColeman那么,我应该将php脚本粘贴到submitted.php上吗?@MichaelColeman它现在使用submitted.php。我把php代码放在那里,它给了我很长的错误。我会编辑我的帖子,你能提供包含表单的HTML吗?谢谢,您提供的php脚本的文件名是什么?虽然我很想在这里发布所有内容,但它不允许发布太多代码:(我会读你的这本书,谢谢。POST
<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>
\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); } ?>
echo "1"; if( empty($_POST["Name"]) ) { echo "2"; $error["name"] = "Name is required."; $Name = ""; } else { echo "3"; $Name = $_POST["Name"]; echo "4"; }