表单数据未按预期传递给数据库

时间:2014-05-16 01:33:47

标签: php sql forms oop required

NEWB在这里处理了很多超出他们能力的事情(尝试学习)。简而言之,我正在建立一个表格,允许有人报告一个巨大的kaiju事件(哥斯拉,Cloverfield,Gamera,无论你喜欢什么巨型怪物 - 这很愚蠢,但它激励我)。我的表单在所有方面都如预期的那样工作,但是当我通过表单提交数据时,我收到一条错误消息,内容为“所需表单数据未通过”。

过去两个小时我已经过了表格,我无法弄清楚我做错了什么 - 我确信它的一些简单但无论它是什么都超出了我。我试图谷歌一个答案,并查看stackOverflow等 - 但我没有找到一个答案,我可以内化到足以弄清楚我做错了什么。

A working version of the form can be seen here

CODE:

    <?php //w05c09b_OOPincidentReport.php

require '../inc_0700/config_inc.php'; #provides configuration, pathing, error handling, db credentials
//END CONFIG AREA ----------------------------------------------------------
# Read the value of 'action' whether it is passed via $_POST or $_GET with $_REQUEST
if(isset($_REQUEST['act'])){$myAction = (trim($_REQUEST['act']));}else{$myAction = "";}
switch ($myAction)
{//check 'act' for type of process
    case "add": //2) Form for adding new report
        addReport();
        break;
    case "insert": //3) Insert new report
        insertExecute();
        break;
    default: //1)Show existing Reports
        showReport();
}
function showReport()
{//Select Customer
    global $config;
    get_header();
    echo '<h3 align="center">' . smartTitle() . '</h3>';

    $sql = 'select kID, incWho, incWhat, incWhen from incKaiju';
    $result = mysqli_query(IDB::conn(),$sql) or die(trigger_error(mysqli_error(IDB::conn()), E_USER_ERROR));
    if (mysqli_num_rows($result) > 0)//at least one record!
    {//show results
        echo '<table align="center" border="1" style="border-collapse:collapse" cellpadding="3" cellspacing="3">';
        echo '<tr>
                <th>Kaiju Incident Report</th>
                <th>Officer Reporting</th>
                <th>Kaiju Identified</th>
                <th>Date of Incident</th>
            </tr>
            ';
        while ($row = mysqli_fetch_assoc($result))
        {//dbOut() function is a 'wrapper' designed to strip slashes, etc. of data leaving db
            echo '<tr>
                    <td>Kid00' . (int)$row['kID'] . '</td>
                    <td>Officer ' . dbOut($row['incWho']) . '</td>
                    <td>' . dbOut($row['incWhat']) . '</td>
                    <td>' . dbOut($row['incWhen']) . '</td>
                </tr>
                ';
        }
        echo '</table>';
    }else{//no records
      echo '<div align="center"><h3>Currently No Customers in Database.</h3></div>';
    }
    echo '<div align="center"><a href="' . THIS_PAGE . '?act=add"><br />ADD REPORT</a></div>';
    @mysqli_free_result($result); //free resources
    get_footer();
}
function addReport()
{# shows details from a single customer, and preloads their first name in a form.
    global $config;

    //Inject my styles and crap here

    $config->loadhead .= '
    <script type="text/javascript" src="' . VIRTUAL_PATH . 'include/util.js"></script>
    <script type="text/javascript">
        function checkForm(thisForm)
        {//check form data for valid info
            if(empty(thisForm.incWho,"Reporting Officer ")){return false;}
            if(empty(thisForm.incWhat,"Confirm Kaiji ")){return false;}
            if(empty(thisForm.incWhen,"Date of Incident ")){return false;}

            if(!isEmail(thisForm.Email,"Please Enter a Valid Email")){return false;}
            return true;//if all is passed, submit!
        }
    </script>';

    get_header();
    echo '
    <h3 align="center">' . smartTitle() . ' List + Add + Insert</h3>
    <h4 align="center">Enter Report</h4>
    <form action="' . THIS_PAGE . '" method="post" onsubmit="return checkForm(this);">
    <table align="center">
       <tr><td align="right">Reporting Officer</td>
            <td>
                <input type="text" name="incWho" />
                <font color="red"><b>*</b></font> <em>(alphanumerics & punctuation)</em>
            </td>
       </tr>
       <tr><td align="right">Confirm Kaiji</td>
            <td>
                <input type="text" name="incWhat" />
                <font color="red"><b>*</b></font> <em>(alphanumerics & punctuation)</em>
            </td>
       </tr>
       <tr><td align="right">Date of Incident </td>
            <td>
                <input type="text" name="incWhen" />
                <font color="red"><b>*</b></font> <em>(alphanumerics & punctuation)</em>
            </td>
       </tr>
       ';

       echo '<input type="hidden" name="act" value="insert" />
       <tr>
            <td align="center" colspan="2">
                <input type="submit" value="Submit Report"><em>(<font color="red"><b>*</b> required field</font>)</em>
            </td>
       </tr>
    </table>
    </form>
    <div align="center"><a href="' . THIS_PAGE . '">Exit (Do not submit Incident Report)</a></div>
    ';
    get_footer();
}

function insertExecute()
{
    $iConn = IDB::conn();//must have DB as variable to pass to mysqli_real_escape() via iformReq()
    $redirect = THIS_PAGE; //global var used for following formReq redirection on failure
    $incWho = strip_tags(iformReq('incWho', $iConn));
    $incWhat = strip_tags(iformReq('incWhat', $iConn));
    $incWhen = strip_tags(iformReq('incWhen', $iConn));
    $Email = strip_tags(iformReq('Email',$iConn));
    //next check for specific issues with data
    if(!ctype_graph($_POST['incWho'])|| !ctype_graph($_POST['incWhat'])|| !ctype_graph($_POST['incWhen']))
    {//data must be alphanumeric or punctuation only
        feedback("First and Last Name must contain letters, numbers or punctuation");
        myRedirect(THIS_PAGE);
    }
    if(!onlyEmail($_POST['Email']))
    {//data must be alphanumeric or punctuation only
        feedback("Data entered for email is not valid");
        myRedirect(THIS_PAGE);
    }
    $sql = "INSERT INTO incKaiju (incWho, incWhat, incWhen) VALUES ('%s','%s','%s')";
    $sql = sprintf($sql,$incWho,$incWhat,$incWhen);
    @mysqli_query($iConn, $sql) or die(trigger_error(mysqli_error($iConn), E_USER_ERROR));
    #feedback success or failure of update
    if (mysqli_affected_rows($iConn) > 0)
    {//success!  provide feedback, chance to change another!
        feedback("Report added successfully!","notice"); //notice changes color to red!
    }else{//Problem!  Provide feedback!
        feedback("Report failed to submit!");
    }
    myRedirect(THIS_PAGE);
}

0 个答案:

没有答案