最好的方法使用PHP / AJAX / MySQL的添加记录到数据库?最好的、数据库、方法、PHP

2023-09-11 01:26:14 作者:冷 眸 ゞ

想知道,如果有人能提出一个更好的方法,以目前的方法我都集成PHP / JS /的MySQL。

Wondering if someone can suggest a better approach to the current method I have of integrating php/js/mysql.

我用这个方法就好了,返回的样本数据,设置了独特的价值等,以及它工作得很好(有多种验证方法来减缓的攻击企图)。不过,我想现在添加相对大量的数据,不知道是否有更好的方法?

I use this method just fine for returning sample data, setting a unique value, etc, and it works just fine (with various authentication methods to slow an attack attempt). However, I want to now add relatively large amount of data and wondering if there is a better method?

目前我这样做:

JS

$.ajax( {
    type : "GET",
    url : "_dbGetSomeData",
    dataType : "html",
    success: function(data) {
        parseData(data);
    }
});

PHP

<?php 
include '_common.php'; 
include '_auth.php';

$INPUT_EMAIL= mysql_real_escape_string($_GET["e"]);
$INPUT_FINGERPRINT = $_SESSION[SESH_VAR_NAME];

//--- Do some SQL stuff in the DB
?>

这工作正常一次性请求数据或设置一个值,但我可以用这个方法来保存更多的数据?我真的不想要开始搞乱了巨大的查询字符串的帖子到_db PHP文件。

This works fine for a one-off request for data or setting one value, but can I use this method to save more data? I don't really want to start messing about with a huge querystring posts to the _db PHP file.

推荐答案

下面是你应该怎么做Ajax请求使用jQuery / PHP / MySQL的

Here's an example of how you should do Ajax requests with jQuery/PHP/MySQL

JS

var postData = {
    first_name: 'Foo',
    last_name: 'Bar',
    phone: '1-555-432-1234'
};

$.post('path/to/ajax/url.php', postData)
 .done(function(response) {
    alert("Data Loaded: " + response);
 });

PHP

// Connect to MySQL
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

// We want do some validation on the data so we can make a function called
// `validate()` if you want.
$data = validate($_POST);

$stmt = $dbh->('INSERT INTO my_table (first_name, last_name, phone) 
    VALUES (:first_name, :last_name, :phone)');

$stmt->execute($data);