获得致命错误:不能使用try而不是catch或者最终

时间:2014-11-18 03:24:23

标签: php

当我尝试将我的php脚本上传到我的localhost时,我是第60行的“致命错误:无法使用try without catch或finally”。我不知道为什么会这样,我怎么能解决它。本课程的目的是在提交表单时验证潜在错误。

<?php 
include("header.php"); 
require_once('./model/contactinfo.php');
require_once('./DAO/contactDAO.php');



?>
            <div id="content" class="clearfix">
                <aside>
                        <h2>Mailing Address</h2>
                        <h3>1385 Woodroffe Ave<br>
                            Ottawa, ON K4C1A4</h3>
                        <h2>Phone Number</h2>
                        <h3>(613)727-4723</h3>
                        <h2>Fax Number</h2>
                        <h3>(613)555-1212</h3>
                        <h2>Email Address</h2>
                        <h3>info@wpeatery.com</h3>
                </aside>
                <div class="main">
                    <h1>Sign up for our newsletter</h1>
                    <p>Please fill out the following form to be kept up to date with news, specials, and promotions from the WP eatery!</p>

                    <?php
        try{
            $contactDAO = new contactDAO();

            $hasError = false;

            $errorMessages = Array();

            if(isset($_POST['phoneNumber']) ||
                isset($_POST['customerName']) || 
                isset($_POST['emailAddress'])){

                if(!is_numeric($_POST['phoneNumber']) || $_POST['phoneNumber'] == ""){
                    $hasError = true;
                    $errorMessages['phoneNumberError'] = 'Please enter a numeric phone number.';
                }

                if($_POST['customerName'] == ""){
                    $errorMessages['customerNameError'] = "Please enter a first name.";
                    $hasError = true;
                }

                if($_POST['emailAddress'] == ""){
                    $errorMessages['emailAddressError'] = "Please enter an email address.";
                    $hasError = true;
                }

                if(!$hasError){
                    $contact = new Contact($_POST['phoneNumber'], $_POST['customerName'], $_POST['emailAddress']);
                    $addSuccess = $contactDAO->addcontact($contact);
                    echo '<h3>' . $addSuccess . '</h3>';
                }
            }  
}
            if(isset($_GET['deleted'])){
                **if($_GET['deleted'] == true){**  This is where the error occurs
                    echo '<h3>Customer Deleted</h3>';
                }
            }

             ?>

                    <form name="fromcontact" id="fromcontact" method="post" action="contact.php">
                        <table>
                            <tr>
                                <td>Name:</td>
                                <td><input type="text" name="phoneNumber" id="phoneNumber" size='40'>
                                 <?php if(isset($errorMessages['firstNameError'])){
                            echo '<span style=\'color:red\'>' . $errorMessages['firstNameError'] . '</span>';
                      }
                ?></td>
                            </tr>
                            <tr>
                                <td>Phone Number:</td>
                                <td><input type="text" name="phoneNumber" id="phoneNumber" size='40'>
                <?php   if(isset($errorMessages['phoneNumberError'])){
                        echo '<span style=\'color:red\'>' . $errorMessages['phoneNumberError'] . '</span>';
                      }
                ?></td>
                            </tr>
                            <tr>
                                <td>Email Address:</td>
                                <td><input type="text" name="emailAddress" id="emailAddress" size='40'>
                                 <?php if(isset($errorMessages['emailAddressError'])){
                        echo '<span style=\'color:red\'>' . $errorMessages['emailAddressError'] . '</span>';
                      }
                ?>
                            </tr>
                            <tr>
                                <td>How did you hear<br> about us?</td>
                                <td>Newspaper<input type="radio" name="referral" id="referralNewspaper" value="newspaper">
                                    Radio<input type="radio" name='referral' id='referralRadio' value='radio'>
                                    TV<input type='radio' name='referral' id='referralTV' value='TV'>
                                    Other<input type='radio' name='referral' id='referralOther' value='other'>
                            </tr>
                            <tr>
                                <td colspan='2'><input type='submit' name='btnSubmit' id='btnSubmit' value='Sign up!'>&nbsp;&nbsp;<input type='reset' name="btnReset" id="btnReset" value="Reset Form"></td>
                            </tr>
                        </table>
                        <?php

        $contact = $contactDAO->getContact();
            if($contact){
                echo '<table border=\'1\'>';
                echo '<tr><th>contact ID</th><th>Customer Name</th>';
                foreach($contact as $contact){
                    echo '<tr>';
                    echo '<td><a href=\'edit_contact.php?contactId='. $contact->getcontactId() . '\'>' . $contact->getcontactId() . '</a></td>';
                    echo '<td>' . $contact->getcustomerName() . '</td>';
                    echo '</tr>';
                }
            }

        }catch(Exception $e){
            echo '<h3>Error on page.</h3>';
            echo '<p>' . $e->getMessage() . '</p>';            
        }
        ?>
                    </form>
                </div><!-- End Main -->
            </div><!-- End Content -->
<?php include("footer.php"); ?>

1 个答案:

答案 0 :(得分:0)

这里有一个try块:

try {
    $contactDAO = new contactDAO();

在此终止:

            echo '<h3>' . $addSuccess . '</h3>';
        }
    }
}

而你需要捕获一些内容或运行最后一段代码,否则,try中没有任何意义。

由于 实际上是代码中的catch子句,因此您很可能无法想出结束括号的位置,它&只是在try被关闭后它就好了。

我建议您格式化代码以使缩进更清晰,并且应该立即向您显示您遇到问题的原因。