【问题标题】:Where to put PHP script for order form?在哪里放置订单表单的 PHP 脚本?
【发布时间】: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">&nbsp;</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">&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp; 
<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


【解决方案1】:

不看markup/html那一面是很难做出判断的,但这里有几点建议:

  • 您可以使用标签上的操作属性中的htmlentities($_SERVER['PHP_SELF']); 将页面提交给它自己,或者提交给另一个页面action="submitted.php"
  • 确保输入区域name= 属性名称大小写匹配。如果您在帖子之后的脚本中有$_POST["test"],那么&lt;input name="test" 应该是相同的,而不是&lt;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 验证不会阻止提交。删除验证并测试脚本以查看它是否提交。如果是,则问题出在客户端代码上。

我希望这会有所帮助。

【讨论】:

  • 尽管我想在这里发布所有内容,但它不允许发布太多代码。 :( 我会读你的这个指针谢谢。
【解决方案2】:

不是你想要的确切答案,而是 PHP 初学者应该知道的一些事情

  • 您的代码包含一些已弃用的组件 magic_quotes addslashes mysql*
  • 您的代码缺少input validation
  • 您的代码缺少separation of concerns。 HTML、CSS、业务逻辑、表示逻辑等所有内容都在一个文件中
  • 开始使用像 Komodo PDT 这样的好的 IDE
  • 使用良好的调试工具,例如 XDebug
  • 最后,开始使用一个好的框架,如 Zend、CakePHP CodeIgniter Symphony 或成千上万的框架中的任何一个。几乎所有框架都在其入门指南中包含表单到数据库

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 2017-12-20
    • 2012-07-30
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2015-08-05
    相关资源
    最近更新 更多