在HTML中使用PHP将对象值发送到mysql数据库

时间:2015-01-27 22:48:30

标签: php mysql database

我正在创建一个网站,通过PHP将JavaScript对象的值发送到MySQL数据库

以下是代码:

<!DOCTYPE html>
<html>
<body>
<p>Creating a JavaScript Object.</p>

<p id="demo"></p>

<script>
    var person = {
    firstName : "John",
    lastName  : "Doe",
    age       : 50,
    eyeColor  : "blue"
};

</script>

</body>
</html>

总的来说,我的问题是如何使用PHP将对象数据发送到MySQL?

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>

如果我在打印出来之前输入代码:

  

connect_error){die(“连接失败:”。$ conn-&gt; connect_error);} echo“已成功连接”;?&gt;

1 个答案:

答案 0 :(得分:2)

听起来我觉得你正试图从不知道如何使用PHP和MySQL跳到添加JavaScript。

首先让我举一个如何处理所有这些事情的例子。 以下是包含所有这些文件的回购:https://github.com/Goddard/simplelogin-example

这就是连接数据库的原因:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

define("__DB_NAME__", 'job');
define("__DB_DSN__", 'mysql:dbname=' . __DB_NAME__ . ';host=127.0.0.1');
define("__DB_USERNAME__", 'root');
define("__DB_PASSWORD__", '');

if(session_id() == '') {
  session_start();
}

if(!isset($_SESSION['username']))
{
    $_SESSION['username'] = NULL;
}

//database setup
try {
    $db = new PDO ( __DB_DSN__, __DB_USERNAME__, __DB_PASSWORD__ );
    $db->query ( "use " . __DB_NAME__);
}

catch ( PDOException $e ) {
    echo 'Could not connect : ' . $e->getMessage ();
}
?>

这适用于数据库信息:

<?php

include("db.php");

if(trim(htmlentities(addslashes(filter_input(INPUT_GET, 'type')), ENT_QUOTES)) === "loginUser")
{
    try {
        $username = trim(filter_input(INPUT_GET, 'username'));
        $password = trim(filter_input(INPUT_GET, 'password'));

        $fetch = $db->prepare("SELECT * FROM `users` WHERE user_name = :username");
        $fetch->bindParam(':username', $username, PDO::PARAM_STR);
    $fetch->execute();
        $result = $fetch->fetch(PDO::FETCH_OBJ);

        if($result)
        {
            if(password_verify($password, $result->password_hash))
            {
                $currentDateTime = date('Y-m-d H:i:s');

                $update = $db->prepare("UPDATE `users` SET `last_login` = :lastlogin WHERE `client_id` = :clientid");
                $update->bindParam(':lastlogin', $currentDateTime);
                $update->bindParam(':clientid', $result->client_id);
                $loginUpdate = $update->execute();

                $resultArray['error'] = 0;
                $resultArray['errorMessage'] = "None";
                $resultArray['userName'] = $result->user_name;
                $_SESSION['username'] = $result->user_name;

                echo json_encode($resultArray);
            }

            else
            {
                $resultArray['error'] = 1;
                $resultArray['errorMessage'] = "Incorrect Password";
                echo json_encode($resultArray);
            }
        }

        else
        {
            $resultArray['error'] = 1;
            $resultArray['errorMessage'] = "Incorrect Username";
            echo json_encode($resultArray);
        }

    } catch (PDOException $e) {
        $resultArray['error'] = 1;
        $resultArray['errorMessage'] = $e->getMessage();
        echo json_encode($resultArray);
    }
}