Jump to content

Archived

This topic is now archived and is closed to further replies.

quasiman

move mysql record with changes

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?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

you dont need all that code do this ok.

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

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
make me lol....................

what's wrong know lol.....................

Share this post


Link to post
Share on other sites
Well I'm just asking...is that a correct way to do it?  Am I missing anything?

Share this post


Link to post
Share on other sites
WHERE id='$id'

if its a website for members ok do this so that users info gets updataed ok.

or it's cool star 10/10


also dont forget this as you dont want sql injection 

if($_POST['submit']) {

code

}

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
no but add

add this above new data entry ok.
$takeaway_email=$_POST['takeaway_email'];
$takeaway_email=stripslashes($takeaway_email);

good luck mate.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.