-1

I've created a simple MySQL connection to insert data. The connection is fine, but when I submit the form data, the INSERT is not working. But if I put manual data into the query, it works fine.

<form action="dbconnection.php" method="POST">
    <label>Username</label>
    <input type="text" name="username" placeholder="Username" required=""/>
<label>Password</label>
<input type="text" name="password" required="" placeholder="Password"/>
    <input type="submit" value="save"/>
</form>

<?php
print_r($_POST);
$server = "localhost";
$dbname="mytestdb";
$username = "root";
$password= "";

//create connection
$conn = mysqli_connect($server,$username,$password,$dbname);


//check connection

if($conn->connect_error){
    die("connection failed". $conn->connect_error);
}

echo "connected successfull";

$username = $_POST['username'];
$password = $_POST['password'];

$sql = "INSERT INTO user('username','password') VALUES ($username,$password)";

mysqli_query($conn,$sql);
Pang
  • 9,564
  • 146
  • 81
  • 122
KBK
  • 375
  • 1
  • 4
  • 20
  • 2
    How does it fail? Check for any errors returned from the database, it could very well be telling you what the problem is. Note also that your code is *wide open* to **SQL injection attacks**. So technically that query could be doing *anything* that your users want it to do. – David Jun 12 '15 at 18:17
  • 1
    What do you think code isn't working? Do you getting any error messages. – Dhanuka Jun 12 '15 at 18:17
  • I got it.... thks every one query shouldbe $sql = "INSERT INTO user(username,password) VALUES ('".$username."','".$password."')"; – KBK Jun 12 '15 at 18:20

1 Answers1

0

You are using single quotes around your column names. Change the ' to `

Brett
  • 1,951
  • 2
  • 28
  • 35