Jump to content

Recommended Posts

Hello everyone :) hows everyone's night.. I'm coding this page basically a 'vault', storing inventory. Basically in this vault, i'm using Javascript  to act like a checkbox (so when you click the images) it checks off the item. The end result is, your supposed to be able to re-ward these inventory items to other members in you're group. Now, i'm running into a problem with the reward aspect. I get all the inventory ID'S to show up (using an onmousover as my test to make sure the id's are correct) however; during my award, i can't seem to get it to run properly. :( I've rescripted it 4 times, nothing will work.

 

<?php
<?php

if($_POST["submit"] == "Award")
{
$sec_answer = mysql_real_escape_string($_POST['answer']);
$user_answer = $stat[sec_answer];
if ($sec_answer != $user_answer)
{
	echo "<script>alert(\"Your security answer did not match. Please try again.\");</script>";
}
else
{
	$count = 0;
	$selected_item = mysql_fetch_array($_POST['v_selected_$count']);
	$award = mysql_fetch_array ( mysql_query ( "SELECT * FROM user_equipment WHERE crew = $stat[crewid]" ) );
	$award_itemname = $award['name'];
	$award_itemid = $award['id'];
	if($selected_item == $award_itemid)
	{
		/* lazy fetch just to see if it works */
		$award_username = mysql_real_escape_string($_POST['username']);
		$user = mysql_fetch_array ( mysql_query ( "SELECT * FROM users WHERE id = '$award_username'") );
		$username = $user['name'];
		$userid = $user['id'];
		$date = date("M/d/Y g:iA");
		mysql_query("insert into award_log (crew, log, date) values($stat[crewid],'<font color=#949477>$stat[name] Gave ".$award_itemname." to ".$username.".</font>','".$date."')");
		mysql_query("UPDATE user_equipment SET owner = '$userid', crew='0' where id = '$award_itemid'") or die ( mysql_error ());
		echo "Item Awarded.";
	}
	else
	{
		echo "something wen't wrong.";
	}
		$count++;
}

}

 

 

what ends up happening is, it chooses the wrong item being awarded.

 

my checkbox name is 'v_selected_$count' the value is $itemid. cannot get it to work :/ help please would be great, some type of guidence, someone work with me, i'd love it! hehe

Link to comment
https://forums.phpfreaks.com/topic/268489-stuck-on-something-2nd-eye-please/
Share on other sites

Name your checkbox v_selected[] that way on the PHP side you have an array.  That will make it easier to process.

 

Also $_POST['v_selected_$count'] is not going to work right.  Variables are not translated when inside single-quoted strings so you are looking for a posted item named quite literally 'v_selected_$count' rather than 'v_selected_0' as you intend.

 

I re-modified my code a bit

 

<?php

if($_POST["submit"] == "Award")
{
$sec_answer = mysql_real_escape_string($_POST['answer']);
$user_answer = $stat[sec_answer];
if ($sec_answer != $user_answer)
{
	echo "<script>alert(\"Your security answer did not match. Please try again.\");</script>";
}
else
{
	$count = 0;
	$crewid = $stat['crewid'];
	$crew_vault_award_result = mysql_query("select * from user_equipment where crew = '$crewid' order by `name` ASC")or die(mysql_error());
	while($row = mysql_fetch_array($crew_vault_award_result))
	{
		$item_name = $row["name"];
		$item_id = $row["id"];
		$award_target_id = mysql_real_escape_string($_POST["username"]);
		$award_name_result = mysql_query("select * from `users` where id = $award_target_id")or die(mysql_error());
		$award_user = mysql_result($award_name_result,0,"name");
		if($_POST["v_selected_$count"] == "$item_id")
		{
			$date = date("M/d/Y g:iA");
			mysql_query("insert into award_log (crew, log, date) values($stat[crewid],'<font color=#949477>$stat[name] Gave ".$item_name." to ".$award_user.".</font>','".$date."')");
			mysql_query("UPDATE user_equipment SET owner = '$_POST[username]', crew='0' where id = '$item_id'") or die ( mysql_error ());
			echo "succeed.";
		} 
		else 
		{ 
			echo "error."; 
		}
		$count++;
	}	
}
}

You'd need a condition where if the item is not selected, do not do anything.

 

If you change your variable to an array as I mentioned before it'll make the process easier because you could just loop the array of selected items rather than have to loop over everything.

 

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.