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

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

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

Post 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 :)
Last edited by Slav2 on Sat Feb 19, 2011 4:05 am, edited 1 time in total.
Thanks,

Slav2
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

Re: two problems that are giving me a hard time

Post by Slav2 »

anyone?????
Thanks,

Slav2
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: two problems that are giving me a hard time

Post 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.
The indelible lord of tl;dr
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

Re: two problems that are giving me a hard time

Post by Slav2 »

ok fixed first issue...but im not understanding how to fix the second one..please more info on that...
Thanks,

Slav2
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: two problems that are giving me a hard time

Post 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.
The indelible lord of tl;dr
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

Re: two problems that are giving me a hard time

Post 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'];
		}
		
?>
Thanks,

Slav2
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: two problems that are giving me a hard time

Post 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.
The indelible lord of tl;dr
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

Re: two problems that are giving me a hard time

Post 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!
Thanks,

Slav2
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: two problems that are giving me a hard time

Post 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 :)
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: two problems that are giving me a hard time

Post 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'];
Post Reply

Return to “Advanced Help and Support”