jjzjj

Php/Jquery 表单和数据表

coder 2023-10-25 原文

希望有人能提供帮助。我有一个客户数据库表,我试图使用 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/Jquery 表单和数据表的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  4. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  5. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

  6. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  7. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  8. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  9. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  10. ruby-on-rails - 创建 ruby​​ 数据库时惰性符号绑定(bind)失败 - 2

    我正在尝试在Rails上安装ruby​​,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf

随机推荐