使用 php 将实体添加到数据存储
Adding an entity to datastore with php
我正在尝试将实体添加到数据存储区。谁能看到我哪里出错了?
我已经能够编辑实体但无法添加实体。
register.php
require_once __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Datastore\DatastoreClient;
$projectId = "assignment-2-2020";
$datastore = new DatastoreClient([ 'projectId' => $projectId]);
$transaction = $datastore->transaction();
$key = $datastore->key('user');
$task = $transaction->lookup ($key);
$task['Name/ID'] = $_POST['id'];
$task['name'] = $_POST['name'];
$task['password'] = $_POST['password'];
$transaction->update($task);
$transaction->commit();
$_SESSION['username'] = $_POST['name'];
if (!is_null($_POST['name'])) {
echo '<script type="text/javascript">
window.location = "/main.php"
</script>';
}
?>
这是我的数据存储
我的解决方法到底!
<?php
$kind = 'user';
$id = $_POST['id'];
$name = $_POST['name'];
$password = $_POST['password'];
require_once __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Datastore\DatastoreClient;
$projectId = "assignment-2-2020";
$datastore = new DatastoreClient([ 'projectId' => $projectId]);
$key = $datastore->key($kind, $id);
$task = $datastore->entity($key, [
'name' => $name,
'password' => $password,
]);
$datastore->insert($task);
$_SESSION['username'] = $_POST['name'];
if (!is_null($_POST['name'])) {
echo '<script type="text/javascript">
window.location = "/main.php"
</script>';
}
?>
我正在尝试将实体添加到数据存储区。谁能看到我哪里出错了? 我已经能够编辑实体但无法添加实体。
register.php
require_once __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Datastore\DatastoreClient;
$projectId = "assignment-2-2020";
$datastore = new DatastoreClient([ 'projectId' => $projectId]);
$transaction = $datastore->transaction();
$key = $datastore->key('user');
$task = $transaction->lookup ($key);
$task['Name/ID'] = $_POST['id'];
$task['name'] = $_POST['name'];
$task['password'] = $_POST['password'];
$transaction->update($task);
$transaction->commit();
$_SESSION['username'] = $_POST['name'];
if (!is_null($_POST['name'])) {
echo '<script type="text/javascript">
window.location = "/main.php"
</script>';
}
?>
这是我的数据存储
我的解决方法到底!
<?php
$kind = 'user';
$id = $_POST['id'];
$name = $_POST['name'];
$password = $_POST['password'];
require_once __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Datastore\DatastoreClient;
$projectId = "assignment-2-2020";
$datastore = new DatastoreClient([ 'projectId' => $projectId]);
$key = $datastore->key($kind, $id);
$task = $datastore->entity($key, [
'name' => $name,
'password' => $password,
]);
$datastore->insert($task);
$_SESSION['username'] = $_POST['name'];
if (!is_null($_POST['name'])) {
echo '<script type="text/javascript">
window.location = "/main.php"
</script>';
}
?>