正确使用MYSQL UPDATE函数

时间:2014-04-03 05:44:04

标签: php mysql

我正在使用MySQL查询来更新数据库中的信息。

我尝试从localhost / phpmyadmin更新信息,然后复制localhost / phpmyadmin中给出的代码。

问题是数据库中的信息/值没有更新。

以下是代码:

<?php

   if(isset($_POST['updateProfile']))
     {
       $newUser = $_POST['newUsername'];
       $newPass = $_POST['newPassword'];
       $newConNum = $_POST['newContactNumber'];
       $newAdd = $_POST['newAddress'];

    include("dbconnect.php");

    //avatarPATH
    $filepath = "avatar/owner-".$_SESSION['username']."-fname-".$_SESSION['fname']."-l_name-".$_SESSION['lname']."-filename-".$_FILES["file"]["name"];

  $checkQuery = "SELECT * FROM `users`.`info` WHERE username = '".$userName."' ";
                $checkResult = $con->query($checkQuery);
                $count = mysqli_num_rows($checkResult);

                    while ($rows = mysqli_fetch_array($checkResult, MYSQLI_ASSOC))

                        {
                            $username = $rows['username'];
                            $userpass = $rows['password'];
                            $firstName = $rows['firstname'];
                            $lastName = $rows['lastname'];
                            $ConNum = $rows['contact_number'];
                            $usrAdd = $rows['user_address'];
                            $avaImgPth = $rows['avatar_image_path'];
                            $adminLvl = $rows['admin_level'];
                        }

                $query = "SELECT * FROM `users`.`info` WHERE username = '".$newUser."' ";
                $queryResult = $con->query($query);
                $result = mysqli_num_rows($queryResult);

                    if($result == 1)
                        {
                            echo $newUser." is already in use";
                        }

                    else
                        {
                            move_uploaded_file($_FILES["file"]["tmp_name"], "avatar/owner-".$_SESSION['username']."-fname-".$_SESSION['fname']."-l_name-".$_SESSION['lname']."-filename-".$_FILES["file"]["name"]);

                            //$updateQuery = "UPDATE `users`.`info` SET `username` = $newUser, `password` = $newPass, `contact_number` = $newConNum , `user_address` = $newAdd, `avatar_image_path` = $filepath WHERE `info`.`username` = '$username' AND `info`.`password` = '$userpass' AND `info`.`f_name` = '$firstname' AND `info`.`l_name` = '$lastname' AND `info`.`admin_level` = '$adminLvl'  AND  `info`.`contact_number` = '$ConNum' AND  AND  `info`.`user_address` = '$usrAdd' AND  `info`.`avatar_image_path` = '$avaImgPth' ";

                            $updateQuery = " UPDATE  `users`.`info` SET  `username` =  '$newUser',`password` =  '$newPass',`f_name` =  '$firstName',`l_name` =  '$lastName',`contact_number` =  '$newConNum',`user_address` =  '$newAdd',`avatar_image_path` =  '$filepath' WHERE  `info`.`username` =  'username' AND  `info`.`password` =  'userpass' AND `info`.`f_name` =  'firstName' AND  `info`.`l_name` =  'lastName' AND  `info`.`admin_level` =$adminLvl AND  `info`.`contact_number` =  '$ConNum' AND  `info`.`user_address` =  '$usrAdd' AND `info`.`avatar_image_path` =  '$avaImgPth' ";

                            echo "Profile successfully UPDATED!";
                        }

            }
    ?>

非常感谢您的回复。

1 个答案:

答案 0 :(得分:2)

您忘了执行查询。

    $updateQuery =" UPDATE  `users`.`info` SET  `username` =  '$newUser',`password` =  '$newPass',`f_name` =  '$firstName',`l_name` =  '$lastName',`contact_number` =  '$newConNum',`user_address` =  '$newAdd',`avatar_image_path` =  '$filepath' WHERE  `info`.`username` =  '$username' AND  `info`.`password` =  '$userpass' AND `info`.`f_name` =  '$firstName' AND  `info`.`l_name` =  '$lastName' AND  `info`.`admin_level` =$adminLvl AND  `info`.`contact_number` =  '$ConNum' AND  `info`.`user_address` =  '$usrAdd' AND `info`.`avatar_image_path` =  '$avaImgPth' ";

    $excuteQuery = $con->query($updateQuery);