whitehat Posted October 19, 2006 Share Posted October 19, 2006 Hello @allI'm having a problem creating a function for a deletion and inserting from 1 to another database table.Below are some sql query's to give you an idea of what i try to do:[code=php:0]function orders() {$mysample1 .= $db->sql_query("select * from cart inner join calendar on cart.products_id = calendar.calendar_id where cart.cookieId = '" . Getbasket_id() . "' and main_id = '$Pid'"); $mysample2 .= $db->sql_query("insert into orders(user_id, products_id, booking_date, qty) values('$uid', '$products_id', now(), '$qty')");$mysample3 .= $db->sql_query("delete from cart where cookieId = '" . Getbasket_id() . "' and products_id = $products_id");}return $mysamples;$content .= "<div><a href=\"#\" onclick=\"$mysamples\"><img src=\"modules/Fly_to_Basket/images/order.gif\"></div>";[/code]on top is just to give an idea pls dont laugh lol.i really have no clue how to go on below i add the db examples if it is needed to help me out:[code]CREATE TABLE `cart` ( `basket_id` int(11) NOT NULL auto_increment, `cookieId` varchar(50) default NULL, `products_id` int(11) default NULL, `booking_date` datetime NOT NULL default '0000-00-00 00:00:00', `qty` int(11) default NULL, PRIMARY KEY (`basket_id`), UNIQUE KEY `id` (`basket_id`)) TYPE=MyISAMCREATE TABLE `orders` ( `orders_id` int(11) NOT NULL auto_increment, `user_id` int(11) NOT NULL default '0', `products_id` int(11) default NULL, `booking_date` datetime NOT NULL default '0000-00-00 00:00:00', `qty` int(11) default NULL, PRIMARY KEY (`orders_id`)) TYPE=MyISAM[/code] Link to comment https://forums.phpfreaks.com/topic/24428-request-help-creating-simple-function/ Share on other sites More sharing options...
obsidian Posted October 19, 2006 Share Posted October 19, 2006 i'm not sure i completely understand, but usually, you'll have to have something like this if you're doing multiple inserts based on a query:[code]<?php$sql = mysql_query("SELECT * FROM table1");if (mysql_num_rows($sql) > 0) { $results = array(); while ($row = mysql_fetch_row($sql)) $results[] = $row; // connect to database for insertion here // now that you have your new connection, insert the records foreach ($results as $row) { $q = "INSERT INTO tableName VALUES ('" . implode("', '", $row) . "')"; mysql_query($q); }}?>[/code]you probably would want to update this to allow for some error checking/catching, but at least you can get the idea.good luck Link to comment https://forums.phpfreaks.com/topic/24428-request-help-creating-simple-function/#findComment-111210 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.