如何执行多requete pdo mysql

时间:2018-01-22 09:52:38

标签: php mysql pdo

我不知道如何执行multi requete sql

   <?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_SESSION['pseudo']) && !empty($_SESSION['pseudo']) && isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) {

        $id     = trim(htmlspecialchars($_SESSION['user_id']));
        $pseudo = trim(htmlspecialchars($_SESSION['pseudo']));
        $stmt->$connect->prepare('SELECT id,pseudo,jeton FROM users  WHERE id=:id AND pseudo=:pseudo');
        $stmt->execute(array(
            ':id' => $id,
            ':pseudo' => $pseudo
        ));

        $row = $stmt->fetch();

        if ($_SESSION['pseudo'] == $row['pseudo'] && $_SESSION['user_id'] == $row['id']) {



            // Files Image profil
            $avatarName    = $_FILES['avatarCouverture']['name'];
            $avatarTmp     = $_FILES['avatarCouverture']['tmp_name'];
            $avatarSize    = $_FILES['avatarCouverture']['size'];
            $avatarType    = $_FILES['avatarCouverture']['type'];
            $extension     = array(
                'jpg',
                'jpeg',
                'png'
            );
            $avatarExplode = explode('.', $avatarName);
            $avatarEnd     = end($avatarExplode);
            $avatarLower   = strtolower($avatarEnd);



            $saveIdUser    = trim(htmlspecialchars($_SESSION['user_id']));
            $saveIdUserNew = filter_var($saveIdUser, FILTER_SANITIZE_NUMBER_INT);
            $avatar        = rand(0, 100000) . '_' . date('Y-m-d H-i-s') . '_' . $saveIdUserNew;

            if (isset($avatarName) && !empty($avatarName)) {

                if ($avatarSize < 8000000) {
                    if (in_array($avatarLower, $extension)) {
                        if (isset($avatarTmp) && !empty($avatarTmp)) {


                            $image = getimagesize($avatarTmp);
                            if ($image['mime'] === 'image/jpeg') {
                                $imageSrc = imagecreatefromjpeg($avatarTmp);

                            } elseif ($image['mime'] === 'image/png') {
                                $imageSrc = imagecreatefrompng($avatarTmp);
                            } else {
                                $imageSrc = false;

                            }
                            // modifier size image
                            if ($imageSrc !== false) {
                                $imageWidth  = 60;
                                $imageHeight = 60;

                                if ($image[0] >= $imageWidth && $image[1] >= $imageHeight) {
                                    $imageFinale = $imageSrc;
                                } else {
                                    $newWidth[0]  = $imageWidth;
                                    $newHeight[1] = $imageHeight;
                                    $imageFinale  = imagecreatetruecolor($newWidth[0], $newHeight[1]);
                                    imagecopyresampled($imageFinale, $imageSrc, 0, 0, 0, 0, $newWidth[0], $newHeight[1], $image[0], $image[1]);
                                }


                                $target = imagejpeg($imageFinale, 'upload/avatarPost/' . $avatar . '.jpg');

                                move_uploaded_file($avatarTmp, $target);
                            } // ! == false 

                        }


                    }

                }

            }

            $stmt = $connect->prepare('INSERT INTO users(avatarcouverture) VALUES(:avatarne) ');

            $stmt->execute(array(
                ":avatarne" => $avatar
            ));

            //header('Location: profil.php');
        }

    }

}
?>

1 个答案:

答案 0 :(得分:0)

我认为问题是初步准备好的声明作业

$stmt->$connect->prepare('SELECT id,pseudo,jeton FROM users  WHERE id=:id AND pseudo=:pseudo');

应该更像是这样。

$stmt = $connect->prepare('SELECT id, pseudo, jeton FROM users WHERE id=:id AND pseudo=:pseudo');

最终的sql查询肯定只有在成功添加头像的情况下才能运行?在寻找问题的同时进行了一些重组 - 希望它有所帮助。

if( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
    if( !empty( $_SESSION['pseudo'] ) && !empty( $_SESSION['user_id'] ) ) {

        $id = trim( htmlspecialchars( $_SESSION['user_id'] ) );
        $pseudo = trim( htmlspecialchars($_SESSION['pseudo'] ) );
        /*
            Assign the prepared statement as a variable
        */
        $stmt = $connect->prepare('SELECT id, pseudo, jeton FROM users WHERE id=:id AND pseudo=:pseudo');
        if( !$stmt )exit('Failed to prepare sql');
        $stmt->execute( array(
                        ':id'       => $id,
                        ':pseudo'   => $pseudo
                    )
                );
        $row=$stmt->fetch();

        if( $_SESSION['pseudo'] == $row['pseudo'] && $_SESSION['user_id'] == $row['id'] ) {

            $avatarName     =   $_FILES['avatarCouverture']['name'];
            $avatarTmp      =   $_FILES['avatarCouverture']['tmp_name'];
            $avatarSize     =   $_FILES['avatarCouverture']['size'];
            $avatarType     =   $_FILES['avatarCouverture']['type'];

            $extension      =   array('jpg','jpeg','png');
            $avatarLower    =   strtolower( pathinfo($avatarName,PATHINFO_EXTENSION ) );

            $saveIdUser  = trim( htmlspecialchars( $_SESSION['user_id'] ) );
            $saveIdUserNew = filter_var( $saveIdUser, FILTER_SANITIZE_NUMBER_INT ); 
            $avatar = rand(0,100000).'_'.date('Y-m-d H-i-s').'_'.$saveIdUserNew;

            if( !empty( $avatarName ) ) {
                if( $avatarSize < 8000000 ) {
                    if( in_array( $avatarLower, $extension ) ){
                        if( !empty( $avatarTmp ) ) {

                            $image = getimagesize( $avatarTmp );

                            if( $image['mime'] === 'image/jpeg' ) {
                                $imageSrc = imagecreatefromjpeg( $avatarTmp );
                            } elseif( $image['mime']==='image/png' ) {
                                $imageSrc = imagecreatefrompng( $avatarTmp );
                            } else {
                                $imageSrc = false;
                            }

                            // modifier size image
                            if( $imageSrc !== false ) {

                                $imageWidth = 60;
                                $imageHeight = 60;

                                if( $image[0]>= $imageWidth && $image[1]>= $imageHeight ) {
                                    $imageFinale = $imageSrc ;  
                                } else {
                                    $newWidth[0]  = $imageWidth;
                                    $newHeight[1] = $imageHeight;
                                    $imageFinale = imagecreatetruecolor( $newWidth[0], $newHeight[1] );

                                    imagecopyresampled( $imageFinale, $imageSrc, 0,0,0,0, $newWidth[0], $newHeight[1], $image[0], $image[1] );
                                }

                                $target = imagejpeg( $imageFinale,'upload/avatarPost/'.$avatar.'.jpg' );

                                move_uploaded_file( $avatarTmp ,$target );


                                $stmt=$connect->prepare('INSERT INTO users( avatarcouverture ) VALUES( :avatarne )');
                                if( !$stmt )exit('Failed to prepare sql');
                                $stmt->execute( array( ":avatarne"=> $avatar ) );                               

                            }
                        }
                    }
                }
            }


            //header('Location: profil.php');
        }
    }
}