mysql - php Array values dont get inported to DB -


i'm building browser game in order learn html,css,php , mysql. , wrote code below allow players build new buildings. table loads buildings, , work fine, insert db. i'm new have problem values should put db empty. $_session[user_id] works. not values array. think problem might dont know how values correctly..

so if me here appreciated , educational me! :)

function buildings() {     global $database;     global $player;     global $self_link;       // fetch buildings     $result = $database->query("select * buildings");     $buildings = array();     while($building = $database->fetch($result)) {         $buildings[$building['id']] = $building;     }      if(!empty($_post['build'])) {         $building_id = (int)$_post['building_id'];         $amount = $_post['amount'];          try {             if($player->money < $buildings[$building_id]['cost']) {                 throw new exception("you not have enough money build this!");             }              // purchase technique             $player->money -= $buildings[$building_id]['cost'] * $amount;             $database->query("insert player_buildings (owner_id, name, power_use, life, att_inf, att_veh, att_air, att_sea)                                      values ('$_session[user_id]', '$buildings[name]', '$power_use', '$life', '$att_inf', '$att_veh', '$att_air', '$att_sea')");             $player->update();              echo "debugg: buildings have been added.... hope..";         } catch (exception $e) {             echo $e->getmessage();         }      }     // display form      echo "<table style='width:900px;'>         <tr>             <th style='width:40% text-align:left;'>name</th>             <th style='width:50%;'>description</th>             <th style='width:5%;'>price</th>             <th style='width:5%;'>power usage</th>             <th style='width:5%;'>&nbsp;</th>         </tr>";         foreach($buildings $id => $building) {                echo "<tr>                 <td>{$building['name']}</td>                 <td>{$building['description']}</td>                 <td>{$building['cost']}</td>                 <td>{$building['power_use']}</td>                 <td>                         <form action='$self_link' method='post'>                         <input type='hidden' name='building_id' value='$id' />                         <input style='width:40px' type='number' name='amount' value='amount' />                         <input type='submit' name='build' value='build' />                     </form>                 </td>             </tr>";         }      echo "</table>"; } 

    $buildings[$building['id']] = $building;                ^^^^^^^^^^^^^^^      values ('$_session[user_id]', '$buildings[name]', '$power_[..snip..]                                               ^^^^ 

unless $building['id'] resolves literal word name, you're accessing undefined fields in array...

turn on error_reporting , display_errors confirm - if incorrect key, you'll undefined index warnings.

plus you're vulnerable sql injection attacks. , unless you've enabled exceptions in db library, you're assuming query never fail.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -