dongli1920 2017-02-07 08:11
浏览 35
已采纳

多个查询无效的Ajax帖子

I have html with ajax and php file. I use native php with mysqli for database. in connection, I have sricpt that named is config.php . this script :

<?php
$db = new mysqli('localhost', 'root', '', 'student');
?>

And then I have html with ajax like this :

$("form#form").submit(function(event) {
    event.preventDefault();
    var formData = new FormData($(this)[0]);
    $.ajax({
        url: 'student/action.php',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
            console.log(data);
            if (data == 0) {
                alert('all data updated');
            } else {
                alert('failed');
            }
        }
    });
    return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
    <form id='form'>
        <input type='hidden' name='type' value='new-data'>
        <input name='name' type='text'>
        <input name='class' type='text'>
        <input name='hobbies' type='text'>
        <button type='submit' name='submit'>Submit</button>
    </form>
</body>

in student/action.php script like this,

<?
php require_once '../config/config.php'; 
switch ($_POST[ 'type']) {
    case "new-data": 
    $qry1=$db->query("INSERT INTO tableA SET id=NULL, name='$_POST[name]' "); 

    $qry2=$db->query("INSERT INTO tableB SET id=NULL, name='$_POST[class]' ");

    $qry3=$db->query("INSERT INTO tableC SET id=NULL, name='$_POST[hobbies]' ");

    if($qry1 && $qry2 && $qry3) {
        echo '1';
    } else {
        echo '2';
    }
    break;
} 
?>

nah,this apps on hosting and at here the connection not stable. so when I submit by click submit, it will post data to student/action.php. it must be insert all query ($qry1, $qry2, and $qry3), but if I had bad connection it only insert some query, not all. How to solve it. thanks

</div>
  • 写回答

1条回答 默认 最新

  • dragon321723 2017-02-07 12:15
    关注

    First, never use $_POST, $_GET, $_REQUEST directly in your query (because of SQL injection), alway use mysqli_real_escape_string :

    $name=$db->mysqli_real_escape_string($_POST['name']);
    $class=$db->mysqli_real_escape_string($_POST['class']);
    $hobbies=$db->mysqli_real_escape_string($_POST['hobbies']);
    

    And don't forget quotes

    If you want to make sure all request are executed or all fail use transaction (you need innodb storage engine);

    $db->begin_transaction();
    
    //Your queries…
    
    $db->commit();
    

    If one request fail all previous requests are rolled back. you can use $db->rollback(); to cancel all queries before a commit. See php doc for more infos.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线