Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-21-2011, 08:28 PM   PM User | #1
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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
tomharto is offline   Reply With Quote
Old 09-21-2011, 09:51 PM   PM User | #2
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Anyone able to help?
tomharto is offline   Reply With Quote
Old 09-21-2011, 11:05 PM   PM User | #3
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
why do you have the single quotes around your variables?

Always echo your query or in this case echo $Res;
sunfighter is offline   Reply With Quote
Old 09-21-2011, 11:07 PM   PM User | #4
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
If i echo $Res it says 1, i guess that means the query worked? But it still doesnt update the database :S
tomharto is offline   Reply With Quote
Old 09-21-2011, 11:21 PM   PM User | #5
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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().
Inigoesdr is offline   Reply With Quote
Old 09-21-2011, 11:22 PM   PM User | #6
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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.
tomharto is offline   Reply With Quote
Old 09-21-2011, 11:27 PM   PM User | #7
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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.
Inigoesdr is offline   Reply With Quote
Old 09-21-2011, 11:36 PM   PM User | #8
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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
tomharto is offline   Reply With Quote
Old 09-22-2011, 07:59 AM   PM User | #9
Wanna
Regular Coder

 
Join Date: Sep 2011
Posts: 128
Thanks: 2
Thanked 21 Times in 21 Posts
Wanna is an unknown quantity at this point
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
Wanna is offline   Reply With Quote
Old 09-22-2011, 09:22 AM   PM User | #10
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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?
tomharto is offline   Reply With Quote
Old 09-22-2011, 10:17 AM   PM User | #11
Wanna
Regular Coder

 
Join Date: Sep 2011
Posts: 128
Thanks: 2
Thanked 21 Times in 21 Posts
Wanna is an unknown quantity at this point
Sorry, Give us the echo $Sql; result :P
Wanna is offline   Reply With Quote
Old 09-22-2011, 08:41 PM   PM User | #12
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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..
tomharto is offline   Reply With Quote
Old 09-23-2011, 01:27 PM   PM User | #13
Wanna
Regular Coder

 
Join Date: Sep 2011
Posts: 128
Thanks: 2
Thanked 21 Times in 21 Posts
Wanna is an unknown quantity at this point
I can't find anything in the code (you posted) where i could go wrong.
Maybe you could give us some more code?
Wanna is offline   Reply With Quote
Old 09-23-2011, 03:31 PM   PM User | #14
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Okay ill post the rest when i get in, most of it is just validating the data though.
tomharto is offline   Reply With Quote
Old 09-25-2011, 01:58 PM   PM User | #15
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
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?
tomharto is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:33 AM.


Advertisement
Log in to turn off these ads.