简体   繁体   中英

Data from a form won't pass into MySQL database

I've successfully managed to create a php login form using various tutorials online. But now I want the user to be able to add a device to their account if you will which will be linked to their user ID that was given to them on sign up to the website.

I created a second table to hold the data on the devices which includes its own auto increment ID for that device, this is the primary key. The table also has another column for the user IDs that is a foreign key. I want to show which user has that particular device. I'm new to PHP and MySQL and I can't seem to get this particular form to submit the data to the MySQL database like I previously did with the sign up form.

I keep the database details in a separate folder and refer back to it when it's needed:

<?php

$servername = "xxx";
$dBUsername = "xxx";
$dBPassword = "xxx";
$dBName = "xxx";

$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);

if (!$conn) {
  die("Connection failed: ".mysqli_connect_error());
}

Then this is my form for the user, once logged in, to add a new device (tracker) to their account:

<?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"
  ?>

In the require header.php I keep the 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>

Then finally in trackerSub.inc.php which is the form action I have:

<?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

The users table

Any help would be really appreciated, as I said earlier I'm new to PHP and MySQL and I've spent the last two days trying to fix these and researching but I can't quite see where I went wrong.

Many thanks, Madamot

Try this let me know if its works

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();
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM