quasiman Posted July 6, 2006 Share Posted July 6, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/ Share on other sites More sharing options...
redarrow Posted July 6, 2006 Share Posted July 6, 2006 substr($email,0,4);update new table ok. Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54012 Share on other sites More sharing options...
jvrothjr Posted July 6, 2006 Share Posted July 6, 2006 That works ok if all user email are only 4 char longtry 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] Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54018 Share on other sites More sharing options...
redarrow Posted July 6, 2006 Share Posted July 6, 2006 you dont need all that code do this ok.$takeaway_email=(eregi_replace("@[a-z0-9\-]+\.[a-z0-9\-\.]+$","",$email)); Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54036 Share on other sites More sharing options...
quasiman Posted July 6, 2006 Author Share Posted July 6, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54134 Share on other sites More sharing options...
redarrow Posted July 6, 2006 Share Posted July 6, 2006 [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 . comsgood luck.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54140 Share on other sites More sharing options...
quasiman Posted July 7, 2006 Author Share Posted July 7, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54548 Share on other sites More sharing options...
redarrow Posted July 7, 2006 Share Posted July 7, 2006 make me lol....................what's wrong know lol..................... Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54568 Share on other sites More sharing options...
quasiman Posted July 7, 2006 Author Share Posted July 7, 2006 Well I'm just asking...is that a correct way to do it? Am I missing anything? Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54570 Share on other sites More sharing options...
redarrow Posted July 7, 2006 Share Posted July 7, 2006 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/10also dont forget this as you dont want sql injection if($_POST['submit']) {code} Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54572 Share on other sites More sharing options...
quasiman Posted July 7, 2006 Author Share Posted July 7, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54579 Share on other sites More sharing options...
redarrow Posted July 7, 2006 Share Posted July 7, 2006 no but add add this above new data entry ok.$takeaway_email=$_POST['takeaway_email'];$takeaway_email=stripslashes($takeaway_email);good luck mate. Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54583 Share on other sites More sharing options...
quasiman Posted July 7, 2006 Author Share Posted July 7, 2006 Thanks for all the help!! Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54585 Share on other sites More sharing options...
redarrow Posted July 7, 2006 Share Posted July 7, 2006 no problam ok Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54588 Share on other sites More sharing options...
Barand Posted July 8, 2006 Share Posted July 8, 2006 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_gpcBTW, 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 Quote Link to comment https://forums.phpfreaks.com/topic/13871-move-mysql-record-with-changes/#findComment-54700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.