Having problem with short script, basic PHP blog

Hi there,

I am pretty new to PHP and have been attempting to develop a basic blog as my first php project.
This video is what I am learning from:

At 25:35 in the video is where I am having problems. As far as I can tell, my code matches that of the video author, however I have an error and he doesn’t.

Here is my code that has the issue:

[code][php]

<?php session_start(); ini_set('error_reporting', E_ALL); if(isset($_POST['submit'])){ // $user = $_POST['username']; $pwrd = $_POST['pwrd']; //include database connnectinon include('../includes/db_connection.php'); if(empty($user) || empty($pwrd)){ echo "missing information"; }else{ //prevents javascript injections //prevents sql injections $user = strip_tags($user); $user = $db->real_escape_string($user); $pwrd = strip_tags($pwrd); $pwrd = $db->real_escape_string($pwrd); $pwrd = md5($pwrd); // this is going to grab the username from the user table, where username = user and password = pwrd $query = $db->query("SELECT user_id, username FROM user WHERE username='$user' AND password='$pwrd'"); echo $query->num_rows; } } ?> Untitled Document

Username

Password
[/php][/code]

I was not having trouble until I wrote this code here (its in the above example, just drawing attention to the problematic bit). Do you see any problems with the below code?

[php]
else{
//prevents javascript injections //prevents sql injections
$user = strip_tags($user);
$user = $db->real_escape_string($user);
$pwrd = strip_tags($pwrd);
$pwrd = $db->real_escape_string($pwrd);
$pwrd = md5($pwrd);
// this is going to grab the username from the user table, where username = user and password = pwrd
$query = $db->query(“SELECT user_id, username FROM user WHERE username=’$user’ AND password=’$pwrd’”);
echo $query->num_rows;

 }[/php]

Thanks for any help.

Could it be a problem of using ’ instead of ` in my sql query?

(As mentioned here:
http://betterphp.co.uk/board/viewtopic.php?f=7&t=2164 )

Another question:

How do I get this script to tell me the error?
Basically, right now, when I enter the credentials for the only user in the database:
user: admin
pw: admin

I get this:
“HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.”

Versus, what I want to happen, is for the browser to tell me what errors are occurring on what line. Isn’t that what this code " ini_set(‘error_reporting’, E_ALL); " is supposed to do?

Okay, what’s the version of your PHP? I don’t know exactly but it can be problem with variable global…

So, try still…

$user = $_POST[‘username’];
$pwrd = $_POST[‘pwrd’];
//include database connnectinon
include(’…/includes/db_connection.php’);
$db = new mysqli(yourhost, youruser, yourpass, yourdatabase);

Just try…

Sponsor our Newsletter | Privacy Policy | Terms of Service