Jump to content

[SOLVED] Basic PHP & MYSQL question


ILYAS415

Recommended Posts

I am currently developing a game for someone but ive hit a dead end as im having strange trouble with one the the scripts. In this script I used the while command to echo multiple weapons inside a <select> tag.

When i run the script everything works, apart from the mysql query which doesnt work how its supposed to...

<?php

$playerweapons= mysql_query("SELECT * FROM weapons WHERE owner='$fetch->username'");

$number= mysql_num_rows($playerweapons);

if ($number == "0"){

echo "You have no bought weapons!";

exit();

}elseif ($number > "0"){ ?>

<select name=weapon style="background-color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;">

<?

while ($pw= mysql_fetch_object($playerweapons)){

?>

<option value="<? echo " $pw->name "; ?>"><? echo "$pw->name"; ?></option>

<? }

echo "</select>";

?>

<input type=submit style="background-color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;" name=equip value="Equip">

<? } ?>

<br>

You currently have a <? echo "$fetch->weapon"; ?> equipped!<br>

<?php

if (strip_tags($_POST['weapon']) && strip_tags($_POST['equip'])){

$wep=$_POST['weapon'];

$weapon= mysql_query("SELECT * FROM weapons WHERE `name`='$wep' AND `owner`='$username'");

$wepnum= mysql_num_rows($weapon);

$wp= mysql_fetch_object($weapon);

if ($wep == "0" | $wep == $fetch->weapon){

echo "Either you have this weapon equipped already or you chose an invalid weapon";

exit();

}

echo "Changes have succesfully been made! You are now wielding a $wep!<br>";

mysql_query("UPDATE users SET weapon='$wep' WHERE username='$username'");

}

?>

 

The bit at the end doesnt work.

mysql_query("UPDATE users SET weapon='$wep' WHERE username='$username'");

I didnt include the $username variable in the script but it is on the page though. The problem with the query is that when it runs the query, its sets the weapon to Nothing instead of the selected weapon name.

Link to comment
Share on other sites

  • 3 weeks later...

<?php
$playerweapons= mysql_query("SELECT * FROM weapons WHERE owner='$fetch->username'");
$number= mysql_num_rows($playerweapons);
if ($number == "0"){
echo "You have no bought weapons!";
exit();
}elseif ($number > "0"){
echo"<select name=weapon style=\"background-color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;\">";
while ($pw= mysql_fetch_object($playerweapons)){
echo"<option value=\"$pw->name\">$pw->name</option>";
}
echo "</select>";
?>
<input type=submit style="background-color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;" name=equip value="Equip">
<? } ?>


You currently have a <? echo "$fetch->weapon"; ?> equipped!

<?php
if (strip_tags($_POST['weapon']) && strip_tags($_POST['equip'])){
$wep=$_POST['weapon'];
$weapon= mysql_query("SELECT * FROM weapons WHERE `name`='$wep' AND `owner`='$username'");
$wepnum= mysql_num_rows($weapon);
$wp= mysql_fetch_object($weapon);
if ($wep == "0" | $wep == $fetch->weapon){
echo "Either you have this weapon equipped already or you chose an invalid weapon";
exit();
}
echo "Changes have succesfully been made! You are now wielding a $wep!
";
mysql_query("UPDATE users SET weapon='$wep' WHERE username='$username'");
}
?>

 

Should work :)

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.