Page 1 of 1

INSERT not working

Posted: Mon Feb 23, 2009 11:39 am
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?

Re: INSERT not working

Posted: Mon Feb 23, 2009 12:04 pm
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.

Re: INSERT not working

Posted: Mon Feb 23, 2009 1:07 pm
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!

Re: INSERT not working

Posted: Mon Feb 23, 2009 1:15 pm
by Benjamin
What is the value of $seasonName? What error message are you getting?

Re: INSERT not working

Posted: Mon Feb 23, 2009 1:24 pm
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.

Re: INSERT not working

Posted: Mon Feb 23, 2009 1:29 pm
by Benjamin
Are you getting "Worked" or "Didn't Work"?

Re: INSERT not working

Posted: Mon Feb 23, 2009 1:32 pm
by prgmctan
Didn't work.

Re: INSERT not working

Posted: Mon Feb 23, 2009 1:34 pm
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?

Re: INSERT not working

Posted: Mon Feb 23, 2009 2:12 pm
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.

Re: INSERT not working

Posted: Mon Feb 23, 2009 2:37 pm
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.

Re: INSERT not working

Posted: Mon Feb 23, 2009 2:38 pm
by Benjamin
Good deal. Now you can use mysql_error if the query is still failing.

Re: INSERT not working

Posted: Mon Feb 23, 2009 2:43 pm
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.