1

I have checkbox entries that I am appending to a list by their html name, like so:

    Choose no more than three categories:<br>
    <input id='category1' type="checkbox" name="boxsize[]" 
    onclick="CountChecks('listone',3,this)" value="asian">Asian
    <input id='category2' type="checkbox" name="boxsize[]" 
    onclick="CountChecks('listone',3,this)" value="asianFusion">Asian Fusion

I have many other checkboxes as well. I then implode this list by doing:

    $sanentry=implode(',',$_REQUEST["boxsize"]);

When I echo $sanentry I get a list of the selected values in the following format: asian, asian fusion. However when I try to send these values to my ethnicity table in mysql the ethnicity column is empty. Here is the post method and query I am using to send these values to my table.

    $sanethnicity=mysqli_real_escape_string($con, $_POST['$sanentry']);
    $sql3="INSERT INTO 
    ethnicity(restaurant_id,ethnicity)VALUES('$sanrestid','$sanethnicity')";
    if ($con->query($sql3) === TRUE) {
    echo "New record in ethnicity table created \n";
    } else {
    die("Error: " . $sql3 . "<br>" . $con->error);
    }
    mysqli_close($con);
    ?>

There is no problem with my restaurant_id column as that is being updated fine but for every new row inserted the ethnicity column always comes up blank. Does anyone know what I'm doing wrong? enter image description here

8
  • Typo? Change $_POST['$sanentry'] to $_POST['sanentry'] Commented Mar 3, 2018 at 20:19
  • you are just trying to insert a string technically. maybe the string is to long for the column or something. you are not getting any sql errors? what does the sql profiler say? Commented Mar 3, 2018 at 20:19
  • I'm not getting an SQL errors Commented Mar 3, 2018 at 20:20
  • The ethnicity column just shows up blank each time Commented Mar 3, 2018 at 20:20
  • Also I made the constraints for the ethnicity column varchar(255) so it should be too long for the column Commented Mar 3, 2018 at 20:21

3 Answers 3

1

Guessing the variable name is wrong. should be $sanethnicity you've got $ethnicitydb in your query.

$sql3="INSERT INTO ethnicity(restaurant_id,ethnicity) VALUES('$sanrestid','$sanethnicity')";

Also, is this the field that has raw ethicity array? $_POST['$sanentry'] or has that been imploded. You probably want this:

$sanethnicity=mysqli_real_escape_string($con, $sanethnicity);

Since the $sanethnicity was prior imploded from seomthing like:

$sanethnicity = implode(',',$_REQUEST["boxsize"]);
Sign up to request clarification or add additional context in comments.

Comments

0

In this line your trying to use $sanentry as an entry in $_POST...

$sanethnicity=mysqli_real_escape_string($con, $_POST['$sanentry']);

Should be

$sanethnicity=mysqli_real_escape_string($con, $sanentry);

Although - you should be looking into using prepared statements and bind variables.

Comments

0

If you want to store it without losing the array structure then you should use serialize

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.