nitesh Posted November 16, 2011 Share Posted November 16, 2011 Hi everyone iam passing value check box value from my form 1st <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><span class="style1">Poor</span></td> <td><input name="quality[]" type="checkbox" value="Poor"></td> <td><span class="style1">Good</span></td> <td><input name="quality[]" type="checkbox" value="Good"></td> <td><span class="style1">Excellent</span></td> <td><input name="quality[]" type="checkbox" value="Excellent"></td> </tr> <tr><td> </td></tr></table> 2st<table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><span class="style1">Poor</span></td> <td><input name="portion[]" type="checkbox" value="Poor"></td> <td><span class="style1">Good</span></td> <td><input name="portion[]" type="checkbox" value="Good"></td> <td><span class="style1">Excellent</span></td> <td><input name="portion[]" type="checkbox" value="Excellent"></td> </tr> <tr><td> </td></tr></table> only quality CHECKBOX value is inserting in database not inserting portion CHECKBOX value in database <?php// Connect to server and datanasemysql_connect("$host","$username","$password") or die ("Could not connect to Server");mysql_select_db($db_name) or die("Counld not connect to database table");// Fetch value from html form$name = $_POST["name"];$email = $_POST["email"];$phone = $_POST["phone"]; $quality = $_POST["quality"];$portion = $_POST["portion"]; foreach ($quality as $one){$first.= $one." ";}foreach ($portion as $second){$two.= $second." ";} $sql = "INSERT INTO $db_table (name,email,phone,Food1,Food2) VALUES ('$name','$email','$phone','$first','$two')";$result = mysql_query($sql);if($result){$redirct = "Location:thanku.html";header($redirct);}else{echo "Error in sending data";}mysql_close();?> Link to comment Share on other sites More sharing options...
artistandlive Posted November 16, 2011 Share Posted November 16, 2011 Is portion in your superglobal $_POST ?Try print_r($_POST); exit; Link to comment Share on other sites More sharing options...
birbal Posted November 16, 2011 Share Posted November 16, 2011 use var_dump($_POST); to see what it is actually returning. can you see portion array there? Link to comment Share on other sites More sharing options...
Ingolme Posted November 16, 2011 Share Posted November 16, 2011 I notice that you're not initializing the strings before the loop. $qualityvalues = ''; // Initialize the stringforeach ($quality as $one){ $qualityvalues .= $one . ' ';} $portionvalues = ''; // Initialize the stringforeach ($portion as $two){ $portionvalues .= $two . ' ';} Link to comment Share on other sites More sharing options...
artistandlive Posted November 16, 2011 Share Posted November 16, 2011 I notice that you're not initializing the strings before the loop. Commonly this is not necessary for strings. Link to comment Share on other sites More sharing options...
Ingolme Posted November 16, 2011 Share Posted November 16, 2011 Commonly this is not necessary for strings.It doesn't make sense in computer logic. Any other language wouldn't compile or would append a random value to the string if you tried that. PHP outputs a notice or a warning (usually PHP is set not to display notice and warning-level errors), but it will continue to function normally assuming an empty string. Link to comment Share on other sites More sharing options...
nitesh Posted November 16, 2011 Author Share Posted November 16, 2011 iam getting this errorWarning: Invalid argument supplied for foreach() in /home/content/44/8544344/html/addcomment.php on line 25Warning: Invalid argument supplied for foreach() in /home/content/44/8544344/html/addcomment.php on line 28 Link to comment Share on other sites More sharing options...
nitesh Posted November 16, 2011 Author Share Posted November 16, 2011 I notice that you're not initializing the strings before the loop.$qualityvalues = ''; // Initialize the stringforeach ($quality as $one){ $qualityvalues .= $one . ' ';} $portionvalues = ''; // Initialize the stringforeach ($portion as $two){ $portionvalues .= $two . ' ';} I have initialize the value on top Link to comment Share on other sites More sharing options...
dsonesuk Posted November 16, 2011 Share Posted November 16, 2011 IF you have this on the same page, you have check form has been submitted by checking the submit name value, or individual input name value, or it will try to loop through (foreach) the post array values of the the checkbox values that are empty similar to if(isset($_POST["go"])) // name of submit buuton value { $name = $_POST["name"]; $email = $_POST["email"]; $phone = $_POST["phone"]; $quality = $_POST["quality"]; $portion = $_POST["portion"]; $first=""; $two=""; if(isset($_POST["quality"])) { foreach ($quality as $one){ $first.= $one." "; } } if(isset($_POST["quality"])) { foreach ($portion as $second){ $two.= $second." "; } } $sql = "INSERT INTO $db_table (name,email,phone,Food1,Food2) VALUES ('$name','$email','$phone','$first','$two')";$result = mysql_query($sql);if($result){$redirct = "Location:thanku.html";header($redirct);}else{echo "Error in sending data";}mysql_close(); } Link to comment Share on other sites More sharing options...
nitesh Posted November 16, 2011 Author Share Posted November 16, 2011 Thank you every one problem is solved. Link to comment Share on other sites More sharing options...
artistandlive Posted November 16, 2011 Share Posted November 16, 2011 Thank you every one problem is solved. And the solution is ...? Link to comment Share on other sites More sharing options...
artistandlive Posted November 16, 2011 Share Posted November 16, 2011 It doesn't make sense in computer logic. Any other language wouldn't compile or would append a random value to the string if you tried that. PHP outputs a notice or a warning (usually PHP is set not to display notice and warning-level errors), but it will continue to function normally assuming an empty string. You are totally right.PHP is just a simple script language. Almost nothing matters here Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2011 Share Posted November 16, 2011 You should always be developing with all error messages being shown. Things like undefined variables are in fact an error and should be fixed, your code shouldn't be considered finished until it runs with no errors and no notices. You can enable all errors at runtime like this: ini_set('display_errors', 1);error_reporting(E_ALL); On production servers it's common to send errors to a log instead of displaying them on the page, or turning them off altogether. You should not have error messages disabled while you're developing though, and you should never use the error suppression operator (@). Link to comment Share on other sites More sharing options...
niche Posted November 16, 2011 Share Posted November 16, 2011 Sage advice Ingolme & JSG . Thanks for the reminder. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.