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
https://forums.phpfreaks.com/topic/60481-solved-looping-question/
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());
} 

?> 

 

 

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'].')

                  ");

}

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

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());
} 

?> 

 

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";
?>

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')

                  ");

}

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));
?>

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.