0

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

Error screenshot

<?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>
Pang
  • 9,564
  • 146
  • 81
  • 122
Student
  • 141
  • 9

2 Answers2

1

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";

}
FreedomPride
  • 1,098
  • 1
  • 7
  • 30
0

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

S4NDM4N
  • 904
  • 2
  • 11
  • 26
  • Thanks , for help me out but it still giving the same error ! – Student Sep 19 '17 at 02:48
  • I updated my answer it had an error. – S4NDM4N Sep 19 '17 at 02:53
  • sand, both wil not working , but fredompride answer is working, will you explain this please , why its not working ? – Student Sep 19 '17 at 03:08
  • It's not working because your query is not getting executed look at your `mysql_query` and the answer from fredompride. In his answer his binding the SQL to a variable then calling it using the `mysql_query`. I also over looked it but I'll correct it. Also you should up vote and mark the answer as selected to show that answer solved your problem. – S4NDM4N Sep 19 '17 at 03:14
  • ah I think you marked the wrong answer as the selected :P. – S4NDM4N Sep 19 '17 at 03:22
  • mysql_query ("INSERT INTO tblogin(username,password) values ('$username','$password')"); the working one is that... thanks a lot to help me out! so edit your answer in that way – Student Sep 19 '17 at 03:22
  • oh ok I'll edit it – S4NDM4N Sep 19 '17 at 03:23
  • Hey , Will you give me a one favour please :( – Student Nov 03 '17 at 05:57