【问题标题】:insert checkboxes in sql and echo them using php在 sql 中插入复选框并使用 php 回显它们
【发布时间】:2013-12-28 21:42:13
【问题描述】:

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

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

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

非常感谢任何帮助。

表格

<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>

插入.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>';
}

?>

【问题讨论】:

    标签: php sql checkbox


    【解决方案1】:

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

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

    在您的 INSERT 中使用 $eventtypes。

    【讨论】:

    • 单独说明:在尝试将变量插入数据库之前,您必须验证 $_POST、$_GET 等中的所有数据。
    • 您需要验证 $_POST、$_GET 中的所有数据是什么意思...对不起这里的大新手。
    • 验证您要插入数据库的所有用户输入。 You may want to read this discussion first.
    • 您的意思是出于安全原因?
    • 是的,出于安全原因。
    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2018-07-10
    相关资源
    最近更新 更多