Jump to content

Inserting value into database - problem.


TheKizz

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/210226-inserting-value-into-database-problem/
Share on other sites

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?

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?

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];
    }
  }
}
?>

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.