Hello ive got a while loop and when the user selects one result from the loop it displays the chosen result and inserts it into the database table but i want the selected row to go to a table depending on one field from the while loop. there are three tables to choose from i aint got a clue how to do it, would i use if statement? thank you

can you post any code you have? this would make it easier to help you.

can you post any code you have? this would make it easier to help you.

$insert = "INSERT INTO Products (ItemNo, ItemName, ItemPrice, ItemQty, ItemDate) VALUES ('$ItemNo', '$ItemName', $ItemPrice,$ItemQuantity,#$PurchaseDate#)";
mysql_query($insert);

so if ItemName = mars i want that record to be sent to a database table called mars and if the ItemName = Pit i want that record to another table called pit and so on i need to do this for three tables thanks

This would work but its kind of extensive and not needed if you have control of the item name

<?php

switch($itemName) {
	case "mars":
		$table = 'Mars';
	break;
	case "pit":
		$table = 'Pit';
	break;
	case "othertablename":
		$table = 'OtherTableName';
	break;
}
$sql   = "INSERT INTO `" . $table . "` (ItemNo,ItemName,ItemPrice,ItemQty,ItemDate) VALUES ('$ItemNo', '$ItemName', $ItemPrice,$ItemQuantity,#$PurchaseDate#)";
$query = mysql_query($sql) or die('Error: ' . mysql_error());

?>

you could use an if, elseif, else arrangement to accomplish the same goal

Or you can do it this way, which is less secure if the item name is based on the input of a user of the site

<?php

$sql   = "INSERT INTO `" . $itemName . "` (ItemNo, ItemName, ItemPrice, ItemQty, ItemDate) VALUES ('$ItemNo', '$ItemName', $ItemPrice,$ItemQuantity,#$PurchaseDate#)";
$query = mysql_query($sql) or die('Error: ' . mysql_error());

?>

Hello i would just use else if statements

$insert = "Insert into $table (col1, col2) values ('$hello', $bye')";

Look up else if statements

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.