Need Help with my php mysql code

Hi ,

I have changed by php code to mysqli commands. But still getting error while login as admin in localhost//project1/admin. Below is the index.php code. Please help or suggest the better code. Thanks in advance.

## error - Fatal error: Call to a member function real_escape_string() on a non-object in C:\xampp\htdocs\elearning\admin\index.php on line 120

<?php
include('header.php');
//Start session
session_start();
//Unset the variables stored in session
unset($_SESSION['id']);
?>
<body>

    <div class="row-fluid">
        <div class="span12">


            <div class="navbar navbar-fixed-top navbar-inverse">
                <div class="navbar-inner">
                    <div class="container">

                        <!-- .btn-navbar is used as the toggle for collapsed navbar content -->
                        <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </a>

                        <!-- Be sure to leave the brand out there if you want it shown -->


                        <!-- Everything you want hidden at 940px or less, place within here -->



                        <div class="nav-collapse collapse">
                            <!-- .nav, .navbar-search, .navbar-form, etc -->


                        </div>

                    </div>
                </div>
            </div>

            <div class="hero-unit-header">
                <div class="container">
                    <div class="row-fluid">
                        <div class="span12">


                            <div class="row-fluid">
                                <div class="span6">
                                    <img src="images/head.png">
                                </div>
                                <div class="span6">

                                    <div class="pull-right">
                                        <i class="icon-calendar icon-large"></i>
                                        <?php
                                        $Today = date('y:m:d');
                                        $new = date('l, F d, Y', strtotime($Today));
                                        echo $new;
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>

            <div class="container">
                <div class="row-fluid">
                       <div class="span6">
                    <div class="hero-unit-3">
                        <div class="alert alert-info">What We offer</div>
                        <p>
                            At Qapundit.com you will get to know " All about Qa and Testing Field" also get connected to the industry professionals for a piece of advice. 

                        </p>
                    </div>
                    <div class="hero-unit-3">
                        <div class="alert alert-info">Courses</div>
                        <p>
                             Manual Testing,Automation Testing
                              
                        </p>
                    </div>
                </div>
                    <div class="span6">

                        <div class="alert alert-info"><strong>Login</strong> Administrator login Only!!! </div>
                        <!-- login -->
                        <form class="form-horizontal" method="post">
                            <div class="control-group">
                                <label class="control-label" for="inputEmail">Username</label>
                                <div class="controls">
                                    <input type="text" name="username" id="inputEmail" placeholder="Username" required>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="inputPassword">Password</label>
                                <div class="controls">
                                    <input type="password" name="password" id="inputPassword" placeholder="Password" required>
                                </div>
                            </div>


                            <div class="control-group">
                                <div class="controls">
                                    <button type="submit" name="login" class="btn btn-info"><i class="icon-signin icon-large"></i>&nbsp;Sign in</button>
                                </div>
<br>
                                <?php
                                if (isset($_POST['login'])) {

                                    function clean($str) {
                                        $str = @trim($str);
                                        if (get_magic_quotes_gpc()) {
                                            $str = stripslashes($str);
                                        }
                                        return $mysqli->real_escape_string($str);
                                    }

                                    $username = clean($_POST['username']);
                                    $password = clean($_POST['password']);

                                    $query = $mysqli->query("select * from user where username='$username' and password='$password'") or die($mysqli->error);
                                    $count = $query->num_rows;
                                    $row = $query->fetch_assoc;


                                    if ($count > 0) {
                                        session_start();
                                        session_regenerate_id();
                                        $_SESSION['id'] = $row['user_id'];
                                        header('location:home.php');
                                        session_write_close();
                                        exit();
                                    } else {
                                        session_write_close();
                                        ?>

                                     
                                        <div class="alert alert-danger"><i class="icon-remove-sign"></i>&nbsp;Access Denied</div>

                                        <?php
                                      
                                    }
                                }
                                ?>
                            </div>
                

                    </form>
                </div>
                   <?php include('footer_fixed.php'); ?>
            </div>
      
        </div>
    </div>
</div>

</body>
</html>

Check out variable scope. The object $mysqli does not exist within your function unless you either pass it in as a parameter, or define it as a global.

Maybe this can help : http://php.net/manual/en/mysqli.real-escape-string.php

I don’t know how you activated mysqli so thats why I am referring to the documentation.

error - Fatal error: Call to a member function real_escape_string() on a non-object in C:\xampp\htdocs\elearning\teacher_modal.php on line .

Can anyone help me with the correct code

        <?php
        if (isset($_POST['login_teacher'])) {

            function clean($str) {
                $str = @trim($str);
                if (get_magic_quotes_gpc()) {
                    $str = stripslashes($str);
                }
                return mysql_real_escape_string($str);
            }

            $username = clean($_POST['username']);
            $password = clean($_POST['password']);

            $query = mysql_query("select * from teacher where username='$username' and password='$password'") or die(mysql_error());
            $count = mysql_num_rows($query);
            $row = mysql_fetch_array($query);


            if ($count > 0) {
                session_start();
                session_regenerate_id();
                $_SESSION['id'] = $row['teacher_id'];
                header('location:teacher_home.php');
                session_write_close();
                exit();
            } else {
                session_write_close();
                ?>
                <div class="pull-right">   
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <div class="alert alert-danger"><i class="icon-remove-sign"></i>&nbsp;Access Denied</div>
                </div>
                <?php
                exit();
            }
        }
        ?>

For a start you really shouldn’t e using the mysql_* functions any more. These have been replaced with either mysqli_* functions or better still PDO. The mysql_* functions don’t even exist in the latest version of PHP.

Hi,

please can you provide an example for this

There is a good tutorial in PDO at https://phpdelusions.net/pdo

I thought you’d already rewritten it to use mysqli as you described in this thread: Need Help with my php mysql code

I just merged the two open topics together, I apologize if that makes some of the conversation seem duplicated. But figured discussing this in one spot will make it easier for all.

First of all, if you want this snippet to work, you MUST listen to what people in this topic are telling you. If you ignore them and all you just want is to copy&paste code people do for you, you won’t learn a thing and you won’t get nothing working.

Now, I suggest reading up on basic PHP functions like session_start() because you are placing it in the wrong area. session_start() should always come before any rendered HTML. I will assume that your header.php file contains HTML elements so you are already doing it wrong.

Next, I highly suggest you stop using legacy codes and start using up-to-date that aren’t copied&pasted from tutorials that are from 1990’s.

Before you do anything next, I highly highly highly suggest you read up on basic PHP because it seems to me that you just jumped into using PHP without learning any basics of PHP.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.