在sql中插入复选框并使用php回显它们

时间:2013-12-28 21:42:14

标签: php sql checkbox

我正在构建一个表单来输入不同的数据来将事件(名称,日期,事件类型)表征到sql db中,以便稍后可以按位置搜索它们。

下面是html表单,php文件(insert.php)将数据放入db,最后是搜索结果的php。 问题出在复选框上,我试图让它成为多个值存储在同一个sql列中,有点像可以在搜索中使用的标签。

我现在的问题是,当我处理表单时,复选框列中我的sql表中的条目显示为“Array”,然后它不会显示在搜索结果上。 我不知道问题是我在表格中输入数据不正确,还是结果显示是问题。

任何帮助都非常感激。

FORM

<form class="form-horizontal" enctype="multipart/form-data" action="insert.php" method="post">

 <label class="control-label">  Name Of The Event
  <input type="text" placeholder="Name of the event" name="name"></label>

<div class="control-group">
<label class="control-label">  Address</label>
  <input type="text" placeholder="address" name="address">
<label class="control-label">  City</label>
<input type="text" name="citycountry" class="location" autocomplete="off" id="searchTextField" placeholder="City">         
      <div id="map_canvas"></div>
</div>
</div>

<div class="control-group">
<label class="control-label">  Description</label>
<div class="controls">
<input type="text" name="description" placeholder="Add details about this event.">
</div>
</div>

<div class="control-group">
<label class="control-label">  Start</label>
<div class="controls">
 <div  id="sandbox-container">
<input type="date" name="startdate" placeholder="mm/dd/yyyy"> <!--<span class="add-on"><i class="icon-th"></i></span>-->
</div>         <input type="time" class="span2" name="starttime">
</div>
</div>
     <div class="control-group">
<label class="control-label">  End</label>
<div class="controls">
 <div  id="sandbox-container">
<input type="date" name="enddate" placeholder="mm/dd/yyyy"> <!--<span class="add-on"><i class="icon-th"></i></span>-->
</div>       <input type="time" name="endtime">
</div> 
</div>


<div class="control-group">
<label class="control-label">  Add Image</label>
<div class="input-append">
<input type="file" name="picture"></br>
</div> </div>

<div class="control-group">
<div class="controls">
  <label class="checkbox">
    <input type="checkbox" name="eventtype[]" value="Lesson"> Dance Lesson
  </label>
    <label class="checkbox">
    <input type="checkbox" name="eventtype[]" value="Social"> Social Dancing
  </label>
    <label class="checkbox">
    <input type="checkbox" name="eventtype[]" value="Competition"> Competition
  </label>
    <label class="checkbox">
    <input type="checkbox" name="eventtype[]" value="Show"> Show
  </label>
    <label class="checkbox">
    <input type="checkbox" name="eventtype[]" value="Convention"> Dance Convention
  </label>

</div>
</div>  <div class="control-group">
<div class="controls">
<input class="btn btn-info" type="submit">
  </div>
</div> 
</form>

INSERT.PHP

<?php   

//preparing the patch to copy the uploaded file
$target_path = "upload/";
$target_path = $target_path . uniqid();  

//adding the name of the file, finishing the path
$target_path = $target_path . $_FILES["file"]. basename( $_FILES['picture']['name']); 

//moving the file to the folder
if(move_uploaded_file($_FILES['picture']['tmp_name'], "$target_path")) {
  echo "The file ".  basename( $_FILES['picture']['name']). 
  " has been uploaded";
}

 else{
  echo "There was an error uploading the file, please try again!";
 }

 //preparing the query to insert the values
 $query = "INSERT INTO complete_table (eventtype, name, address, citycountry, starttime, startdate, endtime, enddate, description,picture) VALUES ( '$_POST[eventtype]' ,'$_POST[name]','$_POST[address]','$_POST[citycountry]','$_POST[starttime]', '$_POST[startdate]','$_POST[enddate]','$_POST[endtime]','$_POST[description]', '". $target_path ."')";


//opening connection to db
$link = mysql_connect('localhost', 'root', ' ');
if (!$link) {
   die('Could not connect: ' . mysql_error());
}

 //selecting a db
mysql_select_db("wcs_venues", $link) or die(mysql_error());

//running the query
$result = mysql_query($query) or die (mysql_error());

//closing the connection
mysql_close($link);

搜索结果

<?php
mysql_connect ("localhost", "root"," ")  or die (mysql_error());
mysql_select_db("wcs_venues") or die(mysql_error());
$term = $_POST['term'];


$sql = mysql_query("SELECT * FROM complete_table WHERE citycountry LIKE '%$term%'")or die(mysql_error());


}
/* echo '<td width="250px;" style="float:left;"><img src="http://s3-media3.ak.yelpcdn.com/bphoto/CJziwp9QDcSPuYVjy4uasg/l.jpg"  class="bigthumb" style="margin:30px; width:114px; height:auto;"></td>';*/    
echo '<table class="table table-bordered">';
echo ' <tbody> <tr> <td> Name of the venue </td>';
echo '<td> </td> </tr>';
echo '<tr> <td> Address </td>';
echo '<td>'.$row['citycountry'];
echo '</td> </tr>';
echo '<tr> <td> Date </td>';
echo '<td>'.$row['date'];
echo '</td> </tr>';
echo '<tr> <td> Picture </td>';
echo '<td><img src=http://localhost/theme/'.$row['picture'] .'> </td>'; 
echo '</td> </tr>';
echo '<tr> <td> Eventtype </td>';

 if(isset($_POST["eventtype"])) //checks if any interest is checked
{
    foreach($_POST["eventtype"] as $value) //Iterate the interest array and get the values
    {
        echo '<td>'.$value;  //print the values
    }
 }   
echo '</td> </tr>';

echo '</table>';
}

?>

1 个答案:

答案 0 :(得分:0)

将$ _POST ['eventtype']转换为字符串:

if( isset( $_POST['eventtype'] ) && is_array( $_POST['eventtype'] )) {
    $eventtypes = join( ',', $_POST['eventtype'] );
} else {
    $eventtypes = '';
}

在INSERT中使用$ eventtypes。