使引导模式使用与/sql表分离

Posted

技术标签:

【中文标题】使引导模式使用与/sql表分离【英文标题】:Make bootstrap modal utilize seperate form/sql table 【发布时间】:2016-04-21 08:19:23 【问题描述】:

我有一个表格可以更改名为“发票”的 sql 表。我想弹出一个引导模式,它将改变一个名为“客户”的 sql 表。现在,唯一正确保存的是客户名称......我错过了什么吗?这是我到目前为止的代码......

主页:

<!-- Modal Add Customer -->
     <div class="modal fade" id="addNewCustomer" tabindex="-1" role="dialog" aria-labelledby="addNewCustomerLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Add New Customer</h4>
          </div>
           <div class="modal-body">
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="add_customer_form" method="post" class="form-horizontal myaccount" role="form">
            <div class="form-group col-lg-7">
                    <label for="customer_Name_modal">Customer Name (Last Name, First Name)</label>
                    <input type="text" class="form-control" id="customer_Name_modal" name="customer_Name_modal" placeholder="Last Name, First Name">
                    <span class="help-block"></span>
                </div>
            <div class="form-group col-lg-7">
                    <label for="customer_Phone_modal">Phone Number (555-555-5555)</label>
                    <input type="text" class="form-control" id="customer_Phone_modal" name="customer_Phone_modal" placeholder="555-555-5555">
                    <span class="help-block"></span>
                </div>
            <div class="form-group col-lg-7">
                    <label for="customer_Email_modal">Email (johndoe@gmail.com)</label>
                    <input type="text" class="form-control" id="customer_Email_modal" name="customer_Email_modal" placeholder="john@doe.com">
                    <span class="help-block"></span>
                </div>
            <div class="form-group col-lg-7">
                    <label for="customer_Address1_modal">Address Line 1 </label>
                    <input type="text" class="form-control" id="customer_Address1_modal" name="customer_Address1_modal" placeholder="">
                    <span class="help-block"></span>
                </div>
            <div class="form-group col-lg-7">
                    <label for="customer_Address2_modal">Address Line 2 </label>
                    <input type="text" class="form-control" id="customer_Address2_modal" name="customer_Address2_modal" placeholder="">
                    <span class="help-block"></span>
                </div>
            <input type="hidden" id="current_element_id">
        </form>
        </div>
        <div class="modal-footer">
            <button type="button" id="add_new_customer_btn" class="btn btn-primary" data-loading-text="Saving Customer...">Save changes</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
            </div>
            </div>
            </div>

表单保存代码:

var save_new_customer = function()

    $('#add_new_customer_btn').button('loading');

    data = 
            customerName : $('#customer_Name_modal').val(),
            customerPhone : $('#customer_Phone_modal').val(),
            customerEmail : $('#customer_Email_modal').val(),
            customerAddress1 : $('#customer_Address1_modal').val(),
            customerAddress2 : $('#customer_Address2_modal').val(),
    


    $.ajax(
         url: "ajax.php",
         dataType: "json",
         method: 'post',
         data: 
            data : data,
            type: 'saveNewCustomer'
         ,
         success: function(result)
            if( (typeof result.success !== "undefined") && result.success )

                $("html, body").animate( scrollTop: 0 , "slow");
                $('#add_new_customer_btn').button('reset');

                $('#addNewCustomer').modal('show');
                element_id = $('#current_element_id').val();
                $('#customerName_'+element_id).val($('#customer_Name_modal').val());
                $('#phone_'+element_id).val( $('#customer_Email_modal').val() );
                $('#email_'+element_id).val($('#customer_Phone_modal').val());
                $('#addressLine1_'+element_id).val($('#customer_Address1_modal').val());
                $('#addressLine2_'+element_id).val($('#customer_Address2_modal').val());

                $('#add_customer_form')[0].reset();
                $('#add_customer_form').find("div.form-group").removeClass('has-success');
                $('#message_h1').show();
                message('success', CUSTOMER_ADD_SUCCESS);
            else
                message('fail', CUSTOMER_ADD_FAIL);
            
        
     );



Sql 代码:

if(isset($_POST['type']) && $_POST['type'] == 'saveNewCustomer' )
    $res = array();
    $res['success'] = false;

    if(!isset($_POST['data']) || empty($_POST['data']))
        echo json_encode($res);exit;
     
    $data = $_POST['data'];

    $customerName = mysqli_real_escape_string( $db->con, trim( $data['customerName'] ) );
    $customerPhone = mysqli_real_escape_string( $db->con, trim( $data['phone'] ) );
    $customerEmail = mysqli_real_escape_string( $db->con, trim( $data['email'] ) );
    $customerAddress1 = mysqli_real_escape_string( $db->con, trim( $data['addressLine1'] ) );
    $customerAddress2 = mysqli_real_escape_string( $db->con, trim( $data['addressLine2'] ) );

    $uuid = uniqid();
    $result['operation'] = 'insert';
    $query = "INSERT INTO customers (id, customerName, phone, email, addressLine1, addressLine2, uuid) 
            VALUES (NULL, '$customerName', '$customerPhone', '$customerEmail', '$customerAddress1', '$customerAddress2', '$uuid');";


    if(mysqli_query($db->con, $query))
        mysqli_close($db->con);
        $res['success'] = true;
    
    echo json_encode($res);exit;



【问题讨论】:

【参考方案1】:

您在 PHP 中使用了错误的键

您正在调用这些键:

$customerName = mysqli_real_escape_string( $db->con, trim( $data['customerName'] ) );
$customerPhone = mysqli_real_escape_string( $db->con, trim( $data['phone'] ) );
$customerEmail = mysqli_real_escape_string( $db->con, trim( $data['email'] ) );
$customerAddress1 = mysqli_real_escape_string( $db->con, trim( $data['addressLine1'] ) );
$customerAddress2 = mysqli_real_escape_string( $db->con, trim( $data['addressLine2'] ) );

但你应该打电话:

$customerName = mysqli_real_escape_string( $db->con, trim( $data['customerName'] ) );
$customerPhone = mysqli_real_escape_string( $db->con, trim( $data['customerPhone'] ) );
$customerEmail = mysqli_real_escape_string( $db->con, trim( $data['customerEmail'] ) );
$customerAddress1 = mysqli_real_escape_string( $db->con, trim( $data['customerAddress1'] ) );
$customerAddress2 = mysqli_real_escape_string( $db->con, trim( $data['customerAddress2'] ) );

要解决此问题,只需复制并粘贴(替换)SQL 代码中的变量赋值。

【讨论】:

我在您发帖前 2 秒发现了这一点,哈哈。这已经让我发疯了一个小时!谢谢

以上是关于使引导模式使用与/sql表分离的主要内容,如果未能解决你的问题,请参考以下文章

引导表分页下拉菜单不起作用 - Rails

如何在 React 引导表分页中选择默认的项目数

Vue+SpringBoot 前后端分离实战(mybatisplus多表分页查询+博客显示)

如何启用 Bootstrap 表分页和搜索?

MyCat头脑风暴

Oracle优化——单表分页优化