我已经成功使用各种在线教程成功创建了一个php登录表单。但是现在,我希望用户能够将设备添加到他们的帐户(如果您愿意的话),该设备将链接到在注册网站时提供给他们的用户ID。
我创建第二表来保存,其包括用于该设备自身的自动增量ID的设备上的数据,这是主键。该表还因为这是一个外键的用户ID的另一列。我想显示哪些用户有特定的设备。我是PHP和MySQL的新手,我似乎无法像以前使用注册表单一样,获得这种特殊的表单将数据提交到MySQL数据库。
我保持数据库细节在一个单独的文件夹,并在需要时参照回到它:
<?php
$servername = "xxx";
$dBUsername = "xxx";
$dBPassword = "xxx";
$dBName = "xxx";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
然后,这是我的针对用户的形式,登录后,到新设备(跟踪器)添加到他们的帐户:
<?php
require "header.php";
?>
<main>
<?php
if (isset($_SESSION['userId'])) {
echo '<form action="includes/logout.inc.php" method="post">
<button type="submit" name="logout-submit">Log Out</button>
</form>';
echo '<form action="includes/trackerSub.inc.php" method="post">
<input type="text" name="trackeruid" placeholder="Tracker Name...">
<input type="text" name="trackerType" placeholder="Tracker Type...">
<button type="submit" name="tracker-submit">Submit</button>
</form>';
}
else {
echo '<form action="includes/login.inc.php" method="post">
<input type="text" name="mailuid" placeholder="Username/E-mail...">
<input type="password" name="pwd" placeholder="Password...">
<button type="submit" name="login-submit">Login</button>
</form>
<a href="signup.php">Sign Up</a>';
}
?>
</main>
<?php
require "footer.php"
?>
在需要的header.php我保持在session_start():
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Mother Bird</title>
<link rel="stylesheet" href="master.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
然后,在最后trackerSub.inc.php是表单动作我有:
<?php
if (isset($_POST['tracker-submit'])) {
require 'dbh.inc.php';
$trackeruid = $_POST['trackeruid'];
$trackerType = $_POST['trackerType'];
$idUser = $_SESSION['userId'];
if (empty($trackeruid) || empty($trackerType)) {
header("Location: ../dashboard.php?error=emptyfields");
}
else if (!preg_match("/^[a-zA-Z0-9]*$/", $trackeruid)) {
header("Location: ../dashboard.php?error=invalidtrackeruid");
exit();
}
else {
$sql = "INSERT INTO trackers (idUsers, uidTracker, trackerType) VALUES (?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../dashboard.php?error=sqlerror");
exit();
}
else {
mysqli_stmt_bind_param($stmt, "iss", $idUser, $trackeruid, $trackerType);
mysqli_stmt_execute($stmt);
header("Location: ../dashboard.php?signup=success");
exit();
}
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
else {
header("Location: ../dashboard.php");
exit();
}
The trackers table in phpMyAdmin looks like this
任何帮助将不胜感激,正如我之前说过的,我是PHP和MySQL的新手,我花了最后两天的时间来修复这些问题并进行研究,但我不知道自己出了什么问题。 / p>
非常感谢, madamot
答案 0 :(得分:1)
尝试这个让我知道它是否有效
if(isset($_POST['tracker-submit']))
{
require 'dbh.inc.php';
$trackeruid = $_POST['trackeruid'];
$trackerType = $_POST['trackerType'];
$idUser = $_SESSION['userId'];
if(empty($trackeruid) || empty($trackerType))
{
header("Location: ../dashboard.php?error=emptyfields");
}
else if(!preg_match("/^[a-zA-Z0-9]*$/", $trackeruid))
{
header("Location: ../dashboard.php?error=invalidtrackeruid");
exit();
}
else
{
$sql = "INSERT INTO trackers (idUsers, uidTracker, trackerType) VALUES ('$idUser', '$trackeruid', '$trackerType')";
$res = mysqli_query($conn,$sql);
if(!$res)
{
header("Location: ../dashboard.php?error=sqlerror");
exit();
}
else
{
header("Location: ../dashboard.php?signup=success");
exit();
}
}
mysqli_close($conn);
}
else {
header("Location: ../dashboard.php");
exit();
}