Php/Jquery 表单和数据表

标签 php jquery mysql forms datatables

希望有人能提供帮助。我有一个客户数据库表,我试图使用 jquery 数据表在页面上显示它。该页面还将包含一个表单,我可以在其中输入数据,这将更新数据库并显示在下表中。我有 3 个正在使用的文件。这是代码:

索引.php

    <html> 
    <head> 
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.js"></script>
    <script type="text/javascript" language="javascript" src="jquery.dataTables.js"></script>
    <script type="text/javascript" charset="utf-8">
            /* Table initialisation */
            $(document).ready(function() {
                $('#datatable').dataTable( {
                    "sDom": "<'row'<'span8'l><'span8'f>r>t<'row'<'span8'i><'span8'p>>"
                });
            });
    </script>
    <script type="text/javascript" language="javascript" src="jquery.form.js"></script>
    <script type="text/javascript"> 
        // prepare the form when the DOM is ready 
            $(document).ready(function() { 
            // bind form using ajaxForm 
            $('#htmlForm').ajaxForm({ 
                // target identifies the element(s) to update with the server response 
                target: '#datatable', 
                resetForm: 'true',

                // success identifies the function to invoke when the server response 
                // has been received; here we apply a fade-in effect to the new content 
                success: function() { 
                    $('#datatable').fadeIn('slow'); 
                } 
            });
        }); 
    </script>
</head>
<body>

        <form id="htmlForm" action="customers.php" method="post">
                First:  <input type="text" name="first" /><br/>
                Last:   <input type="text" name="last" /><br/>
                Phone:  <input type="text" name="phone" /><br/>
                Mobile: <input type="text" name="mobile" /><br/>
                Fax:    <input type="text" name="fax" /><br/>
                Email:  <input type="text" name="email" /><br/>
                Web:    <input type="text" name="web" /><br/><br/>
            <input id="btnReload" value="Add Customer" type="submit"> 
        </form>
        <br />

<!---------------------
<-- CUSTOMER TABLE ----
----------------------->

    <?php
    //MySQL Database Connect
     include 'customers.php';
    ?>

</body>
</html>

客户.php

    <?php

    //MySQL Database Connect
    include 'config.php';

    $_POST['first'] = "undefine"; 
    $_POST['last'] = "undefine"; 
    $_POST['phone'] = "undefine"; 
    $_POST['mobile'] = "undefine"; 
    $_POST['fax'] = "undefine"; 
    $_POST['email'] = "undefine"; 
    $_POST['web'] = "undefine"; 

    $sql="INSERT INTO contacts (first, last, phone, mobile, fax, email, web)
    VALUES
    ('$_POST[first]','$_POST[last]','$_POST[phone]','$_POST[mobile]','$_POST[fax]','$_POST[email]','$_POST[web]')";

        if (!mysql_query($sql,$db))
            {
                die('Error: ' . mysql_error());
            }

            $query="SELECT * FROM contacts";
            $result=mysql_query($query);


?>
<h2>Customers Table</h2>
<?php

    $query="SELECT * FROM contacts";
    $result=mysql_query($query);

?>

    <table cellpadding="0" cellspacing="0" border="0" class="bordered-table zebra-striped" id="datatable">
        <thead>
            <tr>
                <th>First</th>
                <th>Last</th>
                <th>Phone</th>
                <th>Mobile</th>
                <th>Fax</th>
                <th>Email</th>
                <th>Web</th>
            </tr>
        </thead>

    <?php   
    $num=mysql_numrows($result);

    $i=0;
    while ($i < $num) {

        $first=mysql_result($result,$i,"first");
        $last=mysql_result($result,$i,"last");
        $phone=mysql_result($result,$i,"phone");
        $mobile=mysql_result($result,$i,"mobile");
        $fax=mysql_result($result,$i,"fax");
        $email=mysql_result($result,$i,"email");
        $web=mysql_result($result,$i,"web");

        ?>
            <tbody>
                <tr>
                    <td><?php echo $first ?></td>
                    <td><?php echo $last ?></td>
                    <td><?php echo $phone ?></td>
                    <td><?php echo $mobile ?></td>
                    <td><?php echo $fax ?></td>
                    <td><?php echo $email ?></td>
                    <td><?php echo $web ?></td>
                </tr>

    <?php
    $i++;
    }

?>

配置.php

    <?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "address";
$prefix = "";
$db = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $db) or die("Could not select database");
?>

当我输入数据时,表单正在更新表格,但每次我重新加载页面时,它都工作得相当好,一条空白记录被插入到数据库中。有人可以就此问题的最佳解决方案提出建议。

最佳答案

正在插入空行,因为即使未提交表单,您的插入代码也会运行。您可以通过在插入之前检查以确保它是一个 POST 操作来解决此问题:

include 'config.php';

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    // your insertion code here
}

//or even better:
if (!empty($_POST)) {
    //insertion code
}

$query="SELECT * FROM contacts";
$result=mysql_query($query);
...

但是,您有一个更严重的问题,因为您没有对输入进行清理。如果有人输入 '); DELETE FROM contacts; 在您的 web 字段中?这称为 SQL 注入(inject)攻击。

为避免这种情况,您应该使用准备好的语句或使用 mysql_real_escape_string 来保护您的应用程序免受此类攻击。

关于Php/Jquery 表单和数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9055692/

相关文章:

php - 如何在 php 中执行搜索?

php - 实现渐进增强时如何防止重复代码?

javascript - 为什么 Javascript 需要匿名函数来延迟执行?

mysql - 在 MySQL 中查找换行符和回车符 (\r\n)

MySQL - 使用复合键删除除最新行之外的所有行

mysql - Node mysql连接池导出

php - <?php bp_activity_id() ?> 在 buddypress 中,有人达标了吗?

php pdo 准备语句动态过滤器

javascript - 如何根据数量和名称拆分字符串列表?

javascript - 在靠近底部的地方拖动时在可移动的 DIV 内部滚动