Jump to content

INSERT INTO


dreamwest

Recommended Posts

im just learning mysql and im trying to use variables in the "INSERT INTO" command

 

can anyone tell me what im doing wrong here:

 

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM example")
or die(mysql_error());

echo "ok";

// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );

mysql_query("INSERT INTO example_2 
(column1, column2) VALUES($row['column1'], $row['column2']) ") 
or die(mysql_error());

echo "ok 2";

 

Im basically just transferring:

 

Table: example
Columns: column1 and column2

TO:

Table: example2
Columns: column1 and column2

 

any help would be appreciated......

Link to comment
Share on other sites

mysql_query("INSERT INTO example_2
(column1, column2) VALUES($row['column1'], $row['column2']) ") 

I think it's a problem with your $row['column1/2']; mysql values need to be in quotes.

 

mysql_query("INSERT INTO example_2 (column1, column2)
VALUES 
('".$row['column1']."', '".$row['column2']."'));

Link to comment
Share on other sites

mysql_query("INSERT INTO example_2
(column1, column2) VALUES($row['column1'], $row['column2']) ") 

I think it's a problem with your $row['column1/2']; mysql values need to be in quotes.

 

mysql_query("INSERT INTO example_2 (column1, column2)
VALUES 
('".$row['column1']."', '".$row['column2']."'));

 

Thanks that was it!

 

Can i ask one more question about the same script...

 

I have 100 rows in column1, how can i make this transfer all rows over instead of just one.

 

Ive tried the while command but no luck:

 

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM example")
or die(mysql_error());

echo "ok";

// store the record of the "example" table into $row
while($row = mysql_fetch_array($result))

{

mysql_query("INSERT INTO example_2 (column1, column2)
VALUES 
('".$row['column1']."', '".$row['column2']."'));
or die(mysql_error());

}

echo "ok2";

Link to comment
Share on other sites

I have 100 rows in column1, how can i make this transfer all rows over instead of just one.

What are you trying to accomplish???

 

Im trying to seperate the 2 tables just in case i screw something up.

 

Once i have the info in example2 table i can use another script to work of that table instead of the example table.

 

Of course example2 table will have extra columns so this will make it easier when writing scripts

Link to comment
Share on other sites

Im trying to seperate the 2 tables just in case i screw something up.

 

Once i have the info in example2 table i can use another script to work of that table instead of the example table.

 

Of course example2 table will have extra columns so this will make it easier when writing scripts

 

I scrapped this idea and just addedd the extra columns to the original table

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.