Jump to content

update sql, php troubleshooting


KillerBeeZ

Recommended Posts

been looking over this script, which is part of a larger script, and so far I can't figure out why it is not saving the data to the sql database. It seems like some small stupid thing I'm overlooking but if so I cannot seem to find it... it should take 4 values, manipulate them, then average them out and save the result to sql, that result (there are 10 passes to this script) is then added upon itself  until in the end there are a few fairly large numbers. This is for a racing game $residual1 2 3 and 4 all do end up with data, the problem is its not stacking due to the fact that its not saving to sql

 

<?
$residual1 = mysql_result($resultd,0,"s1-1st");
$residual2 = mysql_result($resultd,0,"s1-2nd");
$residual3 = mysql_result($resultd,0,"s1-3rd");
$residual4 = mysql_result($resultd,0,"s1-4th");

$powcal1 = ($car1stat_pow - (($tpow) - 15) + rand(1, 30));
$hancal1 = ($car1stat_han - (($than) - 15) + rand(1, 30));
$acccal1 = ($car1stat_acc - (($tacc) - 15) + rand(1, 30));
$brkcal1 = ($car1stat_brk - (($tbrk) - 15) + rand(1, 30));
$totcal1 = (($powcal1 + $hancal1 + $acccal1 + $brkcal1) / 4);
$residual1 = ($residual1 + $totcal1);
$resid1 = "UPDATE racedata SET s1-1st = '$residual1' ";
mysql_query($resid1);

$powcal2 = ($car2stat_pow - (($tpow) - 15) + rand(1, 30));
$hancal2 = ($car2stat_han - (($than) - 15) + rand(1, 30));
$acccal2 = ($car2stat_acc - (($tacc) - 15) + rand(1, 30));
$brkcal2 = ($car2stat_brk - (($tbrk) - 15) + rand(1, 30));
$totcal2 = (($powcal2 + $hancal2 + $acccal2 + $brkcal2) / 4);
$residual2 = ($residual2 + $totcal2);
$resid2 = "UPDATE racedata SET s1-2nd = '$residual2' ";
mysql_query($resid2);

$powcal3 = ($car3stat_pow - (($tpow) - 15) + rand(1, 30));
$hancal3 = ($car3stat_han - (($than) - 15) + rand(1, 30));
$acccal3 = ($car3stat_acc - (($tacc) - 15) + rand(1, 30));
$brkcal3 = ($car3stat_brk - (($tbrk) - 15) + rand(1, 30));
$totcal3 = (($powcal3 + $hancal3 + $acccal3 + $brkcal3) / 4);
$residual3 = ($residual3 + $totcal3);
$resid3 = "UPDATE racedata SET s1-3rd = '$residual3' ";
mysql_query($resid3);

$powcal4 = ($car4stat_pow - (($tpow) - 15) + rand(1, 30));
$hancal4 = ($car4stat_han - (($than) - 15) + rand(1, 30));
$acccal4 = ($car4stat_acc - (($tacc) - 15) + rand(1, 30));
$brkcal4 = ($car4stat_brk - (($tbrk) - 15) + rand(1, 30));
$totcal4 = (($powcal4 + $hancal4 + $acccal4 + $brkcal4) / 4);
$residual4 = ($residual4 + $totcal4);
$resid4 = "UPDATE racedata SET s1-4th = '$residual4' ";
mysql_query($resid4);
?>

Link to comment
Share on other sites

All your variables within your queries need to be concatenated. Whats being stored in your database is the literal string "$whatever".

 

You're query string is no different from a normal string if you want to think of it like that.

 

x = "blah";
echo "something = '$x' somethingelse";
//returns something = '$x' somethingelse
echo "something = {$x} somethingelse";
//returns something = blah somethingelse;
//you can also concatenate like this
echo "blah" . $someVar . "blah blah";
//both work

Link to comment
Share on other sites

The variable in the query string is just fine the way it's posted in the OP. I suspect the problem is being caused by the hyphens in your field names being interpreted as mathematical operators. Add or die( mysql_error() ) to the query execution and see what errors are returned.

Link to comment
Share on other sites

I saw that last night but the board was down or something and couldn't post. I just removed the - in the whole darn thing. Curious though, could I also just use an escape \? such as

 

$residual1 = mysql_result($resultd,0,"s1\-1st");

 

Also, on another note, I'm trying to insert an image where the url is stored in sql.

 

$qcar1image = "SELECT image FROM cars WHERE year = '$a1yr' AND make = '$a1mk' AND model = '$a1md'";
$car1image = mysql_query($qcar1image);

 

getting that old Resource id #13 and such errors.

 

All the variables are filled with the correct data so it makes me think I have a syntax error

Link to comment
Share on other sites

ok it might be that the url has a - in it. I need to leave it like it is however, is there a way in php to call that url as it is without any kind of need for escape or changing the images?

 

http://www.ctlinx.com/piwigo/upload/2012/05/24/20120524171350-684011fb.jpg

 

I cannot ask the people entering in the data to use a \ in the address, they won't understand

 

in C# you could do this with @ I think

Link to comment
Share on other sites

I'll try

 

This is the section of the race engine file in question

 

<table width="100%"  border="1" cellspacing="2" cellpadding="2">
  <tr>
    <th><div align="center">Rank</div></th>
    <th><div align="center">Image</div></th>
    <th>Player, Car, Score</th>
  </tr>
  <tr>
<?
	$qcar1image = "SELECT image FROM cars WHERE year = '$a1yr' AND make = '$a1mk' AND model = '$a1md'";
	$qcar2image = "SELECT image FROM cars WHERE year = '$a2yr' AND make = '$a2mk' AND model = '$a2md'";
	$qcar3image = "SELECT image FROM cars WHERE year = '$a3yr' AND make = '$a3mk' AND model = '$a3md'";
	$qcar4image = "SELECT image FROM cars WHERE year = '$a4yr' AND make = '$a4mk' AND model = '$a4md'";
	$car1image = mysql_query($qcar1image);
	$car2image = mysql_query($qcar2image);
	$car3image = mysql_query($qcar3image);
	$car4image = mysql_query($qcar4image);

	$ranks = array("<div align=\"center\"><img src=\"$car1image\"width=\"50\" height=\"50\"></div></p></td><td>$car1player in the $a1yr $a1mk $a1md" => "$residual1", "<div align=\"center\"><img src=\"$car2image\"width=\"50\" height=\"50\"></div></p></td><td>$car2player in the $a2yr $a2mk $a2md" => "$residual2", "<div align=\"center\"><img src=\"$car3image\"width=\"50\" height=\"50\"></div></p></td><td>$car3player in the $a3yr $a3mk $a3md" => "$residual3", "<div align=\"center\"><img src=\"$car4image\"width=\"50\" height=\"50\"></div></p></td><td>$car4player in the $a4yr $a4mk $a4md" => "$residual4");
	arsort($ranks);
	$ri = 1;
	foreach ($ranks as $key => $val) 
	{
		echo "</tr><td><div align=\"center\">$ri</div></td>";
		echo "<td>$key = $val\n</td><tr>";
		$ri++;
	}
		mysql_close();
?>
  </tr>
</table>

 

I know there is a better way to get the sql data than to use 4 different queries, but this is only a draft while I learn php. My native language is C# and never got around to working with sql. I will clean up the code once it is working properly.

 

This is the row from the sql database I'm trying to call (I apologize, I will likely never get it to line up correctly on here)

 

year 	make 	model 	class 	cost 	image 	description 	bodytrim 	horsepower 	topspeed 	cd 	height 	width 	weight 	hwympg 	citympg 	pow 	han 	acc 	brk 	acnt
2003 	Dodge 	Grand Caravan SE fwd 	D 	4550 	http://www.ctlinx.com/piwigo/upload/2012/05/24/201... 	a mini van with a good bit of acceleration and pow... 	Van 	180 	109 	0.35 	68.9 	78.6 	3991 	24 	17 	50 	30 	65 	14 	KillerBeeZ

 

This is the section that calls the data from yet another sql database that is needed in order to know which car to select...

 

	$co = 0;
$car1player = mysql_result($resultr,$co,"player");
$car1vin = mysql_result($resultr,$co,"vin");
$car1stat_pow = mysql_result($resultr,$co,"totpow");
$car1stat_han = mysql_result($resultr,$co,"tothan");
$car1stat_acc = mysql_result($resultr,$co,"totacc");
$car1stat_brk = mysql_result($resultr,$co,"totbrk");
$a1yr = mysql_result($resultr,$co,"year");
$a1mk = mysql_result($resultr,$co,"make");
$a1md = mysql_result($resultr,$co,"model");

 

all of that gets its data from this row in sql

 

player 		vin 		year 	make 	model 			engine-vin 	trans-vin 	weightmod-vin 	aero-vin 	totpow 	tothan 	totacc 	totbrk
KillerBeeZ 	1234567890 	2000 	Dodge 	Grand Caravan SE fwd 	oem12345 	oem12345 	oem12345 	12345 		50 	30 	87 	14

 

If I forgot something just let me know and I'll add it too. There really is a lot of code to go over

Link to comment
Share on other sites

http://www.ctlinx.com/org/panprix/races/raceengine.php

 

this is the test page I made to debug this issue... if that helps at all. The race will start when the pending race hits the current time and the page will refresh every minute (or refresh manually) The images of the cars are entered manually but you will see the (resource id #**) error in the line below the pending races and above the current race (if a current race is active) will look like "Resource id #19 , 2000 , Dodge , Grand Caravan SE fwd " when a race is current or "Resource id #13 , , , " when its not active. I'd really like to find the answer to this, I know you guys (and gals) are busy and have better things to do than help others solve their problems, but I'd be very grateful for any insight you can offer.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.