TheKizz Posted August 9, 2010 Share Posted August 9, 2010 Hi, Sorry guys i'm kind of new to PHP, and I need help inserting a value into the database, I have the following code: if($_POST['option1'] == 1) { // You have attempted to jack her. $failed = "You have failed to jack her"; $success = "You have successfully jacked her, and recieved £".$random_number." from the jacking"; $successorfail = array($failed,$success); shuffle($successorfail); if($successorfail = $success){ srand ((double) microtime( )* 100000000); $random_number = rand(0,400); $connect = mysql_connect("localhost","root","") or die("<center>Did not connect to database.</center>"); mysql_select_db("thegame") or die("<center>Couldn't find the database.</center>"); $query = mysql_query('INSERT INTO users (money) VALUE($random_number) WHERE username = "'.$_SESSION['username'].'"'); mysql_query($query); echo $successorfail[0]."."; } else die ($failed); } What the code is supposed to do is when the first radio button is checked and the submit button is clicked, it should randomly select if the action "jacking or robbing" has failed or succeeded, if it has succeeded it should give a random number between 0 and 400, and add it into the database to a field called 'money' and display a message in the browser saying 'You have successfully jacked her.... etc', and if it has failed then just display the message 'You have failed to jack her.. etc'. But with the code I already have, this isn't working and I don't know why. Can anybody help? - Kiran. Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/ Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 get rid of the mysql_query($query) line. Also, are you receiving any errors? Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097067 Share on other sites More sharing options...
TheKizz Posted August 9, 2010 Author Share Posted August 9, 2010 I just removed that line, and yes I am. It just displays: Y. When I select the radiobox and click submit. Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097068 Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 Your check isn't actually doing anything. if($successorfail == $success) doesn't make sense. it should be $successorfail[0] == $success The first way will ALWAYS evaluate to true because you are assigning $success to $successorfail as opposed to performing a boolean check. Is the database being updated? Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097071 Share on other sites More sharing options...
TheKizz Posted August 9, 2010 Author Share Posted August 9, 2010 Okay, thanks i've done that. But it still doesn't work, it says I have been successful but it doesn't display or give an amount. You have successfully jacked her, and recieved £ from the jacking. That's what gets displayed. Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097073 Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 That is happening because you are using $random_number before it is actually initialized. If you move your $random_number = rand(0,400); line above the $success line you should be good. Also, is the database being updated with the data even if it is not being displayed? Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097075 Share on other sites More sharing options...
TheKizz Posted August 9, 2010 Author Share Posted August 9, 2010 Okay, i've done that. It displays the string correctly and amount. No, but it doesn't update the database data. Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097077 Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 I've re-written your code to make things a little easier to follow. If this doesn't work, make sure $_SESSION['username'] is being set. <?php if($_POST['option'] == 1) { $amount = rand(0,400); $check = array( 'You have failed to jack her', 'You have successfully jacked her and receieved £'.$amount.' from the jacking' ); if(rand(0,1) == 1) { $connect = mysql_connect("localhost","root","") or die("<center>Did not connect to database.</center>"); mysql_select_db("thegame") or die("<center>Couldn't find the database.</center>"); $sql = 'update users set money = money+'.$amount.' where username = "'.$_SESSION['username'].'"'; if(mysql_query($sql)) { echo $check[1]; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097079 Share on other sites More sharing options...
TheKizz Posted August 9, 2010 Author Share Posted August 9, 2010 Ahh thanks, it works now. Although I don't get this one little bit, what does this mean? if(rand(0,1) == 1) { That part..? Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097084 Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 Based on the $check array, 0 is fail, 1 is success. So I generate either 0 or 1 randomly, and if it's 1 then we proceed with the script. Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097086 Share on other sites More sharing options...
TheKizz Posted August 9, 2010 Author Share Posted August 9, 2010 Alright, thanks so much man. Great help. Quote Link to comment https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/#findComment-1097090 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.