20. august 2007 - 16:30
Der er
18 kommentarer og 1 løsning
Gemme en eller flere values fra multiple formular i mysql!
Jeg vil høre om I kan hjælpe mig med at få min formular til at gemme en eller flere values fra multiple formular i mysql: side1: <form action="insert.php" method="post"> <input type="hidden" value="';?><?php echo $row['userID']; ?><?php echo '" name="userID" size="1" /> ?> <?php $jobtypesql = "SELECT * FROM jobtype ORDER BY sortering"; $result = mysql_query($jobtypesql) or die("Invalid query: " . mysql_error()); while ($row = mysql_fetch_array($result)) { $jobtype[$row['jobtypeID']] = $row['jobtypeNavn']; } switch ($_GET['action']) { case "edit": $usersql = "SELECT * FROM jobbase"; $result = mysql_query($usersql) or die("Invalid query: " . mysql_error()); $row = mysql_fetch_array($result); $jobtypeID = $row['jobtypeNavn']; break; default: $jobtypeID = ""; break; } ?> <select name="jobtypeID" multiple="multiple"> <option value="<?php echo $jobtypeID; ?>" selected>Vælg jobtype</option> <?php foreach ($jobtype as $jobtypeID => $jobtypeNavn) { if ($jobtypeID == $jobtypeNavn) { $selected = "$jobtypeID"; } else { $selected = ""; } ?> <option name="jobtypeID[]" value="<?php echo $jobtypeID; ?>"<?php echo $selected; ?>><?php echo $jobtypeNavn; ?></option> <?php } ?> </select> side2: <? // Receiving variables @$pfw_ip= $_SERVER['REMOTE_ADDR']; @$userID = $_POST['userID']; @$jobtypeID = $_POST['jobtypeID']; if($_POST[action] =="add") { mysql_query("INSERT jobbase SET userID ='$userID', jobtypeID ='$_POST[jobtypeID]'") or die(mysql_error()); } ?> Hvordan løses det?
Annonceindlæg fra Infor
20. august 2007 - 16:37
#1
Der er da kun 1 formel der?
20. august 2007 - 16:39
#2
<ole> Test dette: <? var_dump($_POST); ?> <form action="" method="post"> <select name="gnu[]" multiple="multiple"> <option value="hest">Hest</option> <option value="næbdyr">Næbdyr</option> <option value="ko">Ko</option> <option value="dræbersnegl">Dræbersnegl</option> </select> <button type="submit">TEST</button> </form> Så kan du se, variablen 'gnu' optræder som et array på serveren. Du henter derfor de forskellige options med $_POST["gnu"][0], $_POST["gnu"][1], osv /mvh </bole>
20. august 2007 - 16:40
#3
- og antallet af markerede options finder du naturligvis med count(): $numOpts = count($_POST["gnu"]);
20. august 2007 - 17:30
#4
Vil det sige?: side1: <? var_dump($_POST); ?> <form action="insert.php" method="post"> <input type="hidden" value="';?><?php echo $row['userID']; ?><?php echo '" name="userID" size="1" /> ?> <?php $jobtypesql = "SELECT * FROM jobtype ORDER BY sortering"; $result = mysql_query($jobtypesql) or die("Invalid query: " . mysql_error()); while ($row = mysql_fetch_array($result)) { $jobtype[$row['jobtypeID']] = $row['jobtypeNavn']; } switch ($_GET['action']) { case "edit": $usersql = "SELECT * FROM jobbase"; $result = mysql_query($usersql) or die("Invalid query: " . mysql_error()); $row = mysql_fetch_array($result); $jobtypeID = $row['jobtypeNavn']; break; default: $jobtypeID = ""; break; } ?> <select name="jobtypeID" multiple="multiple"> <option value="<?php echo $jobtypeID; ?>" selected>Vælg jobtype</option> <?php foreach ($jobtype as $jobtypeID => $jobtypeNavn) { if ($jobtypeID == $jobtypeNavn) { $selected = "$jobtypeID"; } else { $selected = ""; } ?> <option name="jobtypeID[]" value="<?php echo $jobtypeID; ?>"<?php echo $selected; ?>><?php echo $jobtypeNavn; ?></option> <?php } ?> </select> side2: <? // Receiving variables $numOpts = count($_POST["jobtypeID"]); @$pfw_ip= $_SERVER['REMOTE_ADDR']; @$userID = $_POST['userID']; @$jobtypeID = $_POST["jobtypeID"]['$numOpts'] if($_POST[action] =="add") { mysql_query("INSERT jobbase SET userID ='$userID', jobtypeID ='$jobtypeID'") or die(mysql_error()); } ?>
20. august 2007 - 18:34
#5
Prøv først at ret din select/option til: <select name="jobtypeID" multiple="multiple"> <option>Vælg jobtype</option> <?php foreach ($jobtype as $jobtypeID => $jobtypeNavn) { echo '<option value="'.$jobtypeID.'">'.$jobtypeNavn.'</option>'; } ?> </select> og ret insert.php til: foreach($_POST['jobtypeID'] AS $id => $value) { mysql_query("INSERT jobbase SET userID ='$userID', jobtypeID ='$id'") or die(mysql_error()); }
20. august 2007 - 18:53
#6
- men name="jobtypeID[]" giver mulighed for at anvende den langt hurtigere for-løkke. foreach-løkken er temmelig meget tungere ;o)
20. august 2007 - 19:18
#7
Hvordan det olebole?
20. august 2007 - 19:47
#8
for ($i=0,$j=count($_POST["jobtypeID"]), $i<$j; $i++) { print $_POST["jobtypeID"]."<br>"; }
20. august 2007 - 19:55
#9
<select name="jobtypeID" multiple="multiple"> skulle have været: <select name="jobtypeID[]" multiple="multiple">
20. august 2007 - 20:08
#10
Men skal name ikke normalt sættes på SELECT og ikke OPTION? Farhang - 20/08-2007 17:30:56 <select name="jobtypeID" multiple="multiple"> og <option name="jobtypeID[]" value="<?php echo $jobtypeID; ?>"<?php echo $selected; ?>><?php echo $jobtypeNavn; ?></option>
20. august 2007 - 20:22
#11
Hvad betyder: array(44) { ["userID"]=> string(5) "13634" ["jobbaseprofil"]=> string(0) "" ["jobtypeID"]=> array(3) { [0]=> string(1) "3" [1]=> string(1) "4" [2]=> string(1) "5" } "on" ["action"]=> string(3) "add" ["submit"]=> string(10) "Ret profil" } Duplicate entry '13634' for key 1 Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0
20. august 2007 - 22:55
#12
Nu ser min kode sådan ud: Side1: <form action="includes/insertjobbaseprofil.php" method="post"> <input type="hidden" value="';?><?php echo $row['userID']; ?><?php echo '" name="userID" size="1" /> <?php $jobtypesql = "SELECT * FROM jobtype ORDER BY sortering"; $result = mysql_query($jobtypesql) or die("Invalid query: " . mysql_error()); while ($row = mysql_fetch_array($result)) { $jobtype[$row['jobtypeID']] = $row['jobtypeNavn']; } switch ($_GET['action']) { case "edit": $usersql = "SELECT * FROM jobbase"; $result = mysql_query($usersql) or die("Invalid query: " . mysql_error()); $row = mysql_fetch_array($result); $jobtypeID = $row['jobtypeNavn']; break; default: $jobtypeID = ""; break; } ?> <select name="jobtypeID[]" multiple="multiple"> <option value="<?php echo $jobtypeID; ?>" selected>Vælg jobtype</option> <?php foreach ($jobtype as $jobtypeID => $jobtypeNavn) { if ($jobtypeID == $jobtypeNavn) { $selected = "$jobtypeID"; } else { $selected = ""; } ?> <option name="jobtypeID[]" value="<?php echo $jobtypeID; ?>"<?php echo $selected; ?>><?php echo $jobtypeNavn; ?></option> <?php } ?> </select> insert.php: <? var_dump($_POST); // Receiving variables @$pfw_ip= $_SERVER['REMOTE_ADDR']; @$userID = $_POST['userID']; foreach($_POST['jobtypeID'] AS $id => $value) if($_POST[action] =="add") { mysql_query("INSERT jobbase SET userID ='$userID', jobtypeID ='$id'") or die(mysql_error()); } $status = Opdateret; header("location: ../redigerjobbaseprofil.php"); ?> Mysql gemmer kun userID og browseren udskriver: array(4) { ["userID"]=> string(5) "13634" ["jobtypeID"]=> array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" } ["action"]=> string(3) "add" ["submit"]=> string(10) "Ret profil" } Duplicate entry '13634' for key 1 Hvad er problemet?
20. august 2007 - 23:10
#13
Nej, de firkantede brackets skal ikke sidde på option-elementerne - men på select-elementet: (20/08-2007 16:39:49) og (20/08-2007 19:55:51) ;o)
21. august 2007 - 07:55
#14
Test denne insert.php <? // Receiving variables @$pfw_ip= $_SERVER['REMOTE_ADDR']; @$userID = $_POST['userID']; if($_POST[action] =="add") { foreach($_POST['jobtypeID'] AS $id => $value) { mysql_query("INSERT jobbase SET userID ='$userID', jobtypeID ='$id'") or die(mysql_error()); } header("location: ../redigerjobbaseprofil.php"); } ?>
21. august 2007 - 13:52
#15
Nu kommer følgende udskrift: array(4) { ["userID"]=> string(5) "13634" ["jobtypeID"]=> array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" } ["action"]=> string(3) "add" ["submit"]=> string(10) "Ret profil" } Duplicate entry '13634' for key 1 Og kun userID bliver gemt i mysql. Jeg kan ikke se, hvad der er galt, kan I?
21. august 2007 - 14:49
#16
Jeg har nu gjort følgende: <? // Receiving variables @$pfw_ip= $_SERVER['REMOTE_ADDR']; @$userID = $_POST['userID']; @$jobtypeID = '*'.implode('*',$_POST['jobtypeID']).'*'; if($_POST[action] =="add") { mysql_query("INSERT jobbase SET userID ='$userID', jobtypeID ='$jobtypeID'") or die(mysql_error()); } header("location: ../redigerjobbaseprofil.php"); ?> Hvilke fungerer perfekt. :-) Kom med jeres svar.
22. august 2007 - 07:59
#17
Svar!
22. august 2007 - 16:13
#18
22. august 2007 - 16:57
#19
Skulle olebole ikke have point?
Vi tilbyder markedets bedste kurser inden for webudvikling