Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Query not updating

    This is a snippet of my code

    PHP Code:
    $Sql "UPDATE clans SET name = '$clanName', details = '$clanDetails', website = '$url', image = '$imagePic', clanTag = '$clanTag', email = '$email' WHERE ID = '$clanID'";
                        
    $im "yes";
                    }
                }
            }
            else
            {
                
    $Sql "UPDATE clans SET name = '$clanName', details = '$clanDetails', website = '$url', clanTag = '$clanTag', email = '$email' WHERE ID = '$clanID'";
                
    $im "no";
            }
            if (
    count($errors) > 0)
            {
                foreach (
    $errors as $a => $b)
                {
                    echo 
    $a." - ".$b."<br />";    
                }
            }
            else
            {
                
    //UPDATE Query//
                
                
    $Res mysql_query($Sql);
                if (
    $Res)
                {
                    
    header("Location: ClanDetails.php?ID=$clanID&$im&$Sql");
                }
                else
                {
                    echo 
    "There was an error";    
                }
            } 
    When i run it it redirects to the clandetails page, and the SQL is valid (tried it in phpMyAdmin and it works) and it says yes (The yes no was to see if it was going through the picture upload section).

    Is there something up with the if ($Res) part of my code? Could it be done a better way? That seems like the only reason its doing this.

    The DB connection file is included at the start of the code.

    If you want to see all the code before just say and ill post is but its about an extra 100 lines :P

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Anyone able to help?

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,296
    Thanks
    23
    Thanked 612 Times in 611 Posts
    why do you have the single quotes around your variables?

    Always echo your query or in this case echo $Res;

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    If i echo $Res it says 1, i guess that means the query worked? But it still doesnt update the database :S

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by sunfighter View Post
    why do you have the single quotes around your variables?
    To encapsulate the text values... as you should. Though they should be passed through mysql_real_escape_string().
    Quote Originally Posted by tomharto View Post
    If i echo $Res it says 1, i guess that means the query worked? But it still doesnt update the database :S
    You should be echoing $Sql to see the query; $Res is the return value from mysql_query().

  • #6
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by Inigoesdr View Post
    To encapsulate the text values... as you should. Though they should be passed through mysql_real_escape_string().

    You should be echoing $Sql to see the query; $Res is the return value from mysql_query().
    Thats what i did at first, i echoed the query, and ran it through phpMyAdmin and it worked, no errors or anything which makes it all that more confusing to me.

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by tomharto View Post
    Thats what i did at first, i echoed the query, and ran it through phpMyAdmin and it worked, no errors or anything which makes it all that more confusing to me.
    So everything is working properly? You said it wasn't updating, but the query succeeds. Are you actually changing anything? If you specify the same values mysql_query() will return true even if nothing changes. You can use mysql_affected_rows() to see if something was changed.

  • #8
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Yeah i do change values, what i mean is if i run the echoed query in phpMyAdmin updates, yet if i run it through my website it doesnt, but the mysql_query still returns true

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Try to put a or die after the query.

    mysql_query($Sql) or die(mysql_error());

    And if there is any result put it here.
    Also post the variables with you use in the query

  • #10
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Okay soon as i get chance to try that i will (currently at work).

    By "Also post the variables with you use in the query " do yuo mean post literally post the varibles it outputs or post where there got from?

  • #11
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Sorry, Give us the echo $Sql; result :P

  • #12
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Sorry about the slow reply. The $Sql is this "UPDATE `clans` SET `name` = 'ATestClan!', `details` = 'ATC!', `website` = '', `image` = 'tomharto1-10003.gif', `clanTag` = 'ATC', `email` = '' WHERE `ID` = '11'";

    I also added the or die... bit and it didnt die.

    Also, i know the website and email values are empty, i didnt enter anything for them in the form
    Last edited by tomharto; 09-22-2011 at 08:53 PM.

  • #13
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    I can't find anything in the code (you posted) where i could go wrong.
    Maybe you could give us some more code?

  • #14
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Okay ill post the rest when i get in, most of it is just validating the data though.

  • #15
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    PHP Code:
    <?php 
    session_start
    ();
    error_reporting (E_ALL E_NOTICE);
    $styleSheet "style.css";
    include (
    "include/config.php");
    include (
    "include/function.php");
    isLoggedIn("yes");
    updateLastAct($_SESSION['userID']);
    $userDet = new getUserDetails($_SESSION['userID']);
    $userID $userDet->userDetails['user_id'];

    if (isset(
    $_GET['ID']))
    {
        
    $clanID $_GET['ID'];
        
    $Sql "SELECT * FROM clan_members WHERE userID = '$userID' AND clanID = '$clanID' AND clanLeader = '1'";
        
    $Res mysql_query($Sql);
        if (
    mysql_num_rows($Res) == "0")
        {
            
    header ("Location: ClanDetails.php?ID=$clanID");    
        }
        if (
    $_GET['action'] == "edit")
        {
            
            if (
    strlen($_POST['clanName']) > 0)
            {
                
    $clanName mysql_real_escape_string($_POST['clanName']);    
            }
            else
            {
                
    $errors['Clan name'] = " wasn't set";
            }
            
            if (
    strlen($_POST['clanTag']) > 0)
            {
                
    $clanTag mysql_real_escape_string($_POST['clanTag']);    
            }
            else
            {
                
    $errors['Clan tag'] = " wasn't set";
            }
            
            if (
    strlen($_POST['clanDetails']) > 0)
            {
                
    $clanDetails mysql_real_escape_string($_POST['clanDetails']);    
            }
            else
            {
                
    $clanDetails "";
            }
            
            
            if (isset(
    $_POST['website']))
            {
                
        
                
    $url_filtered filter_var($_POST['website'], FILTER_VALIDATE_URL );
        
                if ( 
    FALSE !== $url_filtered )
                {
                    
    $url $_POST['website'];
                }  
                else
                {
                    
    $errors['Website'] = " wasn't valid";
                }
                
            }
            
            
            if (
    strlen($_POST['email']) > 0)
            {
                if (
    preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/'$_POST['email']))
                {
                    
    $email $_POST['email'];
                }
                else
                {
                    
    $errors['Email address'] = " wasn't valid";
                }
            
            }
            if (
    strlen($_FILES['profilePic']['name']) > 0)
            {
            
    $aAllowed = array('image/jpg''image/png''image/gif''image/jpeg''image/pjpeg');
                if (!
    in_array($_FILES['profilePic']['type'], $aAllowed))
                {
                   
    $errors['Image'] = " - Invalid image type. (".$_FILES['profilePic']['type'].")";
                   
                }
                else
                {
                    if (
    $_FILES['profilePic']['size'] > (500*1024))
                    {
                        
    $errors['Image'] = " - Image type too big";
                         
                    }
                    else
                    {
                        
    $imagePic str_replace(" ""_"substr($_SESSION['username'], 020))."-".substr($_FILES['profilePic']['name'], -10);
                        
                        
    $target_path "clanImages/";

                        
    $target_path $target_path.$imagePic

                        if(
    move_uploaded_file($_FILES['profilePic']['tmp_name'], $target_path)) {
                    
                        } else{
                            
    $errors['Image'] = " - There was an error uploading the file, please try again!";
                        }
                        
                        
    $Sql "UPDATE `clans` SET `name` = '$clanName', `details` = '$clanDetails', `website` = '$url', `image` = '$imagePic', `clanTag` = '$clanTag', `email` = '$email' WHERE `ID` = '$clanID'";
                        
    $im "yes";
                    }
                }
            }
            else
            {
                
    $Sql "UPDATE clans SET name = '$clanName', details = '$clanDetails', website = '$url', clanTag = '$clanTag', email = '$email' WHERE ID = '$clanID'";
                
    $im "no";
            }
            if (
    count($errors) > 0)
            {
                foreach (
    $errors as $a => $b)
                {
                    echo 
    $a." - ".$b."<br />";    
                }
            }
            else
            {
                
    //UPDATE Query//
                
                
    $Res mysql_query($Sql) or die(mysql_error());
                if (!
    $Res)
                {
                    echo 
    "There was an error";
                }
                else
                {
                        
    header("Location: ClanDetails.php?ID=$clanID&$im&$Sql");
                }
            }
            
        }
        else
        {    
        
        
        
        }
    }
    Thats all the code above what ive already posted.

    Anyone see any issues there as to why this problem is happening?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •