传递由php变量组成的表单时遇到问题

时间:2013-07-16 19:22:35

标签: php database forms

所以我有两页我用来测试的多页表单。当我单独测试表单的第一部分时,它工作正常。所有内容都根据它从我的数据库中检索的信息进行显示。在我单击提交按钮到下一页之前,一切看起来都很棒,这确认了所有值。当我尝试点击提交时,它也应该传递用户的输入并将其保存为$ _POST [],它会给我一个服务器错误。我对php很新,所以我不太熟悉我可能需要做的每件事。

这是我的第一部分代码:

<?php
    $db = mysql_connect(
  ':/Applications/MAMP/tmp/mysql/mysql.sock',
  'root',
  'root'
);
        if(!$db) die("Error connecting to MySQL database.");
        mysql_select_db('onlineform', $db);

        $newQuery = mysql_query("SELECT newPrice,numberOfWeeks FROM onlineformdata ORDER BY id DESC LIMIT 1");
$newRow = mysql_fetch_row($newQuery);



        $newQuery1 = mysql_query("SELECT newCampSessions FROM onlineformdata ORDER BY id DESC LIMIT 1") or die('Error ' . mysql_error());
$newFoo = mysql_fetch_array($newQuery1);


        $newQuery2 = mysql_query("SELECT pricePerWeek FROM onlineformdata ORDER BY id DESC LIMIT 1") or die('Error ' . mysql_error());
$newFoo1 = mysql_fetch_array($newQuery2);

        $newQuery3 = mysql_query("SELECT numberOfSchools FROM onlineformdata ORDER BY id DESC LIMIT 1");
$newRow1 = mysql_fetch_row($newQuery3);

        $newQuery4 = mysql_query("SELECT schools FROM onlineformdata ORDER BY id DESC LIMIT 1") or die('Error ' . mysql_error());
$newFoo2 = mysql_fetch_array($newQuery4);

$newOldString = $newFoo['newCampSessions'];
$newOldString2 = $newFoo1['pricePerWeek'];
$newOldString3 = $newFoo2['schools'];



$newChangedString = unserialize($newOldString);
$newChangedString2 = unserialize($newOldString2);
$newChangedString3 = unserialize($newOldString3);

$_SESSION['quickVariable'] = $newChangedString2;

settype($newRow[1], "integer");

$limit = $newRow[1];
$anotherLimit = $newRow1[0];
mysql_close();


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<link href='http://fonts.googleapis.com/css?family=Alef' rel='stylesheet' type='text/css'>
<link type="text/css" rel="stylesheet" href="style3.css"/>
<script type="text/javascript" src="JavaScript.js">
</script>
 <script type="text/javascript">var jslang='EN';
 </script>
        <link href="http://activemindandbody.orgStyleSheets/ModuleStyleSheets.css" rel="StyleSheet" type="text/css" />
        <script type="text/javascript">var jslang='EN';
        </script>
        <link href="http://activemindandbody.org/style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/modernizr.custom.04022.js"></script>
        <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:700,300,300italic' rel='stylesheet' type='text/css'>
        <link href='http://fonts.googleapis.com/css?family=Alef' rel='stylesheet' type='text/css'>
    </head>
    <body>
        <!-- BC_OBNW -->
        <div id="container">
            <div id="header"><div id="header_text" style="top: 118px; left: 23px;">
                <p>Make a donation today and help&nbsp;&nbsp;âCreating a Brighter Future                      
    Together.â&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <a href="http://activemindandbody.org/donation.html">Make a donation now !</a> </p>
        </div>
        </div>
        <div id="content">
                    <section class="tabs">
                <input id="tab-1" type="radio" name="radio-set" class="tab-selector-1" checked="checked" />
                <label for="tab-1" class="tab-label-1">1. Camper's Info</label>

                <input id="tab-2" type="radio" name="radio-set" class="tab-selector-2" />
                <label for="tab-2" class="tab-label-2">2. Parent's Info</label>

                <input id="tab-3" type="radio" name="radio-set" class="tab-selector-3" />
                <label for="tab-3" class="tab-label-3">3. Emergency<br/> Contacts</label>

                <input id="tab-4" type="radio" name="radio-set" class="tab-selector-4" />
                <label for="tab-4" class="tab-label-4">4. Camper's Ride Home</label>

                <input id="tab-5" type="radio" name="radio-set" class="tab-selector-5" />
                <label for="tab-5" class="tab-label-5">5. <br/> Camp Days & Prices</label>

                 <input id="tab-6" type="radio" name="radio-set" class="tab-selector-6" />
                <label for="tab-6" class="tab-label-6">6. Confirm your details</label>

                <input id="tab-7" type="radio" name="radio-set" class="tab-selector-7" />
                <label for="tab-7" class="tab-label-7">7. <br />Paypal</label>
                    </section>

                <div class="clear-shadow"></div>

                <div class="content indent">

<form id="paymentform" action="amd8.php" method="post">
<br/>
<br />
   <label><b>Camp Sessions: 1-6 (Check off the week(s) the camper will be participating in.)</b> </label><br /><br />
   <ol>
   <?php  
for ($count = 0; $count < $limit; $count=$count+1)
        {
            echo"<li>"; echo $newChangedString[$count]; echo '($'; echo $newChangedString2[$count]; echo '/child)<input type=checkbox name="campsessions[]" value="'; echo $newChangedString[$count]; echo '"></li>'; 
        }

     ?>
   </ol>
    <br/>
    <label><b>Select camp location:</b> </label><br /><br />


    <select name="schoolChoice">
 <?php  

for ($count1 = 0; $count1 < $anotherLimit; $count1=$count1+1)
        {

            echo"<option value="; echo $newChangedString3[$count1]; echo '">'; echo $newChangedString3[$count1]; echo "</option>";        
        }

?> 
    </select>
<br/><br/>



 <input type="hidden" name="totalprice" />  
<input type="submit" name="formSubmit" value="Submit" class="button greenButton"/>
<input type="button" name="cancel" value="Cancel" class="button redButton" onclick="href='activemindandbody.org'">

</form>



                </div>

        </div>
    </body>
</html>

对于以下页面:(我在上一页点击提交时没有加载,而是给了我一个&#34;服务器错误&#34;)

<?php
session_start();

if($_POST['formSubmit'] == "Submit")
{
    $errorMessage="";

    if (empty($_POST['campsessions[]'])) 
    {
        $errorMessage .="";
    }
    if (empty($_POST['campcare'])) 
    {
        $errorMessage ="";
    }
    if (empty($_POST['totalprice']))
    {
        $errorMessage ="";
    }

    $_SESSION['campcare'] = $_POST['campcare'];
    $_SESSION['campsessions'] = $_POST['campsessions'];
    $_SESSION['totalprice'] = $_POST['totalprice'];
    $_SESSION['schoolChoice'] = $_POST['schoolChoice'];
    $weeklyPrice =  $_SESSION['quickVariable'];

    $totalPrice = 0;


    while (isset($_SESSION['campsessions']))
    {   
        foreach ($weeklyPrice as $total)
            {
                $totalPrice = $totalPrice + $total;
            }
    }


}


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Active Mind And Body</title>
        <!-- BC_OBNW -->
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link href="http://activemindandbody.org/StyleSheets/ModuleStyleSheets.css"                 rel="StyleSheet" type="text/css" />
        <script type="text/javascript">var jslang='EN';</script>
        <link href="http://activemindandbody.orgStyleSheets/ModuleStyleSheets.css"                  rel="StyleSheet" type="text/css" />
        <script type="text/javascript">var jslang='EN';</script>
        <link href="http://activemindandbody.org/style.css" rel="stylesheet" type="text/            css" />
        <link rel="shortcut icon" href="../favicon.ico"> 
        <link rel="stylesheet" type="text/css" href="css/demo.css" />
        <link rel="stylesheet" type="text/css" href="style3.css" />
        <script type="text/javascript" src="js/modernizr.custom.04022.js"></script>
        <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:                     700,300,300italic' rel='stylesheet' type='text/css'>
        <link type="text/css" rel="stylesheet" href="stylesheet.css"/>
        <link href='http://fonts.googleapis.com/css?family=Alef' rel='stylesheet'                   type='text/css'>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.js"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>
        <script src="Javascript2.js" type="text/javascript"></script>    
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <!-- BC_OBNW -->
        <div id="container">
            <div id="header"><div id="header_text" style="top: 118px; left: 23px;">
                <p>Make a donation today and help&nbsp;&nbsp;âCreating a Brighter Future                      
    Together.â&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <a href="http://activemindandbody.org/donation.html">Make a donation now !</a> </p>
        </div>
        </div>
        <div id="content">
                    <section class="tabs">
                <input id="tab-1" type="radio" name="radio-set" class="tab-selector-1" checked="checked" />
                <label for="tab-1" class="tab-label-1">1. Camper's Info</label>

                <input id="tab-2" type="radio" name="radio-set" class="tab-selector-2" />
                <label for="tab-2" class="tab-label-2">2. Parent's Info</label>

                <input id="tab-3" type="radio" name="radio-set" class="tab-selector-3" />
                <label for="tab-3" class="tab-label-3">3. Emergency<br/> Contacts</label>

                <input id="tab-4" type="radio" name="radio-set" class="tab-selector-4" />
                <label for="tab-4" class="tab-label-4">4. Camper's Ride Home</label>

                <input id="tab-5" type="radio" name="radio-set" class="tab-selector-5" />
                <label for="tab-5" class="tab-label-5">5. <br/> Camp Days & Prices</label>

                 <input id="tab-6" type="radio" name="radio-set" class="tab-selector-6" />
                <label for="tab-6" class="tab-label-6">6. Confirm your details</label>

                <input id="tab-7" type="radio" name="radio-set" class="tab-selector-7" />
                <label for="tab-7" class="tab-label-7">7. <br />Paypal</label>
                    </section>

                <div class="clear-shadow"></div>

                <div class="content indent">
<br />              
Please confirm the following details:

<p class="confirmation">
<b>SPECIAL CONSIDERATIONS AND PAYMENT INFORMATION:</b><br/><br/>
&nbsp;&nbsp;Is Camper picked up? <?php echo $_SESSION['camperpickedup'] ?> <br/><br/>
&nbsp;&nbsp;Who is picking up the camper? <?php echo $_SESSION['personPickingUpCamper'] ?> <br/><br/>
&nbsp;&nbsp;Chosen Camp Sessions: <br/>
<pre> <?php 
if(isset($_SESSION['campsessions']))
{
    foreach ($_SESSION['campsessions'] as $_SESSION['campsessions'])
    {
        echo "{$_SESSION['campsessions']}\n";
    }
}
?> 
</pre> <br />
</p> 
<p>
Total Price: $<?php echo $_SESSION['totalprice'] ?><br/><br/>
</p>
<form action="amd9.php" method="post">
<input type="submit" value='Confirm' class="button greenButton" name="formSubmit"> 
</form>



                </div>

        </div>
    </body>
</html>

很抱歉,如果代码太多,请告诉我,以便我可以将其删除,因为上次没有提供正确的信息而受到了很多惩罚。

但作为一个更普遍的问题,就是我正在做的,关于使用php变量来显示我的表单,好吗? 或者这是个大问题?

2 个答案:

答案 0 :(得分:0)

while (isset($_SESSION['campsessions']))
{   
    foreach ($weeklyPrice as $total)
        {
            $totalPrice = $totalPrice + $total;
        }
}

这将是一个无限循环,因为变量永远不会被取消设置。

使用if,而不是while

答案 1 :(得分:0)

您的代码充满了错误 - 但我会说服务器关闭的原因是

while (isset($_SESSION['campsessions']))

如果isset($_SESSION['campsessions'])等于真,你可能也写了

while(true)

这是一个无限循环。 PHP会超时并给你一个错误。使用if代替。

这只是其中一件坏事。我不确定为什么你要发出5个查询来检索表中一行的6个字段,并且有一个if(空($ _ POST ['campsessions []']))看起来不正确。