Page 1 of 1

[Resolved] two problems that are giving me a hard time

Posted: Sat Jan 29, 2011 1:04 am
by Slav2
ok this is going to a little hard to explain.

--------------------------------------------------------first issue:---------------------------------------------------------

ok its not adding the cityid to one table...here are the codes:

Code: Select all

//insert into users table
		$sql2="INSERT INTO users (username, points, mycities, email, password) VALUES ('$name', '5', '1', '$email', '$password')";
		$result2=mysql_query($sql2);
		
		if ($result2)
		{
		   
			//starting values
			$mycityname = $name . "\'s city";
			
			$sql43="SELECT * FROM users WHERE username='$name'";
			$result10=mysql_query($sql43);
			$result99=mysql_fetch_array($result10);
			$userid = $result99['id'];
			
			$sql3="INSERT INTO resources (userid, cityname, wood, stone, iron, food, people, gold, happiness, loyalty) VALUES ('$userid', '$mycityname', '200', '200', '200', '150', '10', '200', '100', '100')";
			$result3=mysql_query($sql3) or die ('Error A:'. mysql_error() . ": " . mysql_error());
			
				if ($result3)
				
				{
				$search="SELECT * from resources where userid='$userid'";
				$search2=mysql_query($search) or die("could not cityid");
				$search3 = mysql_fetch_array($search2);
				$cityid = $search2['id'];
				
				}
				
				if ($search3)
				
				{
				$sql3="INSERT INTO player_buildings (cityid, userid, buildingid, buildingtype, tagid) VALUES ('$cityid', '$userid', '250', 'flag', 'tag1')";
				$result3=mysql_query($sql3) or die ('Error B:'. mysql_error() . ": " . mysql_error());
				}
(down torward the bottom were it has to insert into player_buildings its not adding the cityid....everything else its adding like it should....)
this is how it looks in the player_buildings table:


id cityid userid buildingid buildingtype tagid
1 7 24 250 flag tag1
2 7 24 1 townhall tag2
3 7 24 21 warehouse tag3
5 0 27 250 flag tag1

(id's 1-3 i manually inserted and id 5 is what it did just after i registered a new account. so its adding everything except the cityid)

----------------------------------------------------second issue:----------------------------------------------------

this one a little hard to explain but ill try my best. ok this is the users table:

id username points mycities email password current_city
23 slav 5 1 ******** ********* 6
24 test 5 1 ******** ********* 7
25 house 5 1 ******** ********* 10
27 the 5 1 ******** ********* 0

here the problem is that its supposed to update the current_city field each time the player changes cities or when the player logs in, it goes to that current_city id #.(this is the city id) and so i registered that account with the username 'the' and it didnt change the current city. here is the coding behind this:

Code: Select all

<?php
include_once 'confff.php';
session_start();
(!isset($_SESSION['uid'])) ? header("Location: index.html") : "";
$uid = $_SESSION['uid'];
$_SESSION['uid'] = $uid;
	if(isset($_POST['city_change'])){
	mysql_query("UPDATE users SET current_city='$_POST[city_change]' WHERE id='$uid'");
	}
?>

<?php
//current city
$sql = mysql_query("SELECT current_city FROM users WHERE id='$uid'");
$city = mysql_fetch_array($sql);

	if($city['current_city'] < 1)
	{
	$sql = mysql_query("SELECT id FROM resources WHERE userid='$uid'");
	$city2 = mysql_fetch_array($sql);
	mysql_query("UPDATE users SET current_city='$city2' WHERE id='$uid'");
	$cityview = $city2['id'];
	}
		else {
		$cityview = $city['current_city'];
		}
		
?>

<link href="maintemplate.css" rel="stylesheet" type="text/css" />

<div id="backgroundpic">
<?php

echo "<img src='/images/part1.GIF'>";

?>
</div>

<div id="townview">
<?php

echo "<img src='/images/citytiles.png'>";

?>
</div>

<form method="post">
<div id= "cities">
<?php
$sql = mysql_query("SELECT cityname FROM resources WHERE id='$cityview'");
$cityname = mysql_fetch_array($sql);
	echo $cityname['cityname'];
?>
<select name="city_change">
<?php
//drop down box
$sql = mysql_query("SELECT * FROM resources WHERE userid='$uid'");
while($row = mysql_fetch_array($sql))
echo "<option value='" . $row['id'] . "'>" . $row['cityname'] . "</option>";
?>
</select>
<input type="submit" value="change city" />
</form>
</div>
hope to get some help as to what is wrong with those two issues soon. A thanks in advance :)

Re: two problems that are giving me a hard time

Posted: Sat Jan 29, 2011 5:59 pm
by Slav2
anyone?????

Re: two problems that are giving me a hard time

Posted: Sat Jan 29, 2011 6:28 pm
by Jackolantern
First issue: Echo out what is being stored in the cityid variable before trying to insert it, and ensure it doesn't have a value that is outside of the range for the column type in the database. Usually if you are missing only 1 value in an insert statement that is the problem, otherwise the entire query would fail.

Second issue: Store the value from the $_POST into a scalar variable (i.e. regular variable) before using it in a query statement. Sometimes PHP doesn't like it being used like that.

Re: two problems that are giving me a hard time

Posted: Sat Jan 29, 2011 7:47 pm
by Slav2
ok fixed first issue...but im not understanding how to fix the second one..please more info on that...

Re: two problems that are giving me a hard time

Posted: Sat Jan 29, 2011 8:17 pm
by Jackolantern
Instead of using $_POST['somevar'] in the actual query, do something like this:

Code: Select all

$newVar = $_POST['somevar'];
$sql = "SELECT * FROM users WHERE name='$newVar'";
I have been told the reason a couple of times why PHP doesn't like having associative arrays (the $_POST array) in parsed strings (the SQL query string), but I can never remember why, so I just remember to not use it that way. Try taking all $_POST arrays out of any of your query strings and use intermediary regular variables to hold the values and see if that fixes it or changes the error you get.

Re: two problems that are giving me a hard time

Posted: Sat Jan 29, 2011 9:56 pm
by Slav2
oh ok i understand what u mean...but thats not were the problem is coming from...its in this chunk of code:

Code: Select all

<?php
//current city
$sql = mysql_query("SELECT current_city FROM users WHERE id='$uid'");
$city = mysql_fetch_array($sql);

	if($city['current_city'] < 1)
	{
	$sql10 = mysql_query("SELECT id FROM resources WHERE userid='$uid'");
	$city2 = mysql_fetch_array($sql10);
	mysql_query("UPDATE users SET current_city='$city2' WHERE id='$uid'");
	$cityview = $city2['id'];
	}
		else {
		$cityview = $city['current_city'];
		}
		
?>

Re: two problems that are giving me a hard time

Posted: Sun Jan 30, 2011 12:35 am
by Jackolantern
Unfortunately, I don't really know enough about the application to tell what is going wrong in this logical bug. I have looked over it several times and nothing seems amiss. But I don't really know the specifics on how exactly the city changing mechanics work. My best suggestion is to echo out every variable you are working with and make sure they are in the ranges you expect. I use echo so much when debugging, and about 95% of the time it solves my problem. Another important tip would be to be sure to comment all of your code. This not only helps us know what segment of code is supposed to be doing what, but it will help you remember what it does in the future after a month or more. You will come back to it later and will not be able to make heads or tails of it.

Re: two problems that are giving me a hard time

Posted: Sun Jan 30, 2011 5:50 am
by Slav2
Hmm oh ok. That's some ry good advise. I'll surely take that info and start adding more comments and try out the echoing mechanism out. Thanks!

Re: two problems that are giving me a hard time

Posted: Sun Jan 30, 2011 7:04 am
by Jackolantern
No problem!

And echo out the variables after every step of processing in your script. Sometimes things can get messed up where you least expect it, and just 1 set of echos often isn't enough. Echo is your best friend in PHP debugging :)

Re: two problems that are giving me a hard time

Posted: Thu Feb 17, 2011 5:06 am
by hallsofvallhalla
did you ever fix this for i see one problem right off

Code: Select all

 $cityid = $search2['id'];
should be

Code: Select all

 $cityid = $search3['id'];