0

Apparently my POST requests are being cancelled? http://puu.sh/d73LC/c6062c8c07.png

and also, mysqli_result object has all null values when i query the database with a select query:

object(mysqli_result)[2]
  public 'current_field' => null
  public 'field_count' => null
  public 'lengths' => null
  public 'num_rows' => null
  public 'type' => null

here is my php file:

<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "uoitlol";
$name = "test1"; //this should be $_POST['name']; test1 is just to test if it works.
$err = false;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_errno > 0) {
    echo 'connerr';
    die();
}
$sql = "INSERT INTO summoners (name) VALUES (?)";

$getname = "SELECT name FROM summoners";

$result = $conn->query($getname);
while ($row = $result->fetch_assoc()) {
    echo 'name : ' . $row['name'];
    if ($row['name'] === $name) {
        echo 'error, name exists';
        $err = true;
    }
}

$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $name);

if ($err === false) {
    if (!$stmt->execute()) {
        echo 'sqlerr';
    } else {
        echo 'success';
    }
}

$stmt->close();
mysqli_close($conn);

here is my javascript file, which calls the php file with ajax whenever i click submit on my form (in a different html file)

$(document).ready(function () {
    $("#modalClose").click(function () {
        document.getElementById("signupInfo").className = "";
        document.getElementById("signupInfo").innerHTML = "";
    });
    $("#formSubmit").click(function () {
        var name = $("#name").val();
// Returns successful data submission message when the entered information is stored in database.
        var dataString = {'name' :name};
        if (name === '')
        {
            document.getElementById("signupInfo").className = "alert alert-danger";
            document.getElementById("signupInfo").innerHTML = "<b>Please enter a summoner name!</b>";
        }
        else
        {
// AJAX Code To Submit Form.
            $.ajax({
                type: "POST",
                url: "submitName.php",
                data: dataString,
                cache: false,
                success: function (msg) {
                    if (msg === 'error'){
                        document.getElementById("signupInfo").className = "alert alert-danger";
                        document.getElementById("signupInfo").innerHTML = "<b>That summoner name is already in the database!</b>";
                    } else if (msg === 'sqlerror'){
                        document.getElementById("signupInfo").className = "alert alert-danger";
                        document.getElementById("signupInfo").innerHTML = "<b>SQL error, contact the administrator.</b>";
                    } else if (msg === 'success'){
                        document.getElementById("signupInfo").className = "alert alert-success";
                        document.getElementById("signupInfo").innerHTML = "<b>Summoner successfully added!</b>";
                    }
                }
            });
        }
        return false;
    });
});

I'm getting these errors everytime I click my button that submits my form:

Failed to load resource: Unexpected end of file from server (19:41:35:538 | error, network) at public_html/submitName.php

Failed to load resource: Unexpected end of file from server (19:41:35:723 | error, network) at public_html/submitName.php

Failed to load resource: Unexpected end of file from server (19:41:36:062 | error, network) at public_html/submitName.php

I'm using Netbeans IDE, if that matters.

puu.sh/d6YXP/05b5f3dc06.png - screenshot of the IDE, with the output log errors.

2 Answers2

1

Remove this from your submitName.php, unless there really is HTML in it.

<!DOCTYPE html>

If there is HTML in it, do this instead.

<?php
//your PHP code//
?>
<!DOCTYPE html>
//your HTML here//
</html>

Also, if submitName.php contains no HTML, make sure there is no blank line after ?> at the bottom.

EDIT: In regards to your query failing, try this code.

if (!empty($name) { //verify the form value was received before running query//
    $getname = "SELECT name FROM summoners WHERE name = $name";

    $result = $conn->query($getname);
    $count = $getname->num_rows; //verify a record was selected//
    if ($count != 0) {
        while ($row = $result->fetch_assoc()) {
            echo 'name : ' . $row['name'];
            if ($row['name'] === $name) {
                echo 'error, name exists';
                $err = true;
            }
        }
    } else {
        echo "no record found for name";
        exit;
    }
}
EternalHour
  • 8,308
  • 6
  • 38
  • 57
  • Thank you, but this did not fix the issue. – user4298098 Nov 27 '14 at 02:19
  • I've gone through the files you've provided thoroughly and there is nothing wrong with them. Either you are missing a `}`,`;` or there is space before/after your php tags. You may also want to try reloading netbeans. – EternalHour Nov 27 '14 at 02:58
  • I got this from my WAMP server log file: 27-Nov-2014 04:12:21 Europe/Paris] PHP Fatal error: Call to a member function fetch_assoc() on a non-object in C:\Program Files (x86)\wamp\www\WSFinal\public_html\submitName.php on line 27 [27-Nov-2014 04:12:21 Europe/Paris] PHP Stack trace: [27-Nov-2014 04:12:21 Europe/Paris] PHP 1. {main}() C:\Program Files (x86)\wamp\www\WSFinal\public_html\submitName.php:0 – user4298098 Nov 27 '14 at 03:13
  • So `$getname = "SELECT name FROM summoners";` is not returning anything. – EternalHour Nov 27 '14 at 03:16
  • I guess so... but the query is returning a mysqli_result object. However, every field in the object is NULL. I am quite confused. – user4298098 Nov 27 '14 at 03:18
0

Drop the ?> at the end of the php file and instead of using var dataString = 'name=' + name; use this instead:

var data = { "name" : name};

jQuery will automagically do the dirty stuff for you so that you don't have to special text-escape it and stuff.

That's as far as I can help without any log files and just a quick skim of your code.

Philll_t
  • 4,267
  • 5
  • 45
  • 59
  • Thank you for the reply! I made the changes as suggested, and I am still getting errors. These are the errors I got from the Output Log: Failed to load resource: Software caused connection abort: recv failed (20:55:45:109 | error, network) at public_html/submitName.php Failed to load resource: Unexpected end of file from server (20:55:52:199 | error, network) at public_html/submitName.php – user4298098 Nov 27 '14 at 01:56
  • Okay cool. Can you go ahead and update your code please on the question. Thanks! – Philll_t Nov 27 '14 at 01:58
  • I am actually using ajax to execute the php file, so that the browser does not switch pages. I can load the page, but whenever I submit the form I get the unexpected eof error, or i get the Software cause connection abort, recv failed error. – user4298098 Nov 27 '14 at 02:03
  • Okay, that's cool. But you should still be able to load the page on its own to see the output. so load `"submitName.php"` on its own, then using the `die();` method, step through the file until you find the error. Also, if you are not seeing errors add this to the beginning of your PHP code `ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1);` – Philll_t Nov 27 '14 at 02:05
  • Ok, I ran the PHP file and changed it a bit (updated version is edited in the post). It runs fine by itself, it updates the table and will not add a name if it already exists. – user4298098 Nov 27 '14 at 02:20
  • however when I try to run it using the form button, it gives me EOF errors. – user4298098 Nov 27 '14 at 02:21
  • I did a var_dump on $result, and what came bac kwas interesting. It was a mysqli result object, however every single field was NULL. What is happening? – user4298098 Nov 27 '14 at 03:14