Only echoes the second time around .... ?

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
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Only echoes the second time around .... ?

Post by Steveo31 »

Hey all.

This code here works okay, just has to be refreshed to get it to echo the correct values. Does anyone know why? I'm thinking its the auto_increment, but I'm not sure.

Code: Select all

<?php

$db = mysql_connect("localhost", "root") or die("Error.  ".mysql_error());
mysql_select_db('vacation', $db) or die(mysql_error());

$name = $_POST['name'];
@$jamaica = $_POST['jamaica'];
@$bolivia = $_POST['bolivia'];
@$haiti = $_POST['haiti'];

if($jamaica){
	$location = 'jamaica';
}elseif($bolivia){
	$location = 'bolivia';
}elseif($haiti){
	$location = 'haiti';
}

$userInfo = "INSERT INTO ".$location." (`name`) VALUES ('".$name."')";
$tripInfo = "SELECT name, total FROM `".$location."`";
$getTotal = "SELECT COUNT(total) FROM `".$location."`";

$getTripInfo = mysql_query($tripInfo, $db) or die("getTripInfo  ".mysql_error());

$totalResult = mysql_query($getTotal, $db);
$row = mysql_fetch_row($totalResult);


//
//it should output all of this here, and does once refreshed... but I shouldn't have to refresh...
//

$insertUserInfo = mysql_query($userInfo, $db) or die("InsertUserInfo:  ".mysql_error());	
while($row = mysql_fetch_assoc($getTripInfo)){
echo "You chose to go on the <b>".$location."</b> Trip.<br>";
echo "Name:  ".$row['name']."<br>";
echo "Your Spot Number:  ".$row['total'];

}
mysql_close();
		
?>
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

It looks like you run the insert after you do the SELECT. Shouldn't the Select mysql_query be after your insert user info mysql_query.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

yeah its becase you querying the SELECT statement from the DB before you INSERT into it....
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ahh okay. Thanks.

:)
Post Reply