Jump to content

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


psyion

Recommended Posts

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?????
Link to comment
Share on other sites

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);
?>
Link to comment
Share on other sites

[!--quoteo(post=352614:date=Mar 7 2006, 03:01 PM:name=psyion)--][div class=\'quotetop\']QUOTE(psyion @ Mar 7 2006, 03:01 PM) [snapback]352614[/snapback][/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]
[code]
INSERT INTO temp_class (
   SELECT *
   FROM student
   ORDER BY RAND()
   LIMIT 15
)
[/code]

Also, in that code you posted, you cant use strings in a switch() statement.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.