0

I cannot find out what's wrong in this code. Since the query executes and insert also works but the error msg shows:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given on line no 22. I cannot solve this.

<?php

require_once('db_config.php');
if($_SERVER['REQUEST_METHOD']=='POST')
{
    $name = $_POST['Name'];
    $mob = $_POST['Mob'];
    $username = $_POST['Username'];
    $password = $_POST['Password'];

    $sql = "INSERT INTO user (name,phone,username,password) VALUES ('$name','$mob','$username','$password')";

    $result = mysqli_query($con,$sql) or die("Error: ".mysqli_error($con));

    $check = mysqli_fetch_array($result);

    if(isset($check)){
        echo "sucess";
    }else{
        echo "error";
    }
    mysqli_close($con);
}

?>
mymiracl
  • 583
  • 1
  • 16
  • 24
suresh
  • 87
  • 1
  • 10
  • `name` is a reserved keyword in mysql so if you use it as a field name you need to use backticks https://dev.mysql.com/doc/refman/5.5/en/keywords.html. You're also vulnerable to injection attack – Tristan Dec 27 '15 at 03:03
  • 1
    Does this answer your question? http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole – devlin carnate Dec 27 '15 at 03:03
  • also read: http://stackoverflow.com/questions/601300/what-is-sql-injection – Sam Segers Dec 27 '15 at 03:03
  • @Tristan `name` is not a reserved word, it is a keyword (2 different animals). As per the manual, there is no `(R)` next to it. – Funk Forty Niner Dec 27 '15 at 03:04
  • put this after mysqli_query() to see whats going on. if (!$result) { printf("Error: %s\n", mysqli_error($con)); exit(); } – devterm Apr 22 '18 at 19:10

3 Answers3

2

As your SQL is an INSERT query, there will be no result set. The result will be the boolean TRUE if the insert was successful, or FALSE if not. So instead of $check = mysqli_fetch_array($result); you can simply say;

if ($result === TRUE) {
    echo "success";
}
else {
    echo "error";
}
worldofjr
  • 3,868
  • 8
  • 37
  • 49
  • If this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – worldofjr Dec 27 '15 at 03:25
0

mysqli_query manual page:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Therefore $result is already what you want to check for TRUE/FALSE; and not something you want to pass to mysqli_fetch_*.

Please have read of PHP: SQL Injection and also consider using prepared statements+parameters

<?php
require_once('db_config.php');
if($_SERVER['REQUEST_METHOD']=='POST') {
    if (!isset($_POST['Name'], $_POST['Mob'], $_POST['Username'], $_POST['Password']) ) {
        die('missing POST parameter');
    }

    $name = $con->escape_string($_POST['Name']);
    $mob = $con->escape_string($_POST['Mob']);
    $username = $con->escape_string($_POST['Username']);
    $password = $con->escape_string($_POST['Password']);

    $sql = "INSERT INTO user (name,phone,username,password) VALUES ('$name','$mob','$username','$password')";
    $result = mysqli_query($con,$sql);
    if( $result ) {
        echo "sucess";
    }
    else {
        echo "error";
    }
}
else {
    // you should do something/print here....
}
VolkerK
  • 95,432
  • 20
  • 163
  • 226
0

Take MySQL® Databases from Cpanel then click on the privilaged users. After that, ensure 'allow all privileges is checked'. Then execute your php.