更新数据库时出错 - Cpanel PHP

时间:2014-06-25 19:36:20

标签: php html mysql cpanel

我在使用cpanel创建的数据库中插入数据时遇到问题。我在提交表单时遇到错误。

我在html中创建了此表单:

<form  action="employees.php" method="post">
    <div>
                <div >
                    <label for="inputFN">FIRST NAME</label>
                    <input type="text" name="inputFN"/>
                </div>
                <div >
                    <label for="inputLN">LAST NAME</label>
                    <input type="text" name="inputLN"/>
                </div>
                <div>
                    <label for="inputTitle">TITLE</label>
                    <br />
                    <div>
                        <select name="title_select_container">
                            <option value="Select">-- * SELECT * --</option>
                            <option value="Operator">Operator</option>
            <option value="Manager">Manager</option>
            <option value="Contractor">Contractor</option>
            <option value="Worker">Worker</option>
            <option value="none">NO LONGER EMPLOYED</option>
                        </select>
                    </div>
                </div>
                <div>
                    <label for="jobCode">JOB CODE</label>
                    <br />
                    <div>
                        <select name="jobCode_select_container">
                            <option value="Select">-- * SELECT * --</option>
                            <option value="141802">141802BENT</option>
            <option value="141803B">141803BENT</option>
            <option value="141904Z">141904ZOO</option>
            <option value="142405T">142405TEK</option>
            <option value="NEW">--NEW--</option>
                        </select>
                        <input type="text" id="newJobCode"/>
                        <input type="button" id="btnNewJobCode" value="Add"/>
                    </div>
                    <script>
                        $(document).ready(function() {
                    $('#newJobCode').hide();
                    $('#btnNewJobCode').hide();

                    $("#jobCode_select_container").change(function() {
                    var val = $(this).val();

                    if (val == 'NEW') {
                        $('#newJobCode').show();
                        $('#btnNewJobCode').show();
                    } else {
                        $('#newJobCode').hide();
                        $('#btnNewJobCode').hide();
                    }
                }).change();

            }); 
        </script>
        </div>
        <div>
            <label for="lblActivity">Activity</label>
                    <br />
                    <div>
                        <select name="activity_select_container">
                            <option value="Select">-- * SELECT * --</option>
            <option value="W">Working</option>
            <option value="I">Inactive</option>
                        </select>
        </div>
                    </div>
                <input type="submit"></form>

这是我的php文件来处理它:

<?php
mysql_connect('localhost', 'user', 'pass') or die(mysql_error()); 

mysql_select_db('a_db') or die(mysql_error()); 

// escape variables for security
$firstname = mysql_real_escape_string($_POST['inputFN']);
$lastname = mysql_real_escape_string($_POST['inputLN']);
$title = mysql_real_escape_string($_POST['title_select_container']);
$jobCode = mysql_real_escape_string($_POST['jobCode_select_container']);
$activity = mysql_real_escape_string($_POST['activity_select_container']);

$query="INSERT INTO tblEmployees (empFirstName, empLastName, empTitle ,jobCode, empActivity) VALUES ('$firstname', '$lastname', '$title', '$jobCode', '$activity');";

mysql_query( $query ) or die( 'Error updating database' );
echo "Database Updated With: " .$firstname. " ".$lastname." ".$title." ".$jobCode." ".$activity ;

?> 

这就是我的表格设置方式tblEmployee set up

我认为我的insert语句可能有问题,但我不知道它是什么,因为我在其他页面上使用相同的语法(没有select下拉列表的表单),一切都是处理好了。

我还希望能够在网页本身的table上显示此数据。但除非我至少可以插入数据,否则无法开始这样做。

更新 使用@ Fred-ii-的报告建议,有问题的行是php

$title=mysql_real_escape_string($_POST['title_select_container']);
$jobCode=mysql_real_escape_string($_POST['jobCode_select_container']);
$activity=mysql_real_escape_string($_POST['activity_select_container']);

错误:

Undefined index: title_select_container in employees.php
Undefined index: jobCode_select_container in employees.php
Undefined index: activity_select_container in employees.php

我认为select元素是“表单控件,可以在表单中用于收集用户输入”,那为什么它不能检索值呢?

1 个答案:

答案 0 :(得分:0)

我建议尝试以下

<?php
mysql_connect('localhost', 'user', 'pass') or die(mysql_error()); 

mysql_select_db('a_db') or die(mysql_error()); 

// escape variables for security
$firstname = mysql_real_escape_string($_POST['inputFN']);
$lastname = mysql_real_escape_string($_POST['inputLN']);
$title = mysql_real_escape_string($_POST['title_select_container']);
$jobCode = mysql_real_escape_string($_POST['jobCode_select_container']);
$activity = mysql_real_escape_string($_POST['activity_select_container']);    

$query="INSERT INTO tblEmployees(`empFirstName`, `empLastName`, `empTitle` ,`jobCode`, `empActivity`) VALUES ('$firstname', '$lastname', '$title', '$jobCode', '$activity')";

mysql_query( $query ) or die( 'Error updating database' );
echo "Database Updated With: " .$firstname. " ".$lastname." ".$title." ".$jobCode." ".$activity ;

?>

Undefined index: title_select_container in employees.php
Undefined index: jobCode_select_container in employees.php
Undefined index: activity_select_container in employees.php

此部分声明它无法在该文件中找到title_select_container,因此您最好在if或if语句周围添加isset()以检查表单是否已发布