john_6767 Posted October 8, 2006 Share Posted October 8, 2006 hey guys, i've been trying to find out how to do this for a while with no success although i have seen it in as common practise in many places so it can't be too hard..basically i have an item (in my case bouncy balls) and this item can have many of another thing related to it (in my case sizes).I have set up a very simple test site to try get this working and have attatched it hoping that someone can show me where i have gone wrong..I have included the simple database i made as an sql export also so its easy to set up and see what i have done if someone has the time, but any help with links to information or snippets of code would be much appreciated.[attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/23325-php-one-to-many-insert-with-checkboxes-solved/ Share on other sites More sharing options...
tomfmason Posted October 8, 2006 Share Posted October 8, 2006 You should post the code and not just upload it. Most people will not download that. Quote Link to comment https://forums.phpfreaks.com/topic/23325-php-one-to-many-insert-with-checkboxes-solved/#findComment-105750 Share on other sites More sharing options...
john_6767 Posted October 8, 2006 Author Share Posted October 8, 2006 ok cheers,heres the add page, the displaying of checkboxes in the form doesn't work,[code]<?php require_once('Connections/cnnTest.php'); ?><?phpfunction 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"] == "balls_add_form")) { $insertSQL = sprintf("INSERT INTO balls (ball_name, ball_desc) VALUES (%s, %s)", GetSQLValueString($_POST['txt_ball_name'], "text"), GetSQLValueString($_POST['txt_ball_desc'], "text")); mysql_select_db($database_cnnTest, $cnnTest); $Result1 = mysql_query($insertSQL, $cnnTest) or die(mysql_error());//standard dreamweaver insert code above//my insert for sizes belowforeach ($_POST["size"] as $sizeID) { $ary = "INSERT INTO balls_sizes (ball_id, size_id) VALUES (" . $ball_id . ", '" . $sizeID . "')"; // execute query mysql_select_db($database_cnnTest, $cnnTest); $Result2 = mysql_query($ary, $cnnTest) or die(mysql_error());} //end of my insert for sizes $insertGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo));}mysql_select_db($database_cnnTest, $cnnTest);$query_rsSizes = "SELECT * FROM sizes ORDER BY size_name ASC";$rsSizes = mysql_query($query_rsSizes, $cnnTest) or die(mysql_error());$row_rsSizes = mysql_fetch_assoc($rsSizes);$totalRows_rsSizes = mysql_num_rows($rsSizes);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Balls add page</title></head><body><p>Balls add page</p><form id="balls_add_form" name="balls_add_form" method="POST" action="<?php echo $editFormAction; ?>"> <table width="80%" border="0" cellpadding="0" cellspacing="0"> <tr> <td>Ball Name </td> <td><input name="txt_ball_name" type="text" id="txt_ball_name" /></td> </tr> <tr> <td>Ball Desc </td> <td><input name="txt_ball_desc" type="text" id="txt_ball_desc" /></td> </tr> <tr> <td>Sizes</td> <td> <?php // Assume that the database returns the list of sizes for ($i=0; $i < mysql_num_rows($rsSizes); $i++) { $row = mysql_fetch_row($rsSizes); echo "<input type=\"checkbox\" name=\"size[]\" value=\"" . $row[0] . "\">\n"; } ?> <input type="checkbox" name="checkbox" value="checkbox" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Add Ball" /></td> </tr> </table> <input type="hidden" name="MM_insert" value="balls_add_form"></form></body></html><?phpmysql_free_result($rsSizes);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23325-php-one-to-many-insert-with-checkboxes-solved/#findComment-105758 Share on other sites More sharing options...
.josh Posted October 8, 2006 Share Posted October 8, 2006 when you say "displaying of checkboxes in the form doesn't work" do you mean that you are not getting anything at all displayed, including the actual checkboxes, or do you mean to say that the checkboxes are being displayed, but not the info that should go next to them?if you mean the 2nd, it's because you aren't actually echoing the data next to your checkbox. it should look something like this:[code] <?php // Assume that the database returns the list of sizes for ($i=0; $i < mysql_num_rows($rsSizes); $i++) { $row = mysql_fetch_row($rsSizes); echo "<input type=\"checkbox\" name=\"size[]\" value=\"" . $row[0] . "\"> {$row[0]}\n"; } ?>[/code]also just so you know, that code block can be reduced a bit, by doing this instead:[code] <?php // Assume that the database returns the list of sizes while ($row = mysql_fetch_row($rsSizes)) { echo "<input type='checkbox' name='size[]' value='{$row[0]}'> {$row[0]}\n"; } ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23325-php-one-to-many-insert-with-checkboxes-solved/#findComment-105782 Share on other sites More sharing options...
john_6767 Posted October 8, 2006 Author Share Posted October 8, 2006 thanks for your help, that second snippet in your post of php returns 4 checkboxes, but the first snippet returns 5..4 is the correct number as i should have one checkbox for every size and there are 4 sizes in the database table sizes,how do i write the size_name row from the database next to the checkbox not the number,cheers Quote Link to comment https://forums.phpfreaks.com/topic/23325-php-one-to-many-insert-with-checkboxes-solved/#findComment-105788 Share on other sites More sharing options...
.josh Posted October 8, 2006 Share Posted October 8, 2006 you have to change the 0 in $row[0] to whichever element number holds your size_name. That is why i use mysql_fetch_array instead of mysql_fetch_row, because instead of guessing, i can just use the name of the row.[code] <?php // Assume that the database returns the list of sizes while ($row = mysql_fetch_array($rsSizes)) { echo "<input type='checkbox' name='size[]' value='{$row[0]}'> {$row['size_name']}\n"; } ?>[/code]assuming that [b]size_name[/b] is your column name. With mysql_fetch_array by default you still get the number indexed array from fetch_row, as well as the new associative array where you can use the column name, so you can technically use $row[0] in your value='{$row[0]}' but you can change that to value='{$row['column_name_here']}' Quote Link to comment https://forums.phpfreaks.com/topic/23325-php-one-to-many-insert-with-checkboxes-solved/#findComment-105930 Share on other sites More sharing options...
john_6767 Posted October 8, 2006 Author Share Posted October 8, 2006 ok, that works and displays the field names next to the checkboxes but it doesn't display the last checkbox.. ie if there 5 in database it displays the first 4/also my updaqte script doesn't work, should be pretty simple stuff? where have i gone wrong? ???[code=php:0]$editFormAction = $_SERVER['PHP_SELF'];if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);}if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "balls_add_form")) { $insertSQL = sprintf("INSERT INTO balls (ball_name, ball_desc) VALUES (%s, %s)", GetSQLValueString($_POST['txt_ball_name'], "text"), GetSQLValueString($_POST['txt_ball_desc'], "text")); mysql_select_db($database_cnnTest, $cnnTest); $Result1 = mysql_query($insertSQL, $cnnTest) or die(mysql_error());//standard dreamweaver insert code above//my insert for sizes belowforeach ($_POST["size"] as $sizeID) { $ary = "INSERT INTO balls_sizes (ball_id, size_id) VALUES (" . $ball_id . ", '" . $sizeID . "')"; // execute query mysql_select_db($database_cnnTest, $cnnTest); $Result2 = mysql_query($ary, $cnnTest) or die(mysql_error());} //end of my insert for sizes $insertGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo));}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23325-php-one-to-many-insert-with-checkboxes-solved/#findComment-106029 Share on other sites More sharing options...
john_6767 Posted October 9, 2006 Author Share Posted October 9, 2006 i can get the checkboxes to display properly this way but am unsure as to how to loop through the values on the insert.. is this bad practise to do it this way?maybe i can use [code=php:0]expload(",",$POST['size_id'])[/code] ???[code=php:0] <?php do { ?> <input type="checkbox" name="size_id[]" value="<?php echo $row_rsSizes['size_id']; ?>" /> <?php echo $row_rsSizes['size_name']; ?><br /> <?php } while ($row_rsSizes = mysql_fetch_assoc($rsSizes)); ?>[/code]ok this worked ;D, incase anyone is curious i just changed the checkbox field to an array as above and for the insert i did,now thats the easy bit, onto the update..[code=php:0]if (isset($_POST['size_id'])){ foreach ($_POST['size_id'] as $size_id) {//insert here }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23325-php-one-to-many-insert-with-checkboxes-solved/#findComment-106038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.