简体   繁体   中英

Variables wont insert into table ~ mysqli

I'm posting from a form in html to PHP.

I have connect.php which successfully connects to the database:

<?php
    $db = mysqli_connect('localhost','adam','password','easyfix', '3306');
    if(mysqli_connect_errno()){
        echo "Failed to connect to MYSQL: " . mysqli_connect_error();
    }
?>

I then have register.php where I want to insert those variables from the HTML form.

<?php
    require 'connect.php';
    $username = $_POST["Username"];
    $email = $_POST["Email"];
    $password = $_POST["Password"];
    $cpassword = $_POST["Cpassword"];

    //echo $username;
    //echo $email;
    //echo $password;
    //echo $cpassword;

    mysqli_query($db,"INSERT INTO users (username, email, password)
    VALUES ('". $username ."', '". $email ."', '". $password ."')");
    or die(mysqli_error($db));

    $mysqli->close($db);
?>

I've used the echos to make sure the data was actually posted, it has. When I comment out the mysqli_query, the echos are displayed. When I leave the code as is, the echos are not displayed and I only get a white page with nothing inserted to the database.

I know I should use prepared statements, however I wish to just get the data inserted before I look into that.

I've checked out a few similar answers on here but have not found one to work. Previously my variables were not concatenated, I thought that would fix the issue but nope.

Here is the form:

<form action="../php/registration.php" method="POST" name="Login_Form" class="form-signin" >
<h3 class="form-signin-heading">Register for an account!</h3>
<hr class="colorgraph">
<br>
<h4> Username:</h4>
<input type="text" class="form-control" name="Username" placeholder="Username" required="" autofocus="" />
</br>
<h4> Email:</h4>
<input type="text" class="form-control" name="Email" placeholder="Email" required="" autofocus="" />
</br>             
<h4> Password:</h4>
<input type="password" class="form-control" name="Password" placeholder="Password" required=""/>              
</br> 
<h4> Re-enter password</h4>
           <input type="password" class="form-control" name="Cpassword" placeholder="Password" required=""/>
</br>
<div class="btn-toobar">
<button type="submit" class="btn btn-primary">Submit</button>
           </div>
</form>

password是 mysql 保留字,因此应与反引号一起使用:

INSERT INTO users (username, email, `password`) ....

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