[SOLVED] problems with an insert statement

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
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

[SOLVED] problems with an insert statement

Post by thallish »

hi all


I hav a problem with an insert stement. I'll drop you my code

Code: Select all

if(isset($_POSTї'projectClient']) && isset($_POSTї'projectDescription']) ){
    
    if($_POSTї'projectClient'] == '' ){
    
        echo 'Der mangler at blive valgt et firma';

    } else if($_POSTї'projectDescription'] == 'Indtast beskrivelse her' || $_POSTї'projectDescription'] == ''){

        echo 'Der mangler at bllive givet en projektbeskrivelse.';

} else {
    
        $projectClientId = addslashes(trim($_POSTї'projectClient']));
        $projectDescription = addslashes(trim($_POSTї'projectDescription']));
            
        $connection = connection();
            
            
        $projectQuery = "INSERT INTO project SET
                        projectDescription = '$projectDescription' ";
        
        if(mysql_query($projectQuery) ){
            
            $query = "SELECT MAX(projectId) FROM project";
            
            $result = mysql_query($query);
        
            if (!$result) {
              die('<p>Error performing query: ' . mysql_error() .  '</p>');
            &#125; else &#123;

            while($row = mysql_fetch_array($result))&#123;
    
                $projectId = $row&#1111;'MAX(projectId)'];
            
            &#125;
        
            echo($projectId);
            echo($projectClientId);
            
            $projectClientQuery = "INSERT INTO project_client SET
                        projectId ='$projectID',
                        clientId = '$projectClientId' ";
            &#125;
        
            if (mysql_query($projectClientQuery))&#123;//FIXME
            
                echo "Projektet er nu blevet oprettet.";
                
                DBClose();    
                    
            &#125; else &#123;
                
                    $query = "DELETE FROM project WHERE projectId ='$projectId'";
                    
                if(@mysql_query($query))&#123;
                    
                        echo('<p>Noget gik galt under oprettelsen. Du kan prøve igen.</p>');
                    
                &#125; else &#123;
                        
                            echo('<p>Noget gik virkelig galt under oprettelsen og behøver nærmere eftersyn af administratoren af databasen. Men bare rolig det går jo nok al sammen;-).');
                &#125;
            &#125;
        
        &#125; else &#123;
            echo "</p>Der opstod en fejl ved oprettelsen. Prøv igen.</p>";;    
            DBClose();
            
        &#125;
            
    &#125;
    
&#125; else &#123;
    goTo("addproject.php");    
&#125;
Ok my problem lies where it says //FIXME. I don't know what goes wrong here but it dosen't evaluate to true in that if-statement and I haven't the slightest idea why! Can anybody help? I will be greatful! ;)

/thallish
Last edited by thallish on Sun Mar 06, 2005 2:20 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd bet 'projectId' is a primary or unique key. In which case, the insert fails, due to a pre-existing key of the same data. Display mysql_error() in the "false" part..
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

yep it was caused by the unique key or two of them actually.. I have to read up on that I think :lol:

Well now my problem is that my projectId is allways zero though my echo says it the correct number? I'm a little confused. Can someone spot that one?

humble and obeying

/thallish
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

echo $projectClientQuery, make sure it's correct.
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

ahh I got it!!!


My eyes were on everything else than the brackets!! I tried to use $projectClientQuery which were declared in an inner scope!! Stupid me :lol:

well now it works just fine... hahah

thx

/thallish
Post Reply