cloudll Posted October 12, 2011 Share Posted October 12, 2011 At the moment, if i want to update my database, im using an external page with the sql query, say its called update.php, and im including it in an invisible css box with something like this /index.php?site=homepage&hiddenbox=update. That seems to be working fine, however I have a lot of things which constantly get updated in my database so i have lots of pages to update various things. Is there a way to update from within the index.php page, maybe with an if statement if the update is dependant on say a number being less than 10? Any opinions on the best way to go about updating database would be greatly appreciated thanks. Quote Link to comment https://forums.phpfreaks.com/topic/248991-am-i-doing-this-the-best-way/ Share on other sites More sharing options...
titan21 Posted October 12, 2011 Share Posted October 12, 2011 This is probably just a question of style rather than whether its the right or wrong way to do it - can you post some code so we can give you an idea if there is a better way? Quote Link to comment https://forums.phpfreaks.com/topic/248991-am-i-doing-this-the-best-way/#findComment-1278730 Share on other sites More sharing options...
cloudll Posted October 12, 2011 Author Share Posted October 12, 2011 Sure. Its for a game, so I have a compass. Say i want to go east. I click east on my compass (the url is : index.php?hiddenbox=compass_east) and my compass_east.php contains. <?php $sql = "UPDATE game_character SET pos_y = (pos_y+25) WHERE id=1"; $statement = $dbh->prepare($sql); $statement->execute(); ?> and a lot of it is like that, when you click an attack you want to perform it loads an attack_name.php page which updates in the same way. I have about 50 pages now which all only have them 3 lines of code in, so wondered if there was another way. Quote Link to comment https://forums.phpfreaks.com/topic/248991-am-i-doing-this-the-best-way/#findComment-1278732 Share on other sites More sharing options...
Muddy_Funster Posted October 12, 2011 Share Posted October 12, 2011 why not put them all on the same page and call the sql you want dependant on the $_GET[] variable from the url. you could create a function for each, or use an array - or if your feeling really keen a mixture of both. Quote Link to comment https://forums.phpfreaks.com/topic/248991-am-i-doing-this-the-best-way/#findComment-1278746 Share on other sites More sharing options...
Psycho Posted October 12, 2011 Share Posted October 12, 2011 What makes you think you need to put the code within an invisible css box? You can have code that executes without having an output element. For example, let's say you have a form to add a record that POSTs back to the main page (this is not a working script, just a POC) <?php if(isset($_POST['name']) { $name = mysql_real_escape_string(trim($_POST['name'])); $query = "INSERT INTO users (name) VALUES('$name')"; $result = mysql_query($query); } ?> <html> <body> Welcome to the home page </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/248991-am-i-doing-this-the-best-way/#findComment-1278747 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.