duanliang5051 2016-03-29 15:06
浏览 62
已采纳

如何使用PHP,AJAX和JSON使用mysqli插入数据?

PHP

    $data = array(
        'success' => false,
        'errors' => array()
    );

    if(isset($_POST['inputs'])) {


            $inputs = $_POST['inputs'];
            foreach($inputs as $input) {

                    $name = $input['name'];
                    $value = $input['value'];

                    switch ($name) {
                            case 'email':
                                    if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
                                            $msg = "You must provide a valid e-mail address";
                                    }
                                    break;

                            default:
                                    $msg = "Sorry, we do not understand the data, please try again";
                                    break;
                    }

                    if(!empty($msg)) {
                            $data['errors'][] = array(
                                    'msg' => $msg,
                                    'field' => $name
                            );
                    }
            }

            if(empty($data['errors'])) {
                    $data['success'] = true;

                    $json = file_get_contents('php://input');
                    $obj = json_decode($json,true);

                    foreach ($obj as $item) {
                            $query = "INSERT INTO users (email)

                            VALUES

                            ('".$item['email']."')";

                            $objConnection->query($query) or die($objConnection->error);
                            $objConnection->close();
                    }

            }

    } else {
            $data['errors'][] = "Data missing";
    }
    header("Content-type: application/json; charset=UTF-8");
    echo json_encode((object)$data);

Jquery / AJAX

$(function() {
    $('.submit').click(function() {

            var formInputs = new Array();

            var formId = $(this).parent().attr('id');

            $('#' + formId + ' input').each(function() {

                    var obj = {
                        'value': $(this).val(),
                        'name': $(this).attr('name')
                    };

                    formInputs.push(obj);
            });

            $.ajax({
                    url: 'add.php',
                    type: 'POST',
                    dataType: 'json',
                    cache: false,
                    data: {
                            'inputs': formInputs
                    },
                    success: function(data) {
                            if(data.success) {
                                    $(".hej").html("worked");
                            } else {
                                    $.each(data.errors, function() {
                                            var list = "<p>"+this.msg+"</p>";
                                            $(".hej").html(list);
                                    });
                            }
                    }
            });

            return false;

    });
});

I have been searching for an answer, to see if I could figure it out myself, I did find this Insert data with JSON and mysql . It seems to be working, besides that it gives me an error that says: "Invalid argument supplied for foreach()". If anyone has a better method / solution to insert the data, I am open ears.

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 VB6.0操作 webview2内核的浏览器如何精确实现网页弹窗处置
      • ¥15 pr导出的视频打不开,提示“缺少编解码器”怎么解决
      • ¥15 html里js获取php参数值不成功,帮改代码
      • ¥20 如何控制ant design的InputNumber组件 最多输入5位小数
      • ¥15 c语言学生基本信息管理系统
      • ¥100 火车头采集器采集求解
      • ¥88 关于#运行时间 时间重叠 和非重叠#的问题,如何解决?
      • ¥15 C语言,密切接触者追踪
      • ¥20 关于计算机网络问题,请附带讲解
      • ¥30 自动识别图像目标并判断