0

As the title says I'm trying to check via jquery validation plugin if value for Patch_No exists. I read through many other similar topics and that's how I figured the code but it seems not to be working although there are no errors. I'm pretty sure DBconnection.php works hence I'm using it in another file to input data.

<?php
include 'DBconnection.php';
$patch = $_REQUEST['patch'];
$sql="SELECT * FROM champions WHERE Patch_No ='$patch'";
$conn->query($sql);
$num = mysql_num_rows($sql);
if($num>0){
    echo 'false';
}
else{
    echo 'true';
}
?>

Here is also the jquery validation code

<script>
    $(document).ready(function(){
        $('#first_form').validate({
            rules: {
                patch: {
                    required: true,
                    minlength: 4,
                    remote: {
                        url: "checkpatch.php",
                        type: "post"
                        }
                    }
                },
            messages: {
                patch: {
                    required: "Please enter patch version.",
                    remote: "This Patch already exists."
                    }

                }
        });
    });
</script>

And last but not least the actual form in php

echo '<form id="first_form" action="index.php" method="POST" style="border: 0; margin: 0;">';
                echo '<input type="hidden" value="'.$formid.'" name="formid">';
                echo '<input type="text" name="patch" placeholder="PATCH e.g. 4.20" required autofocus><br/>';
                echo '<input placeholder="Number of Champions" value="1" type="number" name="champ_number" min="1" max="99" required><br/>';
                echo '<input type="submit" value="next">';
            echo '</form>';

EDIT DBconnection.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "patches";

$conn = new mysqli($servername, $username, $password, $db);
if($conn->connect_error){
    die("Connection failed: ". $conn->connect_error);
}
?>
6
  • BTW, your code is insecure. See PHP's mysqli_/PDO APIs, and prepared statements! Commented Dec 23, 2014 at 12:53
  • I'm learning mysql and php right now so after I'm done with functionality I'll move to security Commented Dec 23, 2014 at 12:57
  • Strongly suggest you reverse your priorities!! Commented Dec 23, 2014 at 12:59
  • change the code to if($num>0){ echo json_encode(false); } else{ echo json_encode(true); } and see if this makes any difference. Commented Dec 23, 2014 at 13:03
  • You have a mysqli connection and using mysql function. Change the mysql functions to mysqli compatible one. Commented Dec 23, 2014 at 13:10

2 Answers 2

1

try like this:

$sql="SELECT * FROM champions WHERE Patch_No ='".$patch."'";
$result = $conn->query($sql);
$num = $result->num_rows;
Sign up to request clarification or add additional context in comments.

4 Comments

for some reason it worked could you explain it to me?
read this:php.net/manual/en/mysqli-result.num-rows.php even if you are not getting let me know.
and avoid using mysql use PDO or mysqli because mysql is deprecated and you will be getting warnings if you will enable error_reporting().
@Suchitkumar, Can you help me in this link stackoverflow.com/questions/46536181/…
0

These lines are surly bad. You want to fetch a string... Use this:

$sql="SELECT * FROM champions WHERE Patch_No ='$patch'";
$conn->query($sql);
$num = mysql_num_rows($conn);

You need to fetch your $conn, that is the resource, not $sql.

NOTES

  • Do not use mysql functions, they are deprecated.

  • Avoid sql injections by escaping your variables in your query, or use prepared statements.

2 Comments

I changed source to $conn nothing happens still unfortunately
Edit your question, and show me, whats in DBconnection.php

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.