Jump to content

[SOLVED] Using While Statement to Insert numbers


Colton.Wagner

Recommended Posts

I have 17659 entries that are all the same. There are only two fields in the mysql databse Products_id witch auto increments and categories_id witch is an int 10.

 

This source code throws no errors.

 

<?php
error_reporting(E_ALL);
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
	die('Connection Error:' . mysql_error());
}
	mysql_select_db('catgories', $con);
	$result = mysql_query("SELECT * FROM categories");
	$query = "INSERT INTO categories ('products_id', 'categories_id') VALUES ('', '18')";
	$i = 1;

	while ($i <= 17659) {
		mysql_query($query);
		$i++;
	}
?>

Link to comment
Share on other sites

Good for you... are you trying to say that it's not working?

 

If products_id is auto increment and caegories_id is int as you say then you could try this...

 

$query = "INSERT INTO categories ('categories_id') VALUES (18)";

Omit the products_id it's not required on an auto increment field and some versions of MySQL do not like recieving a blank string as a value for auto increment fields. I also removed the single quotes from around categories_id since it is an integer not a string.

Link to comment
Share on other sites

I'm confused. 

Are you wanting to put 1-17659 for each categoryid pulled from the database?

 

something like this

productid    categoryid

1                  1

2                  1

3                  1

..to 17659

1                  2

2                  2

3                  2

..to 17659

 

 

Link to comment
Share on other sites

Good for you... are you trying to say that it's not working?

 

If products_id is auto increment and caegories_id is int as you say then you could try this...

 

$query = "INSERT INTO categories ('categories_id') VALUES (18)";

Omit the products_id it's not required on an auto increment field and some versions of MySQL do not like recieving a blank string as a value for auto increment fields. I also removed the single quotes from around categories_id since it is an integer not a string.

Didn't help and that shouldn't effect it anyways.

Link to comment
Share on other sites

I'm confused. 

Are you wanting to put 1-17659 for each categoryid pulled from the database?

 

something like this

productid    categoryid

1                  1

2                  1

3                  1

..to 17659

1                  2

2                  2

3                  2

..to 17659

 

Yes,

products_id  categories

1                    76

2                    76

3                    76

4                    76

to 17659..

Link to comment
Share on other sites

This should do it. There's probably a more efficient way, but this should work.

error_reporting(E_ALL);
$con = mysql_connect('localhost', 'root', '');
   if (!$con) {
      die('Connection Error:' . mysql_error());
   }
mysql_select_db('catgories', $con);
      
for($i=0;$i<=17659;$i++)
{
$query="INSERT INTO categories(`products_id`,`categories_id`) select $i,categories_id from categories"; 
mysql_query($query);
}

Link to comment
Share on other sites

This should do it. There's probably a more efficient way, but this should work.

error_reporting(E_ALL);
$con = mysql_connect('localhost', 'root', '');
   if (!$con) {
      die('Connection Error:' . mysql_error());
   }
mysql_select_db('catgories', $con);
      
for($i=0;$i<=17659;$i++)
{
$query="INSERT INTO categories(`products_id`,`categories_id`) select $i,categories_id from categories"; 
mysql_query($query);
}

 

Nope it didn't work with no errors. I'm telling you I couldn't figure it out.

Link to comment
Share on other sites

Add display_errors(1); below error_reporting and change your query call to

 

mysql_query($query) or trigger_error("SQL: $query, ERROR: " . mysql_error(), E_USER_ERROR);

 

Edit: oops forgot to add the actual error.

Link to comment
Share on other sites

What message do you get when using...

 

$query = "INSERT INTO categories (categories_id) VALUES (18)";

 

I'm not saying the functionality will work as you intend but it shouldn't throw an error.

Edit: The single quotes around the field names would have thrown an error, I just realised I left that in when I last posted this line.

Link to comment
Share on other sites

Is this the entire code? If nothing it happening when the code is run you should almost certainly be getting an error message. Have you tried using view source to see what if any HTML is written to the browser?

cags: the same error

The html document contains an error now because we set it to tell us.

 

Fatal error: SQL: INSERT INTO categories(`products_id`,`categories_id`) select 0,categories_id from categories, ERROR: in F:\Xampp\xampplite\htdocs\categories.php on line 13

Link to comment
Share on other sites

You need to add the mysql_error() part in that I added with an edit, so that we can see the reason mysql is giving.

 

mysql_query($query) or trigger_error("SQL: $query, ERROR: " . mysql_error(), E_USER_ERROR);

Link to comment
Share on other sites

Well I'm not sure what to say. You obviously haven't changed it as suggested because it's still outputting the SQL statement suggested by taquitosensei...

 

$query = "INSERT INTO categories (categories_id) VALUES (18)";

Link to comment
Share on other sites

gonna give the thread starter a heads up on how to get help...

 

this...

I have 17659 entries that are all the same. There are only two fields in the mysql databse Products_id witch auto increments and categories_id witch is an int 10.

 

This source code throws no errors.

means nothing to me, and 99% of the rest of the people on the board.

 

next time, be very descriptive as to what the problem is, the errors that you are receiving, and what you are trying to acheive .. because right off the bat, you lose over 50% of the people who are capable of creating a solution very quickly, like myself.

 

and i'm not going to read all the other posts in the thread just to try and figure out YOUR problem.

 

i love to help, but i only have so much time, and that time was just spent explaining to you how to get what you came for .. a solution.

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.