0

I've been able to connect to my database. manual insert data to a table by using my own inputs with the VALUES ('$name', '$email', '$userPassword')"; code but for some reason when I try to pass the above variables to my database it will input blank data into my table. this is my PHP for my welcome.php page:

<?php
  $servername = "localhost";
  $username = "root";
  $password = "@Password";
  $dbname = "accounts";

  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  // Check connection
  if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
  }



  $name = mysqli_real_escape_string($link, $_POST['Ausername']);
  $email = mysqli_real_escape_string($link, $_POST['email']);
  $userPassword = mysqli_real_escape_string($link, $_POST['Apassword']);

  $sql = "INSERT INTO users (username, email, password)
  VALUES ('$name', '$email', '$userPassword')";

  if (mysqli_query($conn, $sql)) {
      echo "New record created successfully";
  } else {
     echo "Error: " . $sql . mysqli_error($conn);
  }

  mysqli_close($conn);
?>

note that on the welcome page the below code will pass the info from my form and display it appropriately but will not add it to my database:

<html>
<body>
    Welcome <?php echo $_POST["Ausername"]; ?><br>
    Your email address is: <?php echo $_POST["email"]; ?><br>
    password is <?php echo $_POST["Apassword"]; ?>
</body>
</html> 

my form code:

<form class="form-inline" action="welcome.php" method="post">

                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-user"></i></span>
                            <input type="text" class="form-control" id="Ausername" placeholder="Username" name="Ausername" required>
                        </div>
                        <br>
                        <br>
                        <div class="input-group">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
                            <input type="text" class="form-control" id="email" placeholder="Email" name="email" required>
                        </div>
                        <br>
                        <br>
                        <div class="input-group">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                            <input type="text" class="form-control" id="Apassword" placeholder="Password" name="Apassword" required>
                        </div>
                        <br>
                        <br>                
                        <br>
                        <button type="submit" class="btn btn-primary" id="button" name="submit" value="submit">
                            <i class="fa fa-user-plus"></i>
                            Sign Up
                        </button>
                        <br>
                        <br>
                    </form>
1
  • 2
    Change $link to $conn in the escape calls Commented Sep 16, 2017 at 1:58

1 Answer 1

1

$link has not been defined in your code, your are using $conn to create your connection to the database, so you need to change

$name = mysqli_real_escape_string($link, $_POST['Ausername']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$userPassword = mysqli_real_escape_string($link, $_POST['Apassword']);

to

$name = mysqli_real_escape_string($conn, $_POST['Ausername']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$userPassword = mysqli_real_escape_string($conn, $_POST['Apassword']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.