Jump to content

Having ISSET problem.


iStriide

Recommended Posts

I'm having a problem where when I click the "use" link it only displays my check message which is "Hey", but for some reason I have to click it again for my queries to work. Here's a pick: problem.png.

 

Here's the code:


<?php

if(isset($_SESSION['user'])){

include_once('connect.php');

$session = $_SESSION['user'];

echo "
<br/>
<div class='profile-general-stats'>
<table>

<th>Item Name</th><th>Description</th><th>Use/Sell</th>
";

$yo = "SELECT * FROM player_items WHERE Username = '$session' ORDER BY id ASC";
$check = mysql_query($yo) or die (mysql_error());
$num_of = mysql_num_rows($check);

if($num_of > 0){

while ($is = mysql_fetch_array($check)){

$item_id = $is['id'];
$item_name = $is['Name'];
$item_type = $is['Type'];
$item_value = $is['Value'];
$item_description = $is['Description'];
$item_sell_value = $is['Sell_Price'];

echo "

<tr>

<td align='left' style='padding: 5px; padding-left: 25px;'><strong>$item_name</strong></td>
<td align='left' style='padding: 5px;'>$item_description</td>
<td align='middle' style='padding: 5px;'><a href='?use&id=$item_id&name=$item_name&type=$item_type&value=$item_value'>Use</a> | <a href='#sell_$item_name' id='$item_id'>Sell</a></td>

</tr>

";

}

}else{

echo "
<tr>

<td align='left' style='padding: 5px; padding-left: 25px;'>No Items</td>
<td align='left' style='padding: 5px;'>No Items</td>
<td align='middle' style='padding: 5px;'>No Items</td>

</tr>
";

}

  echo "
</table>
</div>
";

if(isset($_GET['use'])){

echo "Hey";

$use_id = $_GET['id'];
$use_name = $_GET['name'];
$use_type = $_GET['type'];
$use_value = $_GET['value'];

if($use_type == "Credits"){ $use_type = "Current_Credits"; }

$check_item =  "SELECT id FROM player_items WHERE id = '".mysql_real_escape_string($use_id)."' AND Username = '".$_SESSION['user']."'";
$run_check = mysql_query($check_item) or die(mysql_error());
$num_check = mysql_num_rows($run_check);
$e = mysql_fetch_array($run_check);

$item_stat = "SELECT `".$use_type."` FROM stats WHERE Username = '".$_SESSION['user']."'";
$run_stat = mysql_fetch_array(mysql_query($item_stat)) or die (mysql_error());

$new_stat = $run_stat[''.$use_type.''];

if($num_check == 1){

$new_stat = $new_stat + $use_value;

mysql_query("UPDATE stats SET `".$use_type."` = '".mysql_real_escape_string($new_stat)."' WHERE Username = '".mysql_real_escape_string($_SESSION['user'])."'") or die(mysql_error());
mysql_query("DELETE FROM player_items WHERE id = '$use_id'") or die(mysql_error());
}else{
}

}else{
}

}else{

header("Location: index.php");

}

?>

Link to comment
Share on other sites

move the whole... if(isset($_GET['use'])) section to the top of your script. So it's processed first. Your code works, it just needs to be rearranged. You want to do any form processing first, then display the page so you get the 'latest' information from your database queries.

Link to comment
Share on other sites

move the whole... if(isset($_GET['use'])) section to the top of your script. So it's processed first. Your code works, it just needs to be rearranged. You want to do any form processing first, then display the page so you get the 'latest' information from your database queries.

 

Read this ^ .. You're basically updating the data after you've already displayed it, which is obviously not going to show the updated data until the next request.

Link to comment
Share on other sites

Here's the posted code 'fixed' to correct the order of the code on the page and to clean up and eliminate a lot of unnecessary code and passing values through the link -

 

<?php
session_start();

if(!isset($_SESSION['user'])){
header("Location: index.php"); // not logged in, go elsewhere
exit; // prevent the remainder of the code on this page from executing
}
include('connect.php');

// perform any actions/processing for the page request
if(isset($_GET['use'])){
$use_id = (int)$_GET['id']; // cast value as an integer to prevent sql injection/cheating
// get the data for the item requested
$query =  "SELECT * FROM player_items WHERE id = $use_id AND Username = '{$_SESSION['user']}'";
if(!$result = mysql_query($query)){
	// query failed, handle the error here...
	die(mysql_error());
} else {
	// query worked without any errors
	if(!mysql_num_rows($result)){
		// no matching row, user no longer has this item
		// do whatever processing you want for this condition here...
	} else {
		// user has the item
		$row = mysql_fetch_assoc($result); // get the data
		$use_type = $row['Type'];
		$use_value = $row['Value'];
		if($use_type == "Credits"){ $use_type = "Current_Credits"; } // you should really fix this by renaming the column to match the expected value
		mysql_query("UPDATE stats SET `$use_type` = `$use_type` + $use_value WHERE Username = '{$_SESSION['user']}'") or die(mysql_error());
		mysql_query("DELETE FROM player_items WHERE id = $use_id") or die(mysql_error());
	}
}
}

// produce and output the content for this page
echo "
<br/>
<div class='profile-general-stats'>
<table>
<tr><th>Item Name</th><th>Description</th><th>Use/Sell</th></tr>
";

$query = "SELECT * FROM player_items WHERE Username = '{$_SESSION['user']}' ORDER BY id ASC";
if(!$result = mysql_query($query)){
// query failed, handle the error here...
die(mysql_error());
} else {
// query worked without any errors
if(!mysql_num_rows($result)){
	// no matching rows
	echo "
		<tr>
			<td align='left' style='padding: 5px; padding-left: 25px;'>No Items</td>
			<td align='left' style='padding: 5px;'>No Items</td>
			<td align='middle' style='padding: 5px;'>No Items</td>
		</tr>
	";
} else {
	// at least one matching row
	while ($row = mysql_fetch_array($result)){
		$item_id = $row['id'];
		$item_name = $row['Name'];
		$item_type = $row['Type'];
		$item_value = $row['Value'];
		$item_description = $row['Description'];
		$item_sell_value = $row['Sell_Price'];
		echo "
			<tr>
				<td align='left' style='padding: 5px; padding-left: 25px;'><strong>$item_name</strong></td>
				<td align='left' style='padding: 5px;'>$item_description</td>
				<td align='middle' style='padding: 5px;'><a href='?use&id=$item_id'>Use</a> | <a href='#sell_$item_name' id='$item_id'>Sell</a></td>
			</tr>
		";
	}
}
}
echo "
</table>
</div>
";
?>

 

I'm pretty sure the 'automatically refresh' comment had to do with the need to click on the link again/refresh the page to get the updated database values to display, which will be be corrected by fixing the logic on the page as has been suggested, then quoted, and finally posted here...

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.