Jump to content


Photo

Help~ how pick few things and insert to another table?


  • Please log in to reply
3 replies to this topic

#1 psyion

psyion
  • Members
  • PipPip
  • Member
  • 13 posts
  • LocationKota Kinabalu, Sabah, Malaysia.

Posted 07 March 2006 - 09:01 PM

here is my problem, i'm need to randomly pick 15 names from a table call student and store them to another table call temp_class

here is the random part

SELECT name
FROM student
ORDER BY RAND() LIMIT 15

but than how do i send this 15 names to temp_class?????

#2 psyion

psyion
  • Members
  • PipPip
  • Member
  • 13 posts
  • LocationKota Kinabalu, Sabah, Malaysia.

Posted 08 March 2006 - 03:35 AM

can some one help me please... i've been stuck here for roughly 2 days i'll be happy if some one can help me... below are the code generate by Dreamweaver about showing a repeted table showing all the 15 randomly pick name and a text field that submit new name... can some one help me modify or show where i can get more example.


<?php require_once('../Connections/activity.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO temp_class (temp_name) VALUES (%s)",
GetSQLValueString($_POST['temp_name'], "text"));

mysql_select_db($database_activity, $activity);
$Result1 = mysql_query($insertSQL, $activity) or die(mysql_error());

$insertGoTo = "untitled.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_activity, $activity);
$query_rand_pick = "SELECT student.name FROM student ORDER BY RAND() LIMIT 15";
$rand_pick = mysql_query($query_rand_pick, $activity) or die(mysql_error());
$row_rand_pick = mysql_fetch_assoc($rand_pick);
$totalRows_rand_pick = mysql_num_rows($rand_pick);
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>name</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_rand_pick['name']; ?></td>
</tr>
<?php } while ($row_rand_pick = mysql_fetch_assoc($rand_pick)); ?>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>

<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Temp_name:</td>
<td><input type="text" name="temp_name" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="submit" value="Insert record"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($rand_pick);
?>


#3 greycap

greycap
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 08 March 2006 - 03:43 AM

[!--quoteo(post=352614:date=Mar 7 2006, 03:01 PM:name=psyion)--][div class=\'quotetop\']QUOTE(psyion @ Mar 7 2006, 03:01 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
here is my problem, i'm need to randomly pick 15 names from a table call student and store them to another table call temp_class

here is the random part

SELECT name
FROM student
ORDER BY RAND() LIMIT 15

but than how do i send this 15 names to temp_class?????
[/quote]
INSERT INTO temp_class (
   SELECT * 
   FROM student
   ORDER BY RAND()
   LIMIT 15
)

Also, in that code you posted, you cant use strings in a switch() statement.

#4 psyion

psyion
  • Members
  • PipPip
  • Member
  • 13 posts
  • LocationKota Kinabalu, Sabah, Malaysia.

Posted 08 March 2006 - 04:25 AM

thanks alot...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users