0

Im trying to send a string to my database depending on the answers from a multiple select question. The question has three answers, ESEA,FACEIT and Matchmaking. Depending on the answer i want to send strings to my database. The database is set up with Varchar.

The html form for handling my multiple select question looks like this:

<select name="multipleSelect[]" multiple>
   <option value="" disabled selected>Choose your option</option>
   <option name="esea" value="esea">ESEA</option>
   <option name="faceit" value="faceit">FaceIT</option>
   <option name="matchmaking" value="matchmaking">Matchmaking</option>
</select>
<label>What are you looking to play?</label>

The code for handling the form look like this:

$esea = '';
$faceit = '';
$matchmaking = '';
foreach ( $_POST['multipleSelect'] as $value ) {
    if ( $value == 'esea' )        { $esea = 'ESEA';  }
    if ( $value == 'faceit' )      { $faceit= 'FACEIT';      }
    if ( $value == 'matchmaking' ) { $matchmaking= 'Matchmaking'; }
}  

Then i send it to my database like this:

$sql = "INSERT INTO users ( profilename,region, age, ranks, esea, faceit, matchmaking, textarea1 ) VALUES ( 
'{$mysqli->real_escape_string($_POST['profilename'])}',
'{$mysqli->real_escape_string($_POST['region'])}',
'{$mysqli->real_escape_string($_POST['age'])}',
'{$mysqli->real_escape_string($_POST['ranks'])}',
$esea,
$faceit,
$matchmaking,
'{$mysqli->real_escape_string($_POST['textarea1'])}')";


$insert = $mysqli->query($sql);

Unfortunately this code doesnt work. It just sends the rest of the values but not the string from the multipleSelect question. I don't know what im doing wrong?

1 Answer 1

1

As those fields are VARCHAR you need to wrap their values in quotes like this, this applies to any text type datatype used in a MYSQL Table

$sql = "INSERT INTO users ( profilename,region, age, ranks, esea, faceit, matchmaking, textarea1 ) VALUES ( 
        '{$mysqli->real_escape_string($_POST['profilename'])}',
        '{$mysqli->real_escape_string($_POST['region'])}',
        '{$mysqli->real_escape_string($_POST['age'])}',
        '{$mysqli->real_escape_string($_POST['ranks'])}',
        '$esea',
        '$faceit',
        '$matchmaking',
        '{$mysqli->real_escape_string($_POST['textarea1'])}')";


$insert = $mysqli->query($sql);
Sign up to request clarification or add additional context in comments.

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.