0

Function is running properly and my other query of inserting data is also running perfectly which is commented below.
This is the error i am getting "Error: SELECT MAX(product_id) FROM product_attribute "

  public function add_tbl1($attribute_id , $attribute_description){
    $con=$this->con;
    $id = mysqli_real_escape_string($con, $attribute_id);
    $description = mysqli_real_escape_string($con, $attribute_description);
    $sql2="SELECT MAX(product_id) FROM product_attribute ";
    $no=0;


    if ($con->query($sql2) === TRUE) {
        $no= $con->query($sql2);
        $no=$no+1;
        echo $no;


    } else {
        echo "Error: " . $sql2 . "<br>" . $con->error;
    }   

    /*$sql = "INSERT INTO product_attribute ( attribute_id, attribute_description)
    VALUES ( '$id', '$description')";

    if ($con->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $con->error;
    }   */

    }
2
  • You need to show your SQL table layout because it might have a clue in there for us to tell you what is wrong. Commented Jan 21, 2016 at 7:07
  • You might want to take a look at en.wikipedia.org/wiki/SQL_injection#Hexadecimal_conversion. Look down at the hexadecimal conversion part. There is a PHP function there called dosql that you can use to - well - do SQL commands with and it returns various things. Just an idea. :-) Commented Jan 21, 2016 at 7:11

2 Answers 2

2

if $con is a mysqli-connection, then $con->query with a SELECT will never return true. it will either return false or a resource.

you should rather check with

 $con->query($sql2) !== false))

From http://php.net/manual/en/mysqli.query.php

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So how do i store id in a variable?
2
$con->query($sql2) === TRUE

mysqli::query does not return TRUE on success for SELECT statements. It returns

For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object

So change your if condition accordingly.

$sql2="SELECT MAX(product_id) as id FROM product_attribute ";
if ($result = $con->query($sql2)) {
    $row = $result->fetch_assoc();
    $id  = $row["id"];
}

Comments

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.