Jump to content

move mysql record with changes


quasiman

Recommended Posts

I have two mysql tables, and I'd like to copy the records from one to the other.  The first table has an email address field - "user@domain.com", but when it's being copied I want to shorten it down to just "user".

Any ideas?
Link to comment
Share on other sites

That works ok if all user email are only 4 char long

try this have not tested the code but this would be the general layout

[code]
$strcnt = strlen($email);
for ($i=0; $i <= $strcnt; $i++) {
$chkchar = (substr($email,$i,1))
if ($chkchar = "@") {$i = $strcnt;} else {$username = $username + $chkchar;}
}
[/code]
Link to comment
Share on other sites

Wow, thanks for the responses!

So would something like this work?

[code]$takeaway_email=(eregi_replace("@[a-z0-9\-]+\.[a-z0-9\-\.]+$","",$email));

mysql_query("INSERT INTO table_name2
VALUES (firstname, email,....)
FROM table_name1
VALUES (firstname, $takeaway_email,....)")
or die(mysql_error());
[/code]
Link to comment
Share on other sites

[quote author=redarrow link=topic=99687.msg392831#msg392831 date=1152219488]
select table // to get email address's.

put the new code // the code i provided.


insert to new table // insert new code in a diffrent table.

bobs your uncle new database got all names and no @whoever . coms

good luck.
[/quote]
Link to comment
Share on other sites

Ok....I like that you're making me work for this  ;)

[code]<?php

$dbhost = "localhost";
$dbuser = "mydbusername";
$mydbpass = "mydbpassword";
$dbtable1 = "1sttable";
$dbtable2 = "newtable";


$con = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($mydbase, $con);

//get the email address from the first table
$getmail = 'SELECT to_address FROM $dbtable1';
mysql_query($getmail) or die(mysql_error());

//get the other data that's important
$userdata = 'SELECT userlocation, yahooid,  FROM $dbtable1';
mysql_query($userdata) or die(mysql_error());

//take out the 'domain.com' part of the email address
$takeaway_email=(eregi_replace("@[a-z0-9\-]+\.[a-z0-9\-\.]+$","",$getmail));

//insert the username and other data into the 2nd table
$add_user = 'INSERT INTO $dbtable2 (username, userlocation, yahooid) VALUES('$takeaway_email', 'Portland', 'my_yahooid')';
mysql_query($add_user) or die(mysql_error());


?>[/code]
Link to comment
Share on other sites

I'll be using this as an include to the index page, so everytime the index is loaded this file runs with no output to the user that it's happened.

I don't really need to worry about sql injection for that do I?
Link to comment
Share on other sites

You need to worry about SQL injection if you are writing a value to your database which originates from a user-controlled source. These are $_GET, $_POST and $_COOKIE (GPC) and $_REQUEST (which gets all 3).

In these cases, if magic_quotes are off and not adding slashes automatically then you you need to addslashes().

Use stripslashes() when magic_quotes has added them but you want to display the $_POST['var'] etc on the page.

See http://www.php.net/get_magic_quotes_gpc


BTW, You can accomplish your update with
[code]
INSERT INTO table2 (a, b, email, c)
SELECT a, b, SUBSTRING(email, 1, INSTR(email,'@')-1), c FROM table1[/code]
where a,b,c are your other column names
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.