Forums

This topic is locked

Problem Insert alle Values aus Multiple List

Posted 28 Dec 2006 14:29:25
1
has voted
28 Dec 2006 14:29:25 Tim Lehmann posted:
Problem Insert alle Values aus Multiple List von Recordset

Hallo Leuts,
brauche dringend Hilfe,
habe mit Dreamweaver und der MX Kollection ein Recordset erstellt.
Darin habe ich 2 dynamische List Menus,
wo der User mehrere Values auswählen können soll,
in meiner SQL Datenbank kommt aber immer nur die letzte Auswahl an,
nun habe ich dazu im net gesucht, und das unten stehende Posting gefunden,
das scheint genau das zu sein, was ich brauche,
leider weiss ich net, wo genau ich den Code einfügen muss,
könnte mir da bitte jemand behilflich sein ???
Werde sonst noch verrückt mit der Sache.
Danke schon mal

mfg
Tim

***************hier mein code*********************************************

<?php require_once('Connections/local.php'); ?>
<?php
// Load the common classes
require_once('includes/common/KT_common.php');

// Load the tNG classes
require_once('includes/tng/tNG.inc.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("";

// Make unified connection variable
$conn_local = new KT_connection($local, $database_local);

// Start trigger
$formValidation = new tNG_FormValidation();
$tNGs->prepareValidation($formValidation);
// End trigger

if (!function_exists("GetSQLValueString") {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = ""
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string" ? mysql_real_escape_string($theValue) : mysql_escape_string($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;
}
}

mysql_select_db($database_local, $local);
$query_languages = "SELECT * FROM sbdtng_languages ORDER BY sbdtng_languages.sb_desc";
$languages = mysql_query($query_languages, $local) or die(mysql_error());
$row_languages = mysql_fetch_assoc($languages);
$totalRows_languages = mysql_num_rows($languages);

mysql_select_db($database_local, $local);
$query_currencies = "SELECT * FROM sbdtng_currencies_all ORDER BY sbdtng_currencies_all.sb_desc";
$currencies = mysql_query($query_currencies, $local) or die(mysql_error());
$row_currencies = mysql_fetch_assoc($currencies);
$totalRows_currencies = mysql_num_rows($currencies);

// Make an insert transaction instance
$ins_users = new tNG_insert($conn_local);
$tNGs->addTransaction($ins_users);
// Register triggers
$ins_users->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1";
$ins_users->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$ins_users->registerTrigger("END", "Trigger_Default_Redirect", 99, "test.php";
// Add columns
$ins_users->setTable("users";
$ins_users->addColumn("first_name", "STRING_TYPE", "POST", "first_name";
$ins_users->addColumn("last_name", "STRING_TYPE", "POST", "last_name";
$ins_users->addColumn("email_address", "STRING_TYPE", "POST", "email_address";
$ins_users->addColumn("username", "STRING_TYPE", "POST", "username";
$ins_users->addColumn("languages", "STRING_TYPE", "POST", "languages";
$ins_users->addColumn("currencies", "STRING_TYPE", "POST", "currencies";
$ins_users->addColumn("password", "STRING_TYPE", "POST", "password";
$ins_users->addColumn("signup_date", "DATE_TYPE", "POST", "signup_date";
$ins_users->setPrimaryKey("userid", "NUMERIC_TYPE";

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rsusers = $tNGs->getRecordset("users";
$row_rsusers = mysql_fetch_assoc($rsusers);
$totalRows_rsusers = mysql_num_rows($rsusers);
?><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>test</title>
<link href="includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="includes/common/js/base.js" type="text/javascript"></script>
<script src="includes/common/js/utility.js" type="text/javascript"></script>
<script src="includes/skins/style.js" type="text/javascript"></script>
<?php echo $tNGs->displayValidationRules();?>
<style type="text/css">
<!--
.style2 {color: #999999}
-->
</style>
</head>

<body>
<?php
echo $tNGs->getErrorMsg();
?>
<form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">
<table cellpadding="2" cellspacing="0" class="KT_tngtable">
<tr>
<td class="KT_th"><span class="style2">
<label for="first_name">First_name:</label>
</span></td>
<td><input type="text" name="first_name" id="first_name" value="<?php echo KT_escapeAttribute($row_rsusers['first_name']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("first_name";?> <?php echo $tNGs->displayFieldError("users", "first_name"; ?> </td>
</tr>
<tr>
<td class="KT_th"><span class="style2">
<label for="last_name">Last_name:</label>
</span></td>
<td><input type="text" name="last_name" id="last_name" value="<?php echo KT_escapeAttribute($row_rsusers['last_name']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("last_name";?> <?php echo $tNGs->displayFieldError("users", "last_name"; ?> </td>
</tr>
<tr>
<td class="KT_th"><span class="style2">
<label for="email_address">Email_address:</label>
</span></td>
<td><input type="text" name="email_address" id="email_address" value="<?php echo KT_escapeAttribute($row_rsusers['email_address']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("email_address";?> <?php echo $tNGs->displayFieldError("users", "email_address"; ?> </td>
</tr>
<tr>
<td class="KT_th"><span class="style2">
<label for="username">Username:</label>
</span></td>
<td><input type="text" name="username" id="username" value="<?php echo KT_escapeAttribute($row_rsusers['username']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("username";?> <?php echo $tNGs->displayFieldError("users", "username"; ?> </td>
</tr>
<tr>
<td class="KT_th"><span class="style2">
<label for="languages">Languages:</label>
</span></td>
<td><select name="languages" size="3" multiple id="languages">
<?php
do {
?>
<option value="<?php echo $row_languages['sb_desc']?>"<?php if (!(strcmp($row_languages['sb_desc'], $row_rsusers['languages']))) {echo "SELECTED";} ?>><?php echo $row_languages['sb_desc']?></option>
<?php
} while ($row_languages = mysql_fetch_assoc($languages));
$rows = mysql_num_rows($languages);
if($rows > 0) {
mysql_data_seek($languages, 0);
$row_languages = mysql_fetch_assoc($languages);
}
?>
</select>
<?php echo $tNGs->displayFieldError("users", "languages"; ?> </td>
</tr>
<tr>
<td class="KT_th"><span class="style2">
<label for="currencies">Currencies:</label>
</span></td>
<td><select name="currencies" size="3" multiple id="currencies">
<?php
do {
?>
<option value="<?php echo $row_currencies['sb_desc']?>"<?php if (!(strcmp($row_currencies['sb_desc'], $row_rsusers['currencies']))) {echo "SELECTED";} ?>><?php echo $row_currencies['sb_desc']?></option>
<?php
} while ($row_currencies = mysql_fetch_assoc($currencies));
$rows = mysql_num_rows($currencies);
if($rows > 0) {
mysql_data_seek($currencies, 0);
$row_currencies = mysql_fetch_assoc($currencies);
}
?>
</select>
<?php echo $tNGs->displayFieldError("users", "currencies"; ?> </td>
</tr>
<tr>
<td class="KT_th"><span class="style2">
<label for="password">Password:</label>
</span></td>
<td><input type="text" name="password" id="password" value="<?php echo KT_escapeAttribute($row_rsusers['password']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("password";?> <?php echo $tNGs->displayFieldError("users", "password"; ?> </td>
</tr>
<tr>
<td class="KT_th"><span class="style2">
<label for="signup_date">Signup_date:</label>
</span></td>
<td><input type="text" name="signup_date" id="signup_date" value="<?php echo KT_formatDate($row_rsusers['signup_date']); ?>" size="32" />
<?php echo $tNGs->displayFieldHint("signup_date";?> <?php echo $tNGs->displayFieldError("users", "signup_date"; ?> </td>
</tr>
<tr class="KT_buttons">
<td colspan="2"><input name="KT_Insert1" type="submit" id="KT_Insert1" value="Insert record" /> </td>
</tr>
</table>
</form>
<p> </p>
</body>
</html><?php
mysql_free_result($languages);

mysql_free_result($currencies);
?>

******************ende von meinem code******************************************************


****************hier das gefundene posting*************************************

First, you need to rename your form's select tag from name="Type" to name="Type[]". Putting those brackets in at the end
of the name will make it so that when PHP receives the form contents it will turn them into an array. Then you have to take
DW's usual insert record code and put it within a loop that loops through each of the array items. Here's an example:

// set it up
reset ($languages); // this sets the array pointer to the beginning of the array
mysql_select_db($database, $db);

// loop through array of languages[] and write each instance to db
while (list($key, $val) = each ($languages)) {
$insertSQL = sprintf("INSERT INTO users (languages) VALUES (%s)",
GetSQLValueString($val, "text");
$Result1 = mysql_query($insertSQL, $db) or die(mysql_error());
} // end loop

Note that you also need to have the GetSQLValueString function on the page somewhere. DW automatically inserts this
function whenever you do an insert record behavior. You can copy and paste it from another page if you like.
Also be sure to have the include file to your connection settings somewhere on the page.

Reply to this topic