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.

7
  • 1
    Please post your code here, not as pastebin links. Commented Nov 27, 2014 at 0:58
  • I can't post my code here, it gets all garbled. Commented Nov 27, 2014 at 1:01
  • Use the code markup tool in the editor: {} Commented Nov 27, 2014 at 1:03
  • I was using it, but it still got garbled, I managed to fix it, my bad! Commented Nov 27, 2014 at 1:04
  • Are you getting any server-side errors? Commented Nov 27, 2014 at 1:07

2 Answers 2

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;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, but this did not fix the issue.
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.
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
So $getname = "SELECT name FROM summoners"; is not returning anything.
I guess so... but the query is returning a mysqli_result object. However, every field in the object is NULL. I am quite confused.
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.

7 Comments

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
Okay cool. Can you go ahead and update your code please on the question. Thanks!
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.
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);
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.