Executing Query Twice?

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
carleihar
Forum Commoner
Posts: 36
Joined: Fri Nov 13, 2009 5:59 pm

Executing Query Twice?

Post by carleihar »

For some reason this script enters two horses into the horses database. (I've deleted some of the functions for easier viewing.) I don't have any while or for loops so I don't understand why it's doing it. Any help?

Code: Select all

<?php
 
include('includes/header01.html');
include('includes/breed.inc.php');
 
if ( (isset($_GET['dad'])) && (is_numeric($_GET['dad'])) &&  (isset($_GET['mom'])) && (is_numeric($_GET['mom']))) { //from create_horse.php
    $momhorseid = $_GET['mom'];
    $dadhorseid = $_GET['dad'];
 
    $q = "SELECT color, height, temperament, confirmation, movement, understanding, confidence, strength FROM horses WHERE horse_id='$momhorseid'";     
    $r = @mysqli_query ($dbc, $q); // Run the query.
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 
            $momcolor = $row['color'];
            $momheight = $row['height'];
            $momtemp = $row['temperament'];
            $momconfo = $row['confirmation'];
            $mommovement = $row['movement'];
            $momunder = $row['understanding'];
            $momconfi = $row['confidence'];
            $momstrength = $row['strength'];
            
            
    }
        
    $q = "SELECT color, height, temperament, confirmation, movement, understanding, confidence, strength FROM horses WHERE horse_id='$dadhorseid'";     
    $r = @mysqli_query ($dbc, $q); // Run the query.
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 
            $dadcolor = $row['color'];
            $dadheight = $row['height'];
            $dadtemp = $row['temperament'];
            $dadconfo = $row['confirmation'];
            $dadmovement = $row['movement'];
            $dadunder = $row['understanding'];
            $dadconfi = $row['confidence'];
            $dadstrength = $row['strength'];
            
    }
 
 
/******************FUNCTIONS ARE HERE*******************
    
    
                $q = "INSERT INTO horses (dad_id, mom_id, user_name, color, height, speed, temperament, confirmation, movement, understanding, confidence, talent, strength, gender, date_created) VALUES ('$dadhorseid', '$momhorseid', '{$_COOKIE['username']}', '$color', '$height', '$speed', '$temperament', '$confo', '$movement', '$understanding', '$confidence', '$talent', '$strength', '$gender', NOW())";
                $r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error($dbc));
 
                if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
                
                /*  
                    $p = "INSERT INTO activeShow (horseID) VALUES (LAST_INSERT_ID())";
                    $t = mysqli_query($dbc, $p) or trigger_error(mysqli_error($dbc));
                    
                    if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.
        
                        echo 'Your horse is now in foal!';
                        
                    } else { // If it did not run OK.
                        echo 'There was an error. Try again.';
                    }*/
                    
                } else {
                
                    echo 'There was an error. Try again later.';
                    
                }   
    
    
    
    
    
} else {
    echo 'This page has been accessed in error.';
}
    
    
    
    
include('includes/footer01.html');
 
 ?>
P.S. I know this is a huge table with lots of data, but I'm just practicing right now, I'll revise it later.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Executing Query Twice?

Post by JakeJ »

Try just echoing out the results of your query first before doing the insert. That might provide you with some insight as to where the extra data is coming from. But it doesn't look like it should be inserting two records.

My other thought was that: include('includes/breed.inc.php');

Is triggering another insert. Take a look at the code in there to be sure.
Post Reply