plodos Posted December 22, 2008 Share Posted December 22, 2008 if(isset($_FILES['paper'])){ $file_name = $_FILES['paper']['name']; $file_ext = strtolower(substr($file_name,strrpos($file_name,"."))); $q = "insert into paper set title='{$_REQUEST['title']}', abstract='{$_REQUEST['abstract']}', keywords='{$_REQUEST['keywords']}', authors='{$_REQUEST['authors']}', ext='$file_ext', user='{$_SESSION['id']}', status='new', datum='date()' "; $result = mysql_query($q); $fid = mysql_insert_id(); move_uploaded_file($_FILES['paper']['tmp_name'], 'sweet_papers/'.$fid.$file_ext); //problem part $state=$_POST['state']; foreach ($state as $statename) { $qu = "insert into paper_research_field set paper='{$fid}', d='{$statename}'"; $err = mysql_query($qu); if(!$err) { header("Location:".$_SERVER['HTTP_REFERER']."?wrong_code21=true"); } } //problem part end header('Location: http://'.$_SERVER['SERVER_NAME'].'/my_papers.php'); } i want to add these checkbox values with foreach but it gives this Warning: Invalid argument supplied for foreach() WHY ?? <tr> <td><input type='checkbox' name='state[]' value='1'>Acoustics</td> <td><input type='checkbox' name='state[]' value='2'>Antennas/RF Front Ends </td> </tr> Link to comment https://forums.phpfreaks.com/topic/137951-solved-php-chechkbox-warning-invalid-argument-supplied-for-foreach-why/ Share on other sites More sharing options...
Maq Posted December 22, 2008 Share Posted December 22, 2008 Try: foreach($_POST['state'] as $statename) Link to comment https://forums.phpfreaks.com/topic/137951-solved-php-chechkbox-warning-invalid-argument-supplied-for-foreach-why/#findComment-721032 Share on other sites More sharing options...
plodos Posted December 22, 2008 Author Share Posted December 22, 2008 i changed it //$state=$_POST['state']; foreach($_POST['state'] as $statename) { $qu = "insert into paper_research_field set paper='{$fid}', id='{$statename}'"; echo $q; $err = mysql_query($qu); if(!$err) { header("Location:".$_SERVER['HTTP_REFERER']."?wrong_code21=true"); } } but still same problem Warning: Invalid argument supplied for foreach() Link to comment https://forums.phpfreaks.com/topic/137951-solved-php-chechkbox-warning-invalid-argument-supplied-for-foreach-why/#findComment-721033 Share on other sites More sharing options...
plodos Posted December 22, 2008 Author Share Posted December 22, 2008 This occurs when nothing is selected in the checkboxes. Solution is need to control with is_array()! if(is_array($_POST['state'])) { foreach($_POST['state'] as $statename) { /////// } } Link to comment https://forums.phpfreaks.com/topic/137951-solved-php-chechkbox-warning-invalid-argument-supplied-for-foreach-why/#findComment-721039 Share on other sites More sharing options...
Maq Posted December 22, 2008 Share Posted December 22, 2008 Hmm, that's good to know. I thought it still returns an empty array. I guess not... Link to comment https://forums.phpfreaks.com/topic/137951-solved-php-chechkbox-warning-invalid-argument-supplied-for-foreach-why/#findComment-721060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.