0
votes

I made a profile page for users, but I am getting this error:

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'id' at row 1 in C:\wamp64\www\social_network\classes\DB.php on line 12 ( ! ) PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'id' at row 1 in C:\wamp64\www\social_network\classes\DB.php on line 12

I am sure that my \classes\DB.php is correct, because it is connected to other php files. This is the "profile page" code that I am using:

Thank you for your time.

Call Stack
#   Time    Memory  Function    Location
1   0.0009  391176  {main}( )   ...\profile.php:0
2   0.0329  444848  Post::createPost( ) ...\profile.php:62
3   0.0330  445224  DB::query( )    ...\Post.php:25
4   0.0349  461048  execute ( ) ...\DB.php:12

<?php
include('./classes/DB.php');
include('./classes/Login.php');
include('./classes/Post.php');
include('./classes/Image.php');

$username = "";
$verified = False;
$isFollowing = False;
if (isset($_GET['username'])) {
        if (DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$_GET['username']))) {

                $username = DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$_GET['username']))[0]['username'];
                $userid = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$_GET['username']))[0]['id'];
                $verified = DB::query('SELECT verified FROM users WHERE username=:username', array(':username'=>$_GET['username']))[0]['verified'];
                $followerid = Login::isLoggedIn();

                if (isset($_POST['follow'])) {

                        if ($userid != $followerid) {

                                if (!DB::query('SELECT follower_id FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid))) {
                                        if ($followerid == 6) {
                                                DB::query('UPDATE users SET verified=1 WHERE id=:userid', array(':userid'=>$userid));
                                        }
                                        DB::query('INSERT INTO followers VALUES (null, :userid, :followerid)', array(':userid'=>$userid, ':followerid'=>$followerid));
                                } else {
                                        echo 'Already following!';
                                }
                                $isFollowing = True;
                        }
                }
                if (isset($_POST['unfollow'])) {

                        if ($userid != $followerid) {

                                if (DB::query('SELECT follower_id FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid))) {
                                        if ($followerid == 6) {
                                                DB::query('UPDATE users SET verified=0 WHERE id=:userid', array(':userid'=>$userid));
                                        }
                                        DB::query('DELETE FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid));
                                }
                                $isFollowing = False;
                        }
                }
                if (DB::query('SELECT follower_id FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid))) {
                        //echo 'Already following!';
                        $isFollowing = True;
                }

                if (isset($_POST['deletepost'])) {
                        if (DB::query('SELECT id FROM posts WHERE id=:postid AND user_id=:userid', array(':postid'=>$_GET['postid'], ':userid'=>$followerid))) {
                                DB::query('DELETE FROM posts WHERE id=:postid and user_id=:userid', array(':postid'=>$_GET['postid'], ':userid'=>$followerid));
                                DB::query('DELETE FROM post_likes WHERE post_id=:postid', array(':postid'=>$_GET['postid']));
                                echo 'Post deleted!';
                        }
                }


                if (isset($_POST['post'])) {
                        if ($_FILES['postimg']['size'] == 0) {
                                Post::createPost($_POST['postbody'], Login::isLoggedIn(), $userid);
                        } else {
                                $postid = Post::createImgPost($_POST['postbody'], Login::isLoggedIn(), $userid);
                                Image::uploadImage('postimg', "UPDATE posts SET postimg=:postimg WHERE id=:postid", array(':postid'=>$postid));
                        }
                }

                if (isset($_GET['postid']) && !isset($_POST['deletepost'])) {
                        Post::likePost($_GET['postid'], $followerid);
                }

                $posts = Post::displayPosts($userid, $username, $followerid);


        } else {
                die('User not found!');
        }
}

?>
<h1><?php echo $username; ?>'s Profile<?php if ($verified) { echo ' - Verified'; } ?></h1>
<form action="profile.php?username=<?php echo $username; ?>" method="post">
        <?php
        if ($userid != $followerid) {
                if ($isFollowing) {
                        echo '<input type="submit" name="unfollow" value="Unfollow">';
                } else {
                        echo '<input type="submit" name="follow" value="Follow">';
                }
        }
        ?>
</form>

<form action="profile.php?username=<?php echo $username; ?>" method="post" enctype="multipart/form-data">
        <textarea name="postbody" rows="8" cols="80"></textarea>
        <br />Upload an image:
        <input type="file" name="postimg">
        <input type="submit" name="post" value="Post">
</form>

<div class="posts">
        <?php echo $posts; ?>
</div>
1
Want us to count the lines? What is "line 12"?Paul Spiegel
oh sorry bro in this code line 12 is empty but in /classes/DB.php is $statement->execute($params); and in the database id is an integerkarim
This error usually occurs for an INSERT or UPDATE query. So which query in particular is causing that error? What is the id column for that table defined as? Are you setting an empty value on purpose, or is it missing an AUTO_INCREMENT flag?rickdenhaan
The exception should print the complete stacktrace. Where is it?Paul Spiegel
the id in the query followers is AUTO_INCREMENT Primary_KEY with null no and default nonekarim

1 Answers

0
votes

General error: 1366 Incorrect integer value: '' for column 'id' [...]

You are trying to save the string/varchar value '' (an empty string) to the column 'id' which is an integer type. As you cannot save a string in an integer column you get this error message.

Since it is an AUTO_INCREMENT column the solution is to not provide a value for this column at all. You can do this by adding the columns in your INSERT statement you want to fill.

INSERT INTO Table(colA, colB) VALUES (val1, val2);

This way all the remaining columns will get the default or AUTO_INCREMENT values.