Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting unique information and inserting it at the same time??? 1

Status
Not open for further replies.

smashing

Programmer
Joined
Oct 15, 2002
Messages
170
Location
US
I've created a reservation system with PHP scripts hitting a MYSQL database where people reserve to travel on a bus trip. It has been up and running for 2 months now and I'm very happy with it .
I (stupidly) used only one table to collect all the user info. It has columns for their first name, last name, phone number, email address date of travel and so on. The table is filling up and I already have about 9000 rows. But I know for sure that the majority of the customers are returning ones which probably puts the number of unique customers at around 2500. So I created a new table called customers (with an id self-incrementing column) which I'll populate with unique customer info only, and start using an id number for each customer.
My problem now is how to select the people that have the same phone number AND the same first name ('cause I allow separate reservations for husband and wives etc) from the existing table, and put all them into my new table ???
I've tried playing around with SELECT DISTINCT but need help!!
 
i think this will work:
select distinct(namefield),lastname,... from table

Known is handfull, Unknown is worldfull
 
vbkris was right all along!!
Finally got this going like this:

$sql="SELECT DISTINCT fname, lname, phnum, eadd from reserve";
$result = mysql_query($sql, $connection) or die(mysql_error());

$insert_fields = "INSERT INTO customers (id, fname, lname, phnum, eadd)";

while ($row = mysql_fetch_array($result)) {

$display_fields .= " INSERT INTO customers (id, fname, lname, phnum, eadd) VALUES ('', '" . $row[fname]. "', '" . $row[lname]. "', '" . $row[phnum]. "', '" . $row[eadd]. "'); ";

$insert_fields .= " VALUES ('', '" . $row[fname]. "', '" . $row[lname]. "', '" . $row[phnum]. "', '" . $row[eadd]. "')";

$insquery .= mysql_query($insert_fields, $connection);

}
?> <html><head><title>Well Done!</title></head><body>
<p><? echo &quot;$display_fields&quot;; ?></p>
</body></html>

The only problem I'm now having is that whereas $display_fields will loop properly to display the thousands of results on the page, - $insert_fields doesn't loop and only the first row gets into the database. Any idea where I'm going wrong now???
 
try this:
for($i=0;$i<mysql_num_rows($result))
{
$row=mysql_fetch_row($result);
$newsql .= &quot; INSERT INTO customers (id, fname, lname, phnum, eadd) VALUES ('', '&quot; . $row[0]. &quot;', '&quot; . $row[1]. &quot;', '&quot; . $row[2]. &quot;', '&quot; . $row[3]. &quot;'); &quot;;

mysql_query($newsql);
}

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top