This question already has an answer here:
How can I solve this error?
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''user','pass') VALUES('','')' at line 1
<?php
$con = mysql_connect('localhost','root','') or die ('can not connect to server'.$conn->connect_error);
if($con)
{
mysql_select_db('mydb',$con) or die ('can not seletc the database'.$conn->connect_error);
}
$error ='';
if(isset($_POST['login']))
{
$username = $_POST['username'];
$password = $_POST['password'];
mysql_query("INSERT INTO tblogin('username','password') VALUES('$username','$password')");
$id = mysql_insert_id();
if($id > 0)
{
$error .= "Register Successfully";
}
else
{
$error.="Fail To Register<br>".mysql_error();
}
}
?>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div class="row">
<div class="col-md-5"></div>
<div class="col-md-6">
<form action="" method="post">
<h2>POS Register</h2>
<h5 style="color: red;"> <?php if(isset($error)){echo $error;} ?> </h5>
<table>
<tr>
<td>
<label>User Name</label>
<input type="text" name="username" class="form-control">
</td>
</tr>
<tr>
<td>
<label>Password</label>
<input type="password" name="password" class="form-control">
</td>
</tr>
<tr>
<td>
<input type="submit" name="login" class="btn btn-primary" value="Register">
<a href="login.php">Back To Login</a>
</td>
</tr>
</table>
</form>
</div>
<div class="col-md-3"></div>
</div>
</body>
</html>
</div>
Change this,
mysql_query("INSERT INTO tblogin('username','password') VALUES('$username','$password')");
To this
mysql_query($con, "INSERT INTO tblogin(username,password) VALUES('$username','$password')");
or
mysql_query($con, "INSERT INTO tblogin(username,password) VALUES('".$username."','".$password."')");
Above will run the query but if you do like this,
$results = mysql_query($con, "INSERT INTO tblogin(username,password) VALUES('$username','$password')");
You can check if the query got executed successfully using an if ... else
like this,
it($results){
echo "Query execution done";
}else{
echo "Query execution didn't happen";
}
IMPORTAN It's better to drop using mysql
because it has been deprecated and in the new PHP versions it has been removed so it's better to pickup on mysqli_
or PDO
.
mysqli_
is the and improved version of mysql_
it's pretty similar to mysql
but has more security against SQL Injection security threat.
PDO
which stands for PHP Document Object is another interface and very good one to be used with PHP and data bases. Personally I like PDO
it's kind of universal not like mysqli_
which is manly for MySQL and for me prepared statements are much easier in PDO
.
You can do your own research and make the switch drop the use of mysql
In my concern , it's advisable to use mysqli
instead of mysql
since it's been deprecated also , you can know more about it here :- MySQL vs MySQLi when using PHP
To your problem, the answer is :
$mysql_query = "INSERT INTO tblogin(username,password) values ('$username','$password')";
$result = mysql_query($mysql_query);
To detect if it's successfully inserted :
if($result)
{
echo "Success";
}
else
{
echo "Error";
}