1

This is what I get when I attempt to use my log in form. My PHP, HTML, and error is listed below

PHP:

<?php
$con=mysqli_connect("h","h","h","g");
$sql = "SELECT username, password FROM users WHERE username=? AND  pwd=? LIMIT 1";
$stmt = $con->prepare($sql);
$stmt->bind_param('ss', $_POST["username"], $_POST["pwd"]);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt->execute();
$stmt->bind_result($username, $password);
$stmt->store_result();
if($stmt->num_rows == 1)
{
    echo "you are logged in";
}

HTML:

<form action="login.php" method="post">
  <input type="text" name="username"required>
  <p>
    <input type="password" name="pwd"required>
  <p>
    <input type="submit" name="Sign Up" value="Log In">
</form>

Error:

Fatal error: Call to a member function bind_param() on a non-object in /home/u378761662/public_html/login/login.php on line 5
m59
  • 43,214
  • 14
  • 119
  • 136
  • Your prepare() call is failing. See [How to squeeze error message out of PDO?](http://stackoverflow.com/q/3726505) – Pekka Dec 15 '13 at 02:21
  • 3
    Consider removing your credentials (Database User/Pass). – Mohammad Tomaraei Dec 15 '13 at 02:22
  • This is minor, but you should use the `mysqli` constructor instead of the `mysqli_connect` wrapper: `$con = new mysqli("h", "h", "h", "g");`. – Kevin Ji Dec 15 '13 at 02:23
  • @mc10 no he does not, it is just an alias. – Mustafa Dec 15 '13 at 02:25
  • @Class So just change it to 'pwd != password'? –  Dec 15 '13 at 02:26
  • @Mustafa "Should", not "must". The constructor just makes it a bit clearer that you are left with a `mysqli` object. – Kevin Ji Dec 15 '13 at 02:26
  • @Class So just change the stuff on line 3? –  Dec 15 '13 at 02:29
  • you have `SELECT password` then you use `pwd=?` they should be the same name. – Class Dec 15 '13 at 02:32
  • @Class I just updated it and tried it out, but still get the same error –  Dec 15 '13 at 02:36
  • @Pekka웃 Tried that an the issue wasn't resolved –  Dec 15 '13 at 03:32
  • Your prepare() call is failing and the question linked above should be getting an error message telling you what went wrong. – Pekka Dec 15 '13 at 03:34
  • @Pekka웃 I inserted the code and i'm not getting any error message other then the one I already have –  Dec 15 '13 at 03:41
  • did you set `$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);`? – Pekka Dec 15 '13 at 03:42
  • Can you edit the full code you have now into the question? – Pekka Dec 15 '13 at 03:45
  • @Pekka웃 The code above is still the code in question –  Dec 15 '13 at 03:48
  • So you didn't change anything at all? – Pekka Dec 15 '13 at 03:51
  • @Pekka웃 I added the code to retrieve the error message on my file and removed it since it wasn't a help. Is there any way you could edit the code to help me out fix the issue? –  Dec 15 '13 at 03:52
  • You will need to know what goes wrong in order to be able to fix whatever the issue is, so put the error handling code back to see whether there's something wrong with it. – Pekka Dec 15 '13 at 03:55
  • @Pekka웃 I put it back in, Now what? –  Dec 15 '13 at 03:56
  • Oh, you are using mysqli. That's the wrong code for that, sorry. Try `echo $con->error` after the line where you call `$con->prepare();` – Pekka Dec 15 '13 at 03:59
  • @Pekka웃 This is what I get now **Parse error: syntax error, unexpected '$stmt' (T_VARIABLE), expecting ',' or ';' in /home/u378761662/public_html/login/login.php on line 6** –  Dec 15 '13 at 04:03
  • You need to add a semicolon at the end of each command – Pekka Dec 15 '13 at 04:04
  • @Pekka웃 So like this? **$stmt->bind_param('ss', $_POST["username"]; $_POST["pwd"]);** –  Dec 15 '13 at 04:06
  • No after the `echo $con->error` – Pekka Dec 15 '13 at 04:07
  • @Pekka웃 I now get this above the prior error. My table does exist btw **Table 'u378761662_user.users' doesn't exist** –  Dec 15 '13 at 04:11
  • There's your problem then. – Pekka Dec 15 '13 at 04:13
  • My table is called "user" and I entered it as "user" but it gives me the **user.users** In the error code –  Dec 15 '13 at 04:14
  • 1
    You are saying `SELECT username, password FROM users ` – Pekka Dec 15 '13 at 06:39

1 Answers1

0

Looks like an exception is thrown on login.php at the line no: 5.

Add echo $con->error; before $stmt->bind_param('ss', $_POST["username"], $_POST["pwd"]); and see if it throws an error.

Hope this helps.

Rakesh Sankar
  • 9,337
  • 4
  • 41
  • 66