使用Jquery将Javascript数组转换为mysql数据库

使用Jquery将Javascript数组转换为mysql数据库,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我想知道如何将x,y坐标的javascript数组发送到Mysql数据库?我没有任何表格或任何东西 我是mysql的新手,所以我基本上知道如何使用以下连接: $connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname); 但我不知道如何将其连接到javascript。我知道这和代码有关 jQuery.post(url, data); 任何帮助都将不胜感激 搜索ajax。将数据从客户端发送到服务器端: 例如: $('select

我想知道如何将x,y坐标的javascript数组发送到Mysql数据库?我没有任何表格或任何东西

我是mysql的新手,所以我基本上知道如何使用以下连接:

$connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
但我不知道如何将其连接到javascript。我知道这和代码有关

jQuery.post(url, data);

任何帮助都将不胜感激

搜索ajax。将数据从客户端发送到服务器端:

例如:

  $('selector').on('click', function(){
      var coordinates = [x,y];

      $.ajax({
           method: 'POST',
           url: 'yourURL',
           data: {coordinates: coordinates},
           success: function(response){
               Console.log(response);
           }
        });
     });
在PHP中:

    $coor = $_POST['coordinates'];
    echo $coor;

    //Search the manual of php for storing it in the database

注意:不要使用mysql,因为它已被弃用,请使用mysqli或PDO。

我已经完成了这个小的单页演示。看看它可能会帮助你理解它是如何工作的

要使其正常工作,您需要将其放在服务器上(不可能!),并有一个包含
x
y
字段的表。然后,用您自己的凭据替换数据库凭据

参考资料

现场演示

注意

这个不检查类型(您可以输入任何字符串),但您可以添加它

test.php

<?php

// Replace these parameters to match your database
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'mydatabase';
$table  = 'coordinatesTable';

// Connect
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

// If parameters are received
if(isset($_POST['x']) && isset($_POST['y'])){

    $error_messages = array();

    if ($mysqli->connect_errno) {
        $error_messages[] = "Couldn't connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    // Prepare insert
    if (!($stmt = $mysqli->prepare("INSERT INTO `$table` (x,y) VALUES (?,?)"))) {
        $error_messages[] = "Couldn't prepare statement: (" . $mysqli->errno . ") " . $mysqli->error;
    }

    // Bind parameters x and y
    if (!$stmt->bind_param("dd", $_POST['x'], $_POST['y'])) {
        $error_messages[] = "Couldn't bind parameters: (" . $stmt->errno . ") " . $stmt->error;
    }

    // Execute the insert
    if (!$stmt->execute()) {
        $error_messages[] = "Couldn't execute the query: (" . $stmt->errno . ") " . $stmt->error;
    }

    // Prepare some data to return to the client (browser)
    $result = array(
        'success' => count($error_messages) == 0,
        'messages' => $error_messages
    );

    $stmt->close();

    // Send it
    echo json_encode($result);
    // Exit (do not execute the code below this line)
    exit();

} // end if


// Fetch all the coordinates to display them in the page
$res = $mysqli->query("SELECT x,y FROM `$table`");
$rows = array();
while ($row = $res->fetch_assoc()) {
    $rows[] = $row;
}

$mysqli->close();
?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Test page</title>
    <style>
    table{ border-collapse: collapse; }
    th,td{ border: 1px solid #888; padding: .3em 2em; }
    </style>
</head>
<body>
    <h1>New coordinates</h1>
    <p>
        x: <input type="text" id="x"> 
        y: <input type="text" id="y">
        <button id="addBtn">Add</button>
    </p>
    <table id="coordinates">
        <tr>
            <th>x</th>
            <th>y</th>
        </tr>
<?php

if(count($rows)){
    foreach($rows as $row){
?>
        <tr>
            <td><?=$row['x']?></td>
            <td><?=$row['y']?></td>
        </tr>
<?php
    }
}

?>
    </table>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $(function(){
            $('#addBtn').click(postCoordinates);

            function postCoordinates(){
                var x = $('#x').val(),
                    y = $('#y').val();

                if(x.length && y.length){
                    // Change this if it is on a different page
                    var url = window.location.href;
                    $.ajax({
                        'url': url,
                        'method': 'post',
                        'data': {'x': x, 'y': y},
                        'dataType': 'json', // The server will return json
                        'success': function(res){
                            if(res.hasOwnProperty('success') && res.success){
                                $('#x, #y').val(''); // Empty the inputs
                                $('#coordinates').append('<tr><td>'+x+'</td><td>'+y+'</td></tr>');
                            } else if(res.hasOwnProperty('messages')) {
                                alert( res.messages.join('\n') );
                            }
                        },
                        'error': function(x, s, e){
                            alert( 'Error\n' + s + '\n' + e );
                        }
                    });
                } else {
                    alert('You need to provide x and y coordinates');
                }
            }
        });
    </script>

</body>
</html>

您正在搜索的ajax@kdlcruz本教程已过时(2013年2月),不应遵循。它使用的是
mysql
,在PHP7.0.0中删除了它。@blex已删除。谢谢。cooardinates数组是用来画一条线的,所以我试着发送类似这样的东西[x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6]。。我试图将数据更改为一个值“data”:“{'points':data}”,但不断出现错误..:(这是到目前为止的错误消息error parsererror SyntaxError:意外令牌