Jump to content

Need Help In Inserting Multiple Checkbox Value In Database


nitesh

Recommended Posts

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

use var_dump($_POST); to see what it is actually returning. can you see portion array there?

Link to comment
Share on other sites

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

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

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

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

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

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 :P
Link to comment
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...