0

I have made a dropdown list to choose the priority level of one contact, priority is an attribute to the table person and it has the type ENUM. The dropdown list is in a HTML-Form and I want to submit it to the MySQL database. But somehow the value can't be inserted. Can someone help me?

Code

//PHP
  if(isset($_POST["priority"]) && $_POST["priority"] == 'not_selected'){
    $err_priority = "Please assign the priority level!";
  }
  else {
    $priority = $_POST["priority"];
  }

  $sql1 = "INSERT INTO person (priority) VALUES (?)";
if ($stmt1 = mysqli_prepare($conn, $sql1) or die(mysqli_error($conn))) {
          mysqli_stmt_bind_param($stmt1, 's', $priority);
}
mysqli_stmt_execute($stmt1);
mysqli_stmt_close($stmt1);



//HTML-Form
  <div class="form-group <?php echo(!empty($err_priority)) ? 'has-error' : ''; ?>">
    <label>Priority</label>
    <?php
      echo '<select name="priority">';
      $sql = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'person' AND COLUMN_NAME = 'priority'";
      $result = mysqli_query($conn, $sql);
      $row = mysqli_fetch_array($result);
      $enumlist = explode(  ",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

      foreach ($enumlist as $value_p) {
        echo '<option value="$value_p">'.$value_p.'</option>';
        }
        echo '</select>';
    ?>
    <span class="help-block"><?php echo "$err_priority"; ?></span>
  </div>
<div class="form-group">
    <input type="submit" class="btnsml" value="Add Contact">
</div>

Where did I make a mistake?

3
  • 3
    Horrible code design...free hint: dont mix layout, logic and data.. read/think about separation of concerns instead Commented May 29, 2018 at 13:06
  • This code is very hard to read, because you switch in and out of PHP far too often. You should really do all of your layout in plain HTML, and then fill in the data with PHP. Commented May 29, 2018 at 13:07
  • @B001ᛦ Thank you for your advices. I’m very new to PHP so I still have a lot to learn :P I was playing with some ideas and basically just wrote what came into my mind into to the code...I’ll get rid of this bad habit ^^ Commented May 29, 2018 at 13:12

2 Answers 2

1

The problem is here:

echo '<option value="$value_p">'.$value_p.'</option >';

You're not interpolating the contents of $value_p. Change it, like this:

echo '<option value="' . $value_p . '">' . $value_p . '</option >';
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you are not setting the values in the select tag.

 echo '<select>';
 foreach ($enumlist as $value_p) {
        echo '<option value="'.$value_p.'">'.$value_p.'</value>';
 }
 echo '</select>';

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.