0

I have installed lamp on Ubuntu and everything is working fine i tried localhost/testphp() that works fine but when I put that code it shows the values if i echo them but it doesnt insert them in the db and it doesnt give error on mysql connection either. using POST method to send form data

<form action="form.php" method="POST">

        <label>Email:</label>
        <input type="text"  name="name">
        <label>Password</label>
        <input type="password" name="pass">
      </form>

and here is the php code

<?php

ini_set('display_errors',1); 
error_reporting(E_ALL);

echo "hello I am here !<br/>";


$con=mysqli_connect("localhost","root","123","login");

if (mysqli_connect_errno($con))
    {
echo "Failed to connect to MySQL: " . mysqli_connect_error();

    }



$name = $_POST['name'];
$password = $_POST['pass'];
echo " Name: ".$name." <br/>Password: ".$password; 
mysqli_query($con,"INSERT INTO login_data (username, password) VALUES ('". $name ."', '". $password ."')");
//it doesnt insert the data 
//how do i check the error of that query ? 
mysqli_close($con);
?>  

Help !

3
  • try adding error_reporting(E_ALL); at the top so that you will get error on the page.Or exit(" here "); line by line. Commented Nov 15, 2013 at 12:14
  • You have a syntax error on the $sql declaration. Add ini_set('display_errors',1); error_reporting(E_ALL); to the top of your script to find out why. Commented Nov 15, 2013 at 12:14
  • i have added ini_set('display_errors',1); and error_reporting(E_ALL); at the top of script and still the same Commented Nov 15, 2013 at 12:17

3 Answers 3

3
$sql="INSERT INTO login_data(username,password) values(".$_POST['name'].",".$_POST['password'].")";

mysqli_query( mysqli $link , string $query)

First connection, then the query. And use mysqli

$a=mysqli_query($con, $sql );

Sign up to request clarification or add additional context in comments.

Comments

1

First of all you can enable PHP errors with your php.ini with :

display_errors = on

Or by using a .htaccess file.

Then the sql may be :

$sql = "INSERT INTO login_data(username, password) VALUES ('". $name ."', '". $password ."')";

As Don said, you use mysqli and then mysql. You have to use mysqli_query();

6 Comments

i turned on the display_errors = on and its displaying the errors now but $sql = "INSERT INTO login_data(username, password) VALUES ('". $name ."', '". $password ."')"; that doesnt work
@RanaMuhammadWaqas And did you try without ' ?. I mean: $sql = "INSERT INTO login_data(username, password) VALUES (". $name .", ". $password .")";
the username and password columns of you table login_data are strings right (varchar or something like that) ? If so you have to surround them by quotes in the query. Do you test the values of $_POST['name'] and $_POST['pass'] ?
yes i have tested the values and it gives the values fine but it doesn't insert the values using ` $sql = "INSERT INTO login_data(username, password) VALUES ('". $name ."', '". $password ."')";` and no error
As Don said, you use mysqli and then mysql. You have to use mysqli_query();
|
0

your sql statement is incorrect :

$sql="Insert into login_data(username,password) values("$name","$password")";

It should be

$sql="Insert into login_data(username,password) values("'".$name."'","'".$password."'")";

since $name and $password are predefined, you just need to pass them with proper quotes.

6 Comments

passing the values in double quotes in your statement is terminating the sql statement in incorrect manner.
when i put used the $sql="Insert into login_data(username,password) values('$name','$password')"; it gives the error hello I am here Warning: mysql_query() expects parameter 2 to be resource, object given in /var/www/form.php on line 23 [email protected] 123 Could not insert data ! but when i use this $sql="Insert into login_data(username,password) values("'".$name."'","'".$password."'")"; it gives no error and nothing inserted in table
add echo "values : ".$name." ".$password; die(); to check the values of param before insert.
it gives the values of name and password when i added echo "values : ".$name." ".$password; die();
but that doesnt add any values to the DB why how to check it ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.