INSERT not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

INSERT not working

Post by prgmctan »

Here's the code:

Code: Select all

class Schedule{
    public function createSeason($seasonName) {
        echo $seasonName;
        //Get all season names from database
        $query = 'SELECT seasonName FROM season';
        $result = mysql_query($query);
        
        //Loop to see if it's already in use
        $found = 0;
        
        while ($row = mysql_fetch_array($result)) {
            //If match is found, set $found = 1
            if($row['SeasonName'] == $seasonName){
                $found = 1;
            }
            
        }
        
        //If $seasonName isn't already in use
        if($found == 0){
            //Add to database
            echo $seasonName;
            $query = "INSERT INTO season (SeasonName) VALUES ('$seasonName')";
            if(mysql_query($query)){
                echo "Worked.";
            }else{
                echo "Didn't work.";
            }           
        }else{
            //Else return null
            return null;
        }
    }
    
    public function editSeason(){
    
    }   
}
Here's the problem:
I can't seem to get $seasonName to insert into the database. I can hard code a value, such as:

Code: Select all

 $query = "INSERT INTO season (SeasonName) VALUES ('season1')";
But it doesn't work with the variable. Any thoughts?
User avatar
LanceEh
Forum Commoner
Posts: 46
Joined: Tue Feb 17, 2009 11:53 am
Location: Florida

Re: INSERT not working

Post by LanceEh »

Try taking the quotation marks off the variable. Also try echoing the variable instead of doing the mysql_query to see what value the browser is holding in the variable when it tries to insert it into the query. That's what I would do.

Take care.
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

Re: INSERT not working

Post by prgmctan »

I've tried it without the quotes, with singles quotes, concatenated with double quotes. Nothing seems to work. You can see in my code that I echo the variable a couple times and both times it outputted correctly. Do you think it matters that it's in a class? Thanks for your help!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: INSERT not working

Post by Benjamin »

What is the value of $seasonName? What error message are you getting?
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

Re: INSERT not working

Post by prgmctan »

$seasonName = "season1"

I pass it when I create the season. I'm not really getting an error, it just doesn't get set in the database.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: INSERT not working

Post by Benjamin »

Are you getting "Worked" or "Didn't Work"?
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

Re: INSERT not working

Post by prgmctan »

Didn't work.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: INSERT not working

Post by Benjamin »

So an error is there, you just can't see it. What function would you use to display the error? It's in the PHP manual.

How could you turn on error reporting and display errors so you see other errors when they occur?
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

Re: INSERT not working

Post by prgmctan »

I'm not really sure, I'm pretty new to php. Would it be mysql_error or something? I tried that and nothing happened.
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

Re: INSERT not working

Post by prgmctan »

Ok, so I found the error reporting, I'm using

Code: Select all

error_reporting(E_ALL);
It found an error saying SeasonName was empty so I changed the code to

Code: Select all

while ($row = mysql_fetch_row($result)) {
            //If match is found, set $found = 1
            if($row[0] == $seasonName){
                $found = 1;
                echo "found a match";
            }
            
        }
Now there aren't any errors, but it still isn't putting it in the database.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: INSERT not working

Post by Benjamin »

Good deal. Now you can use mysql_error if the query is still failing.
prgmctan
Forum Newbie
Posts: 19
Joined: Mon Feb 23, 2009 11:29 am

Re: INSERT not working

Post by prgmctan »

EDIT:
It's pretty much working now, just getting an auto increment error.

Thanks for helping me out, you're really helping me troubleshoot for the future.

EDIT2:
Now, it's completely fixed, thanks for all of your help.
Post Reply