0

I've been trying to build a comments section for my website using PHP5, however when I try to use the $_GET method it stores the wrong value in the database column.Originally I just used $_GET['pageid'] but that would store NULL instead of an integer and that was because $_GET['pageid'] had a string datatype.

I then began to use (int)$_GET['pageid'] which would store an integer but the wrong value. It would store 0 every time. I know other people have had the same problem as me but I found no viable solution.

I am able to retrieve comments from my database and post them to their respective pages using $_GET['pageid'] after manually inserting integers in the pageID column.

If it makes any difference I am using Webmatrix 3 with MySQLi and PHP5. Thanks in advance!

    if (isset($_POST['submit']))
    {
        $pageid = (int)$_GET['pageid'];
        $username = $_SESSION['username'];
        $comment = $_POST['comment'];
        $query = "INSERT INTO comments (pageID, username, comment) VALUES (?, ?, ?)";

        $statement = $databaseConnection->prepare($query);
        $statement->bind_param('iss', $pageid, $username, $comment);
        $statement->execute();
        $statement->store_result();

        if ($statement->error)
        {
            die('Database query failed: ' . $statement->error);
        }

        $creationWasSuccessful = $statement->affected_rows == 1 ? true : false;
        if ($creationWasSuccessful)
        {   
            header ("Location: index.php");
        }
    }
  • try `$_REQUEST` instead of `$_GET` – Debug Diva Jan 14 '15 at 06:39
  • Your form method is **post** and the way you are retriving form data is **GET** ! that is probably the mistake as far as I noticed – Amit Verma Jan 14 '15 at 06:40
  • it does not matter. you can access get parameters, even if you send the form by post. – vaso123 Jan 14 '15 at 06:41
  • If you have both characters and numbers in your ID , you can make the column type VARCHAR instead of INT , what is your result of $_GET['pageid'] , before casting ??? – Kanishka Panamaldeniya Jan 14 '15 at 06:58
  • Using $_REQUEST doesn't work, lolka_bolka is correct, I only use numbers in my ID and the result of $_GET'pageid'] when I echo it to the webpage is the correct value but as a string. It shows the correct value as an integer after casting but still stores it as a 0. – The_Perfect_Username Jan 14 '15 at 10:08

1 Answers1

0

If you CAST a string to integer it will become 0 , if the string has numbers in the beginning , only those numbers will be converted to integer , an also if a string begins with a space Ex:- " 12word" , the result will be 12 .

check these examples

$str = 'Hello World 12';
$int = (int)$str;   
echo $int; // 0


$str = 'Hello World';
$int = (int)$str;   
echo $int; // 0


$str = 'Hello 12 World';
$int = (int)$str;   
echo $int; // 12  

$str = '12Hello World';
$int = (int)$str;    
echo $int; // 12

Check this link to get more Ideas . http://www.phpf1.com/tutorial/php-string-to-int.html

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • But even when I cast the string to an integer I can echo the correct value to the webpage as an integer when debugging. Casting it doesn't turn it to a 0 until I submit a comment to the database. – The_Perfect_Username Jan 14 '15 at 10:05