简体   繁体   中英

insert checkboxes in sql and echo them using php

I am building a form to input different data to characterize events (name, date, type of event) into a sql db so that they can be searched by location later on.

Below are the html form, the php file (insert.php) to put the data in the db and finally the php of the search result. The issue is with the checkboxes, I am trying to make it so that the multiple values get stored in the same sql column, a little like tags that can be used in the search.

My issue right now is that when I process the form, the entry in my sql table in the column of the checkbox says "Array" and then it does not display on the search result. I don't know if the issue is that I input the data incorrectly in the table or if the results display is the issue.

Any help much appreciated.

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

SEARCH RESULTS

<?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>';
}

?>

Convert $_POST['eventtype'] into string:

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

use $eventtypes in your INSERT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM