Jump to content

[SOLVED] looping question


Custer

Recommended Posts

I currently working on a loop, which I know is selecting the information correctly, but I can't seem to put the insert commands in.

 

$num=rand(1, 20);
$result = mysql_query("SELECT * FROM initialstats WHERE id='$num'");
$initial_stats = mysql_fetch_assoc($result);

while($info = mysql_fetch_assoc( $result )) 
{  
INSERT INTO user_resources (user_id, restype, resamount)
VALUES('$id', '.$info['restype'] .', '.$info['resamount'].');
} 

 

This is waht I'm trying to do..but it isn't inserting anything.

 

But, when I try this code:

 

$num=rand(1, 20);
$result = mysql_query("SELECT * FROM initialstats WHERE id='$num'");
$initial_stats = mysql_fetch_assoc($result);

while($info = mysql_fetch_assoc( $result )) 
{  
Print "<th>id:</th> ".$info['id'] . ""; 
Print "<th>restype:</th> <td>".$info['restype'] . ""; 
Print "<th>Market Sell:</th> ".$info['resamount'] . ""; 
} 

 

It displays the array of the information, and it is all there, so how do I go from showing the information to inserting all of it?

Link to comment
Share on other sites

You have to actually execute the command with mysql_query().

 

<?php

$num=rand(1, 20);
$result = mysql_query("SELECT * FROM initialstats WHERE id='$num'");
//$initial_stats = mysql_fetch_assoc($result); <---Why do you have this line?

while($info = mysql_fetch_assoc( $result )) 
{  
$query = "INSERT INTO user_resources (user_id, restype, resamount)
VALUES('$id', '{$info['restype']} ', '{$info['resamount']}'";

mysql_query($query)or die(mysql_error());
} 

?> 

 

 

Link to comment
Share on other sites

try dis 1....

 

$num=rand(1, 20);

$result = mysql_query("SELECT * FROM initialstats WHERE id='$num'");

$initial_stats = mysql_fetch_assoc($result);

 

while($info = mysql_fetch_assoc( $result ))

mysql_query(" INSERT INTO user_resources (user_id, restype, resamount)

                    VALUES('$id', '.$info['restype'] .', '.$info['resamount'].')

                  ");

}

Link to comment
Share on other sites

Yes, I had tried that one before:

 

mysql_query(" INSERT INTO user_resources (user_id, restype, resamount)
                    VALUES('$id', '.$info['restype'] .', '.$info['resamount'].')
                   ");

 

And still didn't get any luck..:s

Link to comment
Share on other sites

Yes, just did and was given this:

 

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

 

but line 2 of my register script is this:

 

include ('configure.php');

 

and that was never a problem before....

Link to comment
Share on other sites

That error just means there is a problem with your syntax in your query. I accidentally missed a closed parenthesis.

 

<?php

$num=rand(1, 20);
$result = mysql_query("SELECT * FROM initialstats WHERE id='$num'");
//$initial_stats = mysql_fetch_assoc($result); <---Why do you have this line?

while($info = mysql_fetch_assoc( $result )) 
{  

$query = "INSERT INTO user_resources (user_id, restype, resamount)
VALUES('$id', '{$info['restype']}', '{$info['resamount']}')";

mysql_query($query)or die(mysql_error());
} 

?> 

 

Link to comment
Share on other sites

Nope, it's right...Here's the entire code, before I added the loop, everything worked perfectly, the buildings inserted with the correct ID, but now they won't even work...

 

<?php
include ('configure.php');
mysql_connect ("$dbhost", "$dbuser", "$dbpass")or die("Could not connect: ".mysql_error());
mysql_select_db("$dbname") or die(mysql_error());

include 'register.html';   
$email = $_POST['email'];    
$username = $_POST['username'];
$password = md5($_POST['password']);

$username = mysql_escape_string($username);
$email = mysql_escape_string($email);

// lets check to see if the username already exists

$checkuser = mysql_query("SELECT username FROM users WHERE username='$username'"); 

$username_exist = mysql_num_rows($checkuser);

if($username_exist == 1) {
    echo "I'm sorry but the username you specified has already been taken.  Please pick another 

one.";
    unset($username);
    include 'register.html';
    exit();
} 

// lets check to see if the username already exists

$checkemail = mysql_query("SELECT email FROM users WHERE email='$email'"); 

$email_exist = mysql_num_rows($checkemail);

if($email == 1) {
    echo "I'm sorry but the email you specified has already been taken.  Please pick another 

one.";
    unset($email);
    include 'register.html';
    exit();
} 

// lf no errors present with the username
// use a query to insert the data into the database.

mysql_query("INSERT INTO users (id, email, username, password)
VALUES('$id', '$email', '$username', '$password')");


//random id
$num=rand(1, 20);
$result = mysql_query("SELECT * FROM initialstats WHERE id='$num'");
$initial_stats = mysql_fetch_assoc($result); 

while($info = mysql_fetch_assoc( $result )) 
{  

$query = "INSERT INTO user_resources (user_id, restype, resamount)
VALUES('$id', '{$info['restype']}', '{$info['resamount']}')";

mysql_query($query)or die(mysql_error());
} 



//defines variables
$building_id = $initial_stats['building_id'];
$restype = $initial_stats['restype'];
$resamount = $initial_stats['resamount'];

//fetches user_id
$query = mysql_query("SELECT id FROM users WHERE username = '".$username."'");
$users = mysql_fetch_assoc($query);
$id = $users['id'];


//insertion to user_buildings
mysql_query("INSERT INTO user_buildings (user_id, building_id, amount)
VALUES('$id', '$building_id', '1')");

mysql_close ();

echo "You have successfully Registered";
?>

Link to comment
Share on other sites

bwt dis 1....

 

$num=rand(1, 20);

$result = mysql_query("SELECT * FROM initialstats WHERE id='$num'");

$initial_stats = mysql_fetch_assoc($result);

 

while($info = mysql_fetch_assoc( $result ))

{

$type = $info['restype'];

$amount = $info['resamount'];

mysql_query(" INSERT INTO user_resources (user_id, restype, resamount)

                    VALUES('$id', '$type', '$amount')

                  ");

}

Link to comment
Share on other sites

I recommend that you use something like this instead of doing all those queries you can do what you want with just one query.

 

<?php
$insert = '';
while ($info = mysql_fetch_assoc($result))
{
$type = $info['restype'];
$amount = $info['resamount'];
$insert .= "
	('$id', '$tpe', '$amount'),";
}

mysql_query("
INSERT INTO user_resources
	(user_id, restype, resamount)
        VALUES" . substr($insert, 0, -1));
?>

Link to comment
Share on other sites

clearstat, I just tried your code, and it acts like it's completely working, but alas, it's not inserting anything into user_resources...

 

Jayb...what would I put in the $insert label? Because I'm having several things going on in this code...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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