Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialriyanzaman
3,901 PointsProblems with sending data to database
Hi i am having some problem with sending data to the database when i send the data it does say connected buy no data are found in the database . Am new to php ... please help .
<!DOCTYPE html> <html lang="en">
<head>
<meta charset="UTF-8">
<title>Login File </title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/js/materialize.min.js"></script>
</head>
<body> <script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
<div class="container">
<div class="row">
<form action="login_process.php" method="post">
<legend><h1 style="color:red;">Form title </h1></legend>
<div class="input-field col s6">
<div class="form-group">
<span class="help-block"></span>
<input type="text" name="username" class="form-control" placeholder="Username">
</div>
</div>
<div class="input-field col s6">
<div class="form-group">
<span class="help-block"></span>
<input type="password" name="password" class="form-control" placeholder="Password">
</div>
</div>
<button type="submit" class="btn">Submit</button>
</form>
</div>
</div>
</body>
</html> the connection file :
<?php
$connection = mysqli_connect('localhost', 'root', '', 'demo');
if(!$connection) {
die("Database connection failed". mysqli_error()); } else {echo "Connected";}
?> and the process file :
<?php
require_once 'connection.php';
if($connection) {
echo "<h1>Connected</h1>";
}
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$query = "INSERT INTO user_tbl(username,password) VALUES ('$username','$password')";
$result= mysqli_query($connection,$query);
if (!$result) {
die("Query failed");
}
} $result = mysql_query("SELECT * FROM user_tbl"); echo $result;
?>
Chris Shearon
Courses Plus Student 17,330 PointsChris Shearon
Courses Plus Student 17,330 PointsI'm no expert but try adding a name or value="submit" to the button element. It's possible the
if (isset($_POST['submit']))
is returning false so the block content isn't getting executed.
<button type="submit" class="btn" value="submit">Submit</button>
I also noticed in your insert statement that the variable names are in single quotes.