在哪里为订单表单添加PHP脚本?

时间:2015-01-15 05:55:23

标签: php mysql database insert dreamweaver

大家好我使用Dreamweaver并且我一直在关注本教程。 http://www.htmlgoodies.com/beyond/webmaster/projects/article.php/3530691

关于PHP脚本放置位置的详细信息并不是非常具体,所以如果我将它放在同一页面或不同的页面中,我会感到困惑。我有orfm.php,其中包括订单和javascript的计算。这很好。单击提交按钮后,该操作将导致submit.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 =&#34; &#34; ,或者在哪里?

编辑表单操作现在是 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); } ?>

2 个答案:

答案 0 :(得分:1)

如果没有看到标记/ html方面很难做出判断,但这里有一些指示:

  • 您可以使用标记上的操作属性中的htmlentities($_SERVER['PHP_SELF']);或其他页面action="submitted.php"
  • 将页面提交给自己
  • 确保输入区域name=属性名称匹配大小写。如果帖子后的脚本中有$_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());输出数据库错误消息。请尝试使用PDOmysqli_query代替。
  • 确保Javascript验证不会阻止提交。删除验证并测试脚本以查看它是否提交。如果确实如此,则问题出在客户端代码上。

我希望这会有所帮助。

答案 1 :(得分:0)

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

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