关于PHP表单

时间:2017-08-11 10:09:06

标签: php html

我正在处理我的项目我希望将多个选定的选项值存储在数据库中但是当我一次选择多个选项时,我唯一存储在DB中的最后选择的值请帮助我在哪里我错了? 这是我的代码:

  <?php
  require_once('contact_fun.php'); 
  require_once('contact_banner_fun.php');
  $obj = new contact_banner();
  $crud = new contact_us();
  $banner = $obj->get_data_banner();
  if(!$banner){
  echo "No Banner";
  exit;}
  $path = 'http://localhost/THE_VELVET_WALK/contact/';
  $result = $crud->getData("SELECT * FROM tb_contactus");
  foreach ($result as $res) {
  $id = $res['id'];
  $name = $res['name'];
  $business = $res['business'];
  $email = $res['email'];
  $phone = $res['phone'];
  $message = $res['message'];
  $style = $res['style'];
  }
  if(isset($_POST['submit']))
   {    
 $name = $crud->escape_string($_POST['name']);
 $business = $crud->escape_string($_POST['business']);
 $email = $crud->escape_string($_POST['email']);  
 $phone = $crud->escape_string($_POST['phone']);
 $message = $crud->escape_string($_POST['message']);  
 $style = $crud->escape_string($_POST['style']);
 $result = $crud->execute(" INSERT INTO tb_contactus(name, business, email, 
 phone, message, style,  update_dt)VALUES ('$name','$business', '$email', 
'$phone', '$message', '$style',  now())");
 }?>
 <form action="" method="POST">
 <input id="00N7F000001F2j6" name="name" maxlength="40" type="text" 
 class="materialize-input">
 <input id="company" name="business" maxlength="40" type="text" 
 class="materialize-input">
 <input id="email" name="email" maxlength="40" type="email" 
 class="materialize-input">
 <input id="phone" name="phone" maxlength="40" type="text" maxlength="40" 
 class="materialize-input">
 <select multiple id="00N7F000001F2kO" name="style"  multiple="multiple" >
 <option name="consulting" name="styling" value=""selected disabled>image 
 consulting</option>
 <option  value="1">styling</option>
 <option  value="2">hair & make-up</option>
 <option  value="3">designing clothes</option>
 <option  value="4">wedding makeover</option>
 <option  value="5">personal shopper</option>
 <option  value="6">corporate services</option>
 </select>
 </form>

1 个答案:

答案 0 :(得分:1)

你必须在select的名称中指定它将是一个数组:

<select multiple id="00N7F000001F2kO" name="style[]"  multiple="multiple" >
 <option value=""selected disabled>image consulting</option>
 <option  value="1">styling</option>
 <option  value="2">hair & make-up</option>
 <option  value="3">designing clothes</option>
 <option  value="4">wedding makeover</option>
 <option  value="5">personal shopper</option>
 <option  value="6">corporate services</option>
 </select>

如果你想要一个像这样的字符串

,你可以内爆它
implode(',',$_POST['style']);

编辑: 改变这个

$style = $crud->escape_string($_POST['style']);

$style = $crud->escape_string(implode(',',$_POST['style']));