-1

When I click on .deletePost I get the following errors. It seems like $mysqli is undefined, but I use it in a similar php script in the same way and it doesn't have this error so I am confused as to whats going on. Can someone please explain? Thanks!

errors:

Notice: Undefined variable: mysqli in C:\wamp\www\NightOwlSoftware\scripts\post_action.php on line 16

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\NightOwlSoftware\scripts\post_action.php on line 16

post_action.php

<?php

include 'db_connect.php';
include 'functions.php';
sec_session_start();
echo "<div>Hello World</div>";
echo "<div>Hello World</div>";
echo "<div>Hello World</div>";
echo "<div>Hello World</div>";
if($_GET['action'] == "deletePost")
        deletePost($_GET['postTitle']);
function deletePost($title){
    $sql = "DELETE FROM blog WHERE Title = '$title'";
    mysqli_query($mysqli, $sql);
}
?>

functions.php

<?php
function sec_session_start() {
    $session_name = 'sec_session_id'; // Set a custom session name
    $secure = false; // Set to true if using https.
    $httponly = true; // This stops javascript being able to access the session id. 

    ini_set('session.use_only_cookies', 1); // Forces sessions to only use cookies. 
    $cookieParams = session_get_cookie_params(); // Gets current cookies params.
    session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly); 
    session_name($session_name); // Sets the session name to the one set above.
    session_start(); // Start the php session
    session_regenerate_id(); // regenerated the session, delete the old one.  
}
?>

dbconnect.php

<?php
$host="localhost"; // Host name
$username="root"; // username
$password="********"; // password
$dbname="nightowl"; // Database name
$tblname="blog"; // Table name
$mysqli=mysqli_connect($host,$username,$password,$dbname);
mysql_connect("$host", "$username", "$password");
mysql_select_db("$dbname");
?>

Javascript

$(document).ready(function(){
$('.deletePost').click(function(){
    $.ajax({
        url:"scripts/post_action.php",
        data: {action: "deletePost",  postTitle: $(this).siblings("h3.blog").text()},
        success: function(response){
            $("body").html(response);
            alert('DELETED SUCCESSFULLY');
            }
    });
});
});
rynhe
  • 2,509
  • 1
  • 21
  • 27
CodeManiak
  • 1,903
  • 4
  • 19
  • 32
  • 2
    Btw, where is this `$mysqli` variable defined in your post_action.php script ? – dreamweiver Nov 28 '13 at 07:48
  • Well I include dbconnect and functions.php and I use sec_session_start() which I thought defined it? – CodeManiak Nov 28 '13 at 07:50
  • possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – deceze Nov 28 '13 at 07:52
  • The first parameter of `mysqli_query()` should be a link identifier, returned from mysqli_connect() or mysqli_init(). ref:http://us1.php.net/mysqli_query – dreamweiver Nov 28 '13 at 07:52

2 Answers2

3

That is because you're calling $mysqli inside a scope where it's not defined as $mysqli is not global. You will have to pass it as an argument of deletePost function. For example:

function deletePost($title, $mysqli){
    $sql = "DELETE FROM blog WHERE Title = '$title'";
    mysqli_query($mysqli, $sql);
}
Eduardo Casas
  • 578
  • 4
  • 11
  • Thank you for answering my question instead of down voting me... I'm obviously trying to understand and you can't do research on something when you're not sure of the question. – CodeManiak Nov 28 '13 at 07:55
  • `$mysqli` *is* global, that's the problem. You mean to say it's not *superglobal*. :) – deceze Nov 28 '13 at 08:02
2

You don't need mysql_select_db() and mysql_connect() because you already declared it.

Oki Erie Rinaldi
  • 1,835
  • 1
  • 22
  • 31