I can not understand how to do such a thing, I'm already sitting for a long time. There is a table of events in which you need to enter the category ID and the text of this category. I have already tried it through the session, but the text of the category did not correspond to the ID.

Here is the form

<form action="editprofile.php" method="post" enctype="multipart/form-data"> <label>Название</label><br> <input type="text" name="name_event" required><br> <label>Категория</label><br> <select name="cat_event"> <?php $query = "SELECT `id`,`category` FROM `category_events`"; $categories = mysqli_query($conn,$query); ?> <option></option> <?php while($row = mysqli_fetch_array($categories)){ ?> <option value="<?php echo $row['id'] ?>"><?php echo $row['category'] ?></option> <?php } ?> </select></br> <label>Місто проведения</label><br> <select name="location_event"> <?php $query = "SELECT `id`,`title` FROM `location`"; $locations = mysqli_query($conn,$query); ?> <option></option> <?php while($row = mysqli_fetch_array($locations)){ ?> <option value="<?php echo $row['id'] ?>"><?php echo $row['title'] ?></option> <?php } ?> </select></br> <label>Изображение</label><br> <input required type="file" type="file" name="img_event" multiple accept="image/*,image/jpeg"><br> <label>Начало</label> - <input required type="date" name="start_event"> <label>Конец</label> - <input required type="date" name="end_event"><br> <label>Корот. описание</label><br> <textarea required name="preview_event" cols="80" rows="5"></textarea><br> <label>Полное описание</label><br> <textarea required name="full_event" cols="80" rows="10"></textarea><br> <input type="submit" name="btn_new_event"> </form> 

Here is the handler

  <?php // add event if(isset($_POST['btn_new_event'])){ $id_user = $_SESSION['user']['id']; $title = $_POST['name_event']; $category = ""; в эту переменную нужно занести название равное $id_cat(ниже) $id_cat = $_POST['cat_event']; $location = "";а в эту переменную нужно занести название равное $id_loc(ниже) $id_loc = $_POST['location_event']; $post_event = $_SESSION['user']['username']; // image $uploaddir= '../img/eventimg/'; $fot = $_FILES['img_event']['name']; $fot_dir = $uploaddir.$fot; $start_date_event = $_POST['start_event']; $end_date_event = $_POST['end_event']; $add_date_event = date("Ymd"); $preview_event = $_POST['preview_event']; $full_event = $_POST['full_event']; if (move_uploaded_file($_FILES['img_event']['tmp_name'], $fot_dir)) { $result_query_ev = "INSERT INTO `events` (`title`, `pre_event`, `image`, `id_cat_event`, `post_event`, `start_event`, `end_event`, `add_event`, `big_event`, `id_loc_event`, `text_category`, `text_location`, `id_user`) VALUES ('$title','$preview_event', '$fot_dir','$id_cat', '$post_event', '$start_date_event', '$end_date_event', '$add_date_event', '$full_event', '$id_loc', '$category', '$location', '$id_user')"; $result_add = mysqli_query($conn,$result_query_ev) or die(mysqli_error($conn)); if($result_add) { echo "<script>alert('Опубликовано!');location='user?id=".$id_user."'</script>"; } else { echo "<script>alert('Не опубликовано!');location='user?id=".$id_user."'</script>"; } }} ?> 
  • Immediately there was a question. Why do you need to save the text of the category? if only id of this category is enough? - Maxim Cherednik
  • I bring the category name to the post. - xelinel32 2:58 pm
  • For this you just need an id and pull the category through JOIN ?. - Maxim Cherednik
  • Chet did not think, thank you. But still, how do I want to, is it possible to do this? - xelinel32

1 answer 1

Well, you can try to replace

 <?php while($row = mysqli_fetch_array($categories)){ ?> <option value="<?php echo $row['id'] ?>"><?php echo $row['category'] ?></option> <?php } ?> 

on

 <?php while($row = mysqli_fetch_array($categories)){ ?> <option value="<?= $row['id'] . '_' . $row['category'] ?>"><?= $row['category'] ?></option> <?php } ?> 

and in the script

 $category_data = explode('_', $_POST['cat_event']); $id_cat = category_data[0]; $name_cat = category_data[1]; 
  • But this is one of the options))) - Maxim Cherednik
  • Thank you, I already did as you said above. - xelinel32 pm