http://stackoverflow.com/questions/5846/add-1-to-a-field
Didn't really work.

My script looks as follows:

<?php

    if (isset($_GET["id"]) && isset($_GET["char"])) {
        $id = $_GET["id"];
        $char = $_GET["char"];
        $result = mysql_query("SELECT * FROM battles WHERE id='$id'");

        if ($char == "1") {
            $bool = "first_char_votecount";
            while($row = mysql_fetch_array($result)) {
                $mincemeat = 1 + $row["first_char_votecount"];
            }
        }
        elseif ($char == "2") {
            $bool = "second_char_votecount";
            $row = mysql_fetch_array($result);
            $mincemeat = 1 + $row["second_char_votecount"];
        }
    }

    mysql_query("UPDATE battles SET $bool = $mincemeat WHERE id = $id");
?>

At ?id=34&char=1. The query should go from UPDATE battles SET $bool = $mincemeat WHERE id = $id" to UPDATE battles SET first_char_votecount = 2 WHERE id = 34". Instead, I get this:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ===\XAMPP\htdocs\vote.php on line 12
Notice: Undefined variable: mincemeat in ===\XAMPP\htdocs\vote.php on line 23

Recommended Answers

All 4 Replies

Your $result variable probably is false, which isn't a resource and so the fetch_array is failing. Check your query returns a result set and not an error. If it is failing to query I'm guessing $id isn't a value.
So, output the string that makes up the complete query to check for a syntax error or catch the error coming from the database.

Your $result variable probably is false

It's [null] and I don't know why.

Check your query returns a result set and not an error.

MySQL database reacted well without problems.

If it is failing to query I'm guessing $id isn't a value.

As I mentioned in the main post: ?id=34&char=1.

So, output the string that makes up the complete query to check for a syntax error or catch the error coming from the database.

Database reacted well to the query.

Have you tried this:

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

after the mysql_query line? $result should be a result set or FALSE, mysql_query doesn't return null.

Haha. Pathetic me! I forgot to include SQL connection config file. It just didn't knew what to do! The code was correct, but there was no database connected. I'm so stupid!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.