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
https://forums.phpfreaks.com/topic/48110-solved-basic-php-mysql-question/
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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.