-1

I am running a PHP/MySQL insert query using the following code below. I am getting no error, and the query goes off with success. However the database is not updated. I have tested the SQL inside phpAdmin and it does work. I am using binding to add my variables to the SQL query. The only thing I can think of is that the bindings are not working. Is there any way to get the error logs. I cannot seem to find them on XAMPP or online, unless I am terrible at searching for them. Or perhaps someone can see the error in my code below.

<?php

$conn = mysqli_connect("localhost","root","M0nkwork", "blogs");
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
else{

    $msg = '';
    $msgClass = '';

    if(filter_has_var(INPUT_POST, 'submit')){

            //Send PHP to database
        $SQL = "INSERT INTO Blogs(UserID, Title, Author, Blog, Date) VALUES (?,?,?,?,?)";
        $stmt = $conn->prepare($SQL);
        $stmt->bind_param("issss", $UserID, $Title, $Author, $Blog, $Date);

        $Author = $_POST['Author'];
        $Title = $_POST['Title'];
        $BlogEntry = $_POST['BlogEntry'];
        $Date = $date = date('m/d/Y h:i:s a', time());
        $UserID = 1;

        if(!empty($Author) && !empty($Title) && !empty($BlogEntry)){       
            $stmt->execute();
            $stmt->close();

            $msg = 'Success';
            $msgClass = 'alert-success';
        }else{
            $msg = 'Please Fill in all fields';
            $msgClass = 'alert-danger';
        }
    }
}
?>
DaveK
  • 544
  • 1
  • 6
  • 16
  • https://prnt.sc/pstqv4, even better like this https://prnt.sc/pstryi – Zeljka Nov 05 '19 at 16:08
  • 3
    _"I am getting no error."_ That's because you're not checking for any. `prepare()`, `bind_param()`, `execute()` -- they can all fail and return `FALSE`. You need to check for that possibility. – Alex Howansky Nov 05 '19 at 16:11
  • 2
    If your date field is `date` and not `varchar`, then the proper format is `Y-m-d H:i:s`. Anything else can cause issues. – aynber Nov 05 '19 at 16:12
  • Unfortunately that didn't change anything. That is moving the binding function down after the definitions. I'll check into the other things. – DaveK Nov 05 '19 at 16:12
  • In addition, if `UserID` is an AUTO_INCREMENTed column, your query failed. You need to enable php's error reporting and `mysqli_error($conn)` on the query. Try that and post what the errors were, if any. @DaveK – Funk Forty Niner Nov 05 '19 at 16:21
  • Unfortunately none of these ideas got me further. I have tried moving the bind_param to after the variable definitions. I have reformatted the date to be varchar, and have adjusted the date format, and have try catches around all the methods that could fail. It still says "success" with no actual insert. – DaveK Nov 05 '19 at 16:23
  • 1
    try/catch won't catch mysqli errors. You need to explicitly check for them with [mysqli errors](http://php.net/manual/en/mysqli.error.php) – aynber Nov 05 '19 at 16:23
  • Have you also implemented [$stmt->error()](https://www.php.net/manual/en/mysqli-stmt.error.php) in the correct way/place (after your `$stmt->execute()` but before your `$stmt->close()`)? – M. Eriksson Nov 05 '19 at 16:25
  • 1
    Echoing @AlexHowansky ... either check for the return from the mysqli function calls, or enable PHP exception reporting ... `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` https://www.php.net/manual/en/mysqli-driver.report-mode.php otherwise, mysqli::execute will return FALSE if an error occurs. We need to test the return from the funciton, to identify if an error occurred. (How exactly did you determine that the statement execution didn't insert a row? Are you sure you are querying the same table, in the same database?) – spencer7593 Nov 05 '19 at 16:28
  • ...which I said, [no?](https://stackoverflow.com/questions/58715215/mysql-and-php-insert-is-not-inserting-into-database#comment103724195_58715215). – Funk Forty Niner Nov 05 '19 at 16:30
  • ^^ Still working on getting the error logs. – DaveK Nov 05 '19 at 16:33

1 Answers1

2

Thank you one and all for the responses and comments. I found the error and I will post the error here however I wanted to mark this thread as solved as a means for others to get the error logs posted. I believe adding these lines to the beginning of the php code gave me the errors I needed. In the end it turned out to be a disconnect.

If you noticed $BlogEntry is the variable I store my blog body. However $Blog is what I reference in my $SQL query.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
DaveK
  • 544
  • 1
  • 6
  • 16
  • **^^^ THIS ^^^** +10 ... figure out *what* the problem is, before we muck with "try this" and "try that" and "maybe something else" non-solutions, when we don't know what the actual problem is. (I'm not sure why someone downvoted this answer, because this *is* the right answer. And on SO, there's no shortage of "try this" guesses, from the fastest guns in the west, to send you chasing down rabbit holes, making changes that have not one iota to do with the actual problem.) – spencer7593 Nov 05 '19 at 17:29
  • It was basically the question I had asked. I was wondering what I could do to display error logs as I knew it was an SQL error, but as I just picked this up 2 days ago, I was having issues finding this solution online. Thanks for the shout-out. Also I find this place very unapproachable sometimes and it's just my personal opinion that I wish this place wasn't unapproachable. Just my two cents. – DaveK Nov 05 '19 at 17:49
  • I can't explain why those four lines of code (posted in this answer) aren't in *every* mysqli_ example online, much less, can't be found in *any* mysqli_ example online... – spencer7593 Nov 05 '19 at 17:59
  • 1
    @DaveK Stack Overflow can take some getting used to. Try not to interpret brevity or frustration as hostility. You're one of thousands of people who made this sort of mistake and it can be a challenge to deal with yet another instance of something and remain positive. Too many PHP developers come in completely unprepared for development work, don't know where their error logs are, haven't set up their database connection correctly, etc. This is not your fault, it's a failing of the PHP documentation and community best-practices. Hope this exchange helped on the whole. – tadman Nov 05 '19 at 18:08