为什么数据不正确提交?

时间:2014-05-17 20:23:36

标签: php sql database forms

Newb在这里使用表单和sql。

我发生了某种交叉布线,其中eMail输入的表单输入名称(' incEmail')实际上是插入数据库而不是输入的值。使用表单(see here: http://zephir.seattlecentral.edu/~jstein11/itc250/z14/sandbox/w05c09__eMail.php)kID输入的任何数据也保持为零'而不是递增。查看屏幕截图,最后两个条目,列表/显示为' 0')。

总而言之,对于每个创建的新条目,kID保持为零。为电子邮件输入的数据不会开始提交到数据库,而是将输入字段的名称提交给数据库。

enter image description here

        <?php /
# '../' works for a sub-folder.  use './' for the root
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 Report
    global $config;
    get_header();
    echo '<h3 align="center">' . smartTitle() . '</h3>';
    $sql = 'select kID, incWho, incEmail, 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>eMail</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>' . dbOut($row['incEmail']) . '</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 Reports 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 Report, 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(!isEmail(thisForm.incEmail,"Please Enter a Valid Email")){return false;}
            return true;//if all is passed, submit!

            if(empty(thisForm.incWhat,"Confirm Kaiji ")){return false;}
            if(empty(thisForm.incWhen,"Date of Incident ")){return false;}
        }
    </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">Email</td>
               <td>
                   <input type="text" name="incEmail" />
                   <font color="red"><b>*</b></font> <em>(valid email only)</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>
       <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));
    $Email = strip_tags(iformReq('incEmail',$iConn));//if no email then comment line out
    $incWhat = strip_tags(iformReq('incWhat', $iConn));
    $incWhen = strip_tags(iformReq('incWhen', $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("Data submitted may only contain letters, numbers or punctuation");
        myRedirect(THIS_PAGE);
    }
    if(!onlyEmail($_POST['incEmail']))
    {//data must be alphanumeric or punctuation only
        feedback("Data entered for email is not valid");
        myRedirect(THIS_PAGE);
    }
    //build string for SQL insert with replacement vars, %s for string, %d for digits
    $sql = "INSERT INTO incKaiju (incWho, incEmail, incWhat, incWhen) VALUES ('%s','%s','%s','%s')";
    //$sql = "INSERT INTO incKaiju (incWho, incEmail, incWhat, incWhen) VALUES ('%s','%s','%s',NOW())";
    # sprintf() allows us to filter (parameterize) form data
    $sql = sprintf($sql,$incWho,incEmail,$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);
}

1 个答案:

答案 0 :(得分:1)

要解决电子邮件的问题:

$sql = sprintf($sql,$incWho,$incEmail,$incWhat,$incWhen);

你错过了那里的$。

如果kid列未自动递增,则它不会在新插入时自动递增。在MySQL中相应地修改表:

ALTER TABLE incKaiju CHANGE kID kID INT(10) AUTO_INCREMENT PRIMARY KEY;