Message show when all dropdown items have inserted - php

I have a problem. I want when inserted a dropdown item, then this item not able to show in a drop-down in next time. It's working well. Now I want when every dropdown item has inserted, then need to show this message 'All users have assigned'. I am trying but its my code is not working. How can I do that?
Thanks!
<?php
$user_info2 = $wpdb->get_results( "SELECT * FROM user_contact_info WHERE user_company = $company_id" );
if (empty($user_info2))
{
echo "<h2>All users have assigned</h2>";
}
else
{
?>
<select type="text" name="ap_user" id="county" style="font-family: 'FontAwesome', Arial;" class="form-control" required >
<option disabled selected value> &#xf00c Select User</option>
<?php
foreach ($user_info2 as $user)
{
$assigns = $wpdb->get_results( "SELECT * from user_searches WHERE ap_user = $user->id AND ap_company = $company_id AND ap_project = $id " );
// foreach ($assigns as $assigned)
// {
if(!$assigns)
{
echo '<option value="'.$user->id.'" >'.$user->user_name.'</option>';
}
// }
}
?>
</select>
<?php
}
?>

Related

Insert data in table (diary) and update the data in the (category) table mysql

I am trying to insert the image name in the table (diary) and at the same time I am trying to update the image name in the table (category). but the value is not updating the in the category. I tried all the available solutions on the internet but nothing worked can someone help me?
I want to update the (category) table "cat_img" whenever the user adds a new image to the table (diary) diary_thumbnail_image.
Please help me it will be appreciated.
here is my code add_diary.php
<?php include 'header.php';
if ($admin != 1 && $admin != 2) {
header("location:index.php");
}
if (isset($_SESSION['user_data'])) {
// This will fetch the author id that is stored in index['0'].
$author_id = $_SESSION['user_data']['0'];
}
$sql = "SELECT * FROM categories";
$query = mysqli_query($config, $sql);
$sql_school = "SELECT * FROM school";
$query_school = mysqli_query($config, $sql_school);
?>
<form method="POST" action="" enctype="multipart/form-
data"
this diary:</strong></label>
<select class="form-control"
name="show_in_school">
<option value=""
name="show_in_school">Select School
Name</option>
<!-- $cats will fetch all the data that
is stored in the categories. -->
<?php while ($school_result =
mysqli_fetch_assoc($query_school)) { ?>
<option value="<?=
$school_result['school_title'] ?>">
<?= $school_result['school_title'] ?>
</option>
<?php } ?>
}
</select>
</div>
<div class="mb-3">
<input type="submit" name="add_diary"
value="Add" class="btn btn-primary">
<a class="btn btn-secondary"
href="diary_information.php">Back</a>
</div>
</form>
<?php
if(isset(add_diary)){
$select_cat = "SELECT * FROM categories";
$query_cat = mysqli_query($config, $select_cat);
$cat_result = mysqli_fetch_array($query_cat);
$get_cat_id = $cat_result['cat_id'];
// FACING THE ISSUE HERE
$cat_up = "UPDATE categories SET cat_name='$category'
cat_img='$filename' WHERE
cat_id='$get_cat_id'";
$query_up = mysqli_query($config, $cat_up);
if ($query_up) {
$msg = ["Post Publish Successfully", "alert-success"];
$_SESSION['msg'] = $msg;
// If the post publish successfully then redirect to
same page with a success message.
header("location:add_diary.php");
} else {
$msg = ["Failed, Please try again", "alert-danger"];
$_SESSION['msg'] = $msg;
// If the post is not able to post then redirect to the
same
page with an error message.
header("location:add_diary.php");
}
}
?>
I tried to insert using the id but didn't work and checked all the ways to insert and update that were available on the internet but didn't work
// This worked but it stays with the old image and doesn't update the new image.
$sql_up_cat = "UPDATE categories SET cat_img = (SELECT diary_thumbnail_image FROM diary WHERE cat_id = blog_id) ";
$query_up = mysqli_query($config, $sql_up_cat);
Actually, I was sending the value of the cat_name in integer to the database that's why it was not able to check the category name and was not updating the image.
I than changed it's value to the String.
<div class="mb-3">
<label><strong>Category:</strong></label>
<select class="form-control" name="category">
<option value="" name="category">Select
Category</option>
<!-- $cats will fetch all all the data that is store
in the categories. -->
<?php while ($cats = mysqli_fetch_assoc($query)) { ?
>
<option value="<?= $cats['cat_name'] ?>">
<?= $cats['cat_name'] ?>
</option>
<?php } ?>
}
</select>
</div>
Updated the query to this and then it worked
$cat_up = "UPDATE categories SET cat_img='$filename' WHERE cat_name='$category'";
$query_up = mysqli_query($config, $cat_up);

Populating Second Dropdown Based on first dropdown from the database table without using javascript

I am trying to create a form for the admin of an e-commerce site and the admin should be able to create a category and add new products.
I have two tables in my database from where I want to populate the dropdown list. I want the second dropdown to populate as I select the first drop-down value but I have to do it without the submit button.
This is the code for the form with two drop-downs:-
<form method="post" action="add_category.php">
<h4>Choose the root level:</h4>
<select name="rootLevel">
<?php
$conn = mysqli_connect("localhost","root","","store")
or die("Error in Connection on add_category");
$query = "SELECT * FROM root_category";
$result = mysqli_query($conn,$query) or die("Query failed add_category");
$id=1;
//echo $id;
//echo "Hello";
while($row = mysqli_fetch_assoc($result)){
global $id;
//echo "<h1>$id</h1>";
$id = $row['id'];
echo "<option name='rootLevel' value=$id>".$row['Name']."</option>";
//echo "<option>$id</option>";
}
?>
</select>
<br><br>
<h4>Choose the Level 1 Category:</h4>
<select name="level1">
<?php
global $id;
//echo "<option>".$_POST['rootLevel']."</option>";
$query_level1 = "Select * from level1_category Where P_id = $id";
$result1 = mysqli_query($conn,$query_level1) or die("Query failed level 1");
while($row = mysqli_fetch_assoc($result1)){
$id1 = $row['P_id'];
echo "<option name='level1' value=$id1>".$row['Name']."</option>";
}
?>
</select>
I have successfully populated the first drop-down and now I want to fetch the $id in 'resultValue' without the submit button.
You cant do this only with PHP. You have to use jquery OR Ajax to do this.
Please check this example page . This may help you
https://www.tutorialrepublic.com/faq/populate-state-dropdown-based-on-selection-in-country-dropdown-using-jquery.php
OR
https://www.codexworld.com/dynamic-dependent-select-box-using-jquery-ajax-php/
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$("select.country").change(function(){
var selectedCountry = $(".country option:selected").val();
$.ajax({
type: "POST",
url: "states.php",
data: { country : selectedCountry }
}).done(function(data){
$("#states").html(data);
});
});
});
</script>
</head>
<body>
<div class="form-group">
<label for="country" class="input__label">Country</label>
<select id="country" onchange="states(this.value);" name="country" class="country form-control login_text_field_bg input-style">
<option selected>Choose...</option>
<?php
$sql= $cnn->prepare("SELECT key_iso_code FROM country");
$sql->execute();
while($i = $sql-> fetch($cnn::FETCH_ASSOC)){
extract($i);
?>
<option><?php echo $key_iso_code ?></option>
<?php
}
?>
</select>
</div>
<div class="form-group col-md-4">
<label for="inputState" class="input__label">State/Province</label>
<select id="states" name="state" class="form-control input-style">
<option selected>Choose...</option>
</select>
</div>
</body>
<?php
include("PDOConnection.php");
if(isset($_POST["country"])){
// Capture selected country
$country = $_POST["country"];
// Display city dropdown based on country name
if($country !== 'Shoose...'){
$sql= $cnn->prepare("SELECT state.key_name FROM country INNER JOIN state ON country.key_id = state.key_country_id WHERE country.key_iso_code like '$country'");
$sql->execute();
while($i = $sql-> fetch($cnn::FETCH_ASSOC)){
extract($i);
echo "<option>". $key_name . "</option>";
}
}
}
?>

Save ID from dropdown list into other table

I have a form with fields and one dropdown list. The data from the dropdown list comes out the table: categorie. (fields are: catID, catName)
When I select a categorie from the drop down list and fill all the other input fields, it saves all the input fields and only the catName from the categorie table into the tabel: event.
How can I also save the selected catID from the categorie table into the event table?
Can someone helm me out here?
Regards, Benny
<?php require '../Connections/localhost.php'; ?>
<?php
if(isset($_POST['newEvent'])) {
session_start();
$eventName = $_POST['SelCatName'];
$eventSDate = $_POST['Event-StartDate'];
$eventSTime = $_POST['Event-StartTime'];
$eventEDate = $_POST['Event-EndDate'];
$eventETime = $_POST['Event-EndTime'];
$eventDescription = $_POST['Event-Description'];
$catID = $_POST["catID"];
$sql = $con->query("INSERT INTO event (eventName, eventSDate, eventSTime, eventEDate, eventETime, eventDescription, catID)Values('{$eventName}', '{$eventSDate}', '{$eventSTime}', '{$eventEDate}', '{$eventETime}', '{$eventDescription}', '{$catID}')");
header('Location: eventOK.php');
}
?>
<form action="" method="post" name="RegisterForm" id="RegisterForm">
<div class="FormElement">
<select name="SelCatName" class="TField" id="SelCatName">
<option selected="selected" id="0">--Selecteer een categorie--</option>
<?php
$GetAllCategories = $con->query("SELECT * FROM categorie");
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){
?>
<option id="<?php echo $ViewAllCategories['catID']; ?>"><?php echo $ViewAllCategories ['catName']; ?> </option>
<?php } ?>
</select>
You made a mistake in <option id="id"></option>
if you wanna take value from select child.
you must change id to value="" or you must add value="id" proper.
change your select box block completely like below..
<select name="SelCatName" class="TField" id="SelCatName">
<option selected="selected" id="0">--Selecteer een categorie--</option>
<?php
$GetAllCategories = $con->query("SELECT * FROM categorie");
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){
?>
<option value="<?PHP echo $ViewAllCategories['catID'].'-'.$ViewAllCategories['catName']; ?>"> <?PHP echo $ViewAllCategories['catName']; ?></option>
<?php } ?>
</select>
and a tip for mysql query... to the best of one's ability don't use "select * from" use it if you dont need to all colums better than "*"
$con->query("SELECT catID, catName FROM categorie");
PHP file
<?php require '../Connections/localhost.php'; ?>
<?php
if(isset($_POST['newEvent'])) {
session_start();
//$eventName = $_POST['SelCatName'];
$eventSDate = $_POST['Event-StartDate'];
$eventSTime = $_POST['Event-StartTime'];
$eventEDate = $_POST['Event-EndDate'];
$eventETime = $_POST['Event-EndTime'];
$eventDescription = $_POST['Event-Description'];
$catIDs = $_POST["SelCatName"];
$catID = explode("-", $catIDs);
$sql = $con->query("INSERT INTO event (eventName, eventSDate, eventSTime, eventEDate, eventETime, eventDescription, catID)Values('{$catID[1]}', '{$eventSDate}', '{$eventSTime}', '{$eventEDate}', '{$eventETime}', '{$eventDescription}', '{$catID[0]}')");
header('Location: eventOK.php');
}
?>
to me you are missusing the attribute of ID, Replace the ID for value: your option tag already have the id and name
<select name="SelCatName" class="TField" id="SelCatName">
your code will be:
<?php
$GetAllCategories = $con->query("SELECT * FROM categorie");
while ($ViewAllCategories = mysqli_fetch_array($GetAllCategories)){
?>
<option value="<?php echo $ViewAllCategories['catID']; ?>"> <?php echo $ViewAllCategories ['catName']; ?> </option>
<?php } ?>
Therefore you will find your post as only SelCatName = (numerical ID)
Now, if you need that the list of the catID and catName stores to the next page you can re-run your query in the next page and store them in an array.

Dropdown menu php with next/previous button

I have :
<select name="department" id="department" onchange="this.form.submit()">
<?php
do {
?>
<option value="<?php echo $row_department['dept_Name'] ?>"><?php echo $row_department['dept_Name'] == $department ? 'selected' : ''?><?php echo $row_department['dept_Name']
?></option>
<?php
} while ($row_department = mysql_fetch_assoc($department));
$rows = mysql_num_rows($department);
if($rows > 0) {
mysql_data_seek($department, 0);
$row_department = mysql_fetch_assoc($department);
}
?>
</select>
For the dropdown from the database named department.
Once dropdown selected, multiple file sets will viewed from database such as id, name, dept_name with next/previous button.
The problem is, whenever i clicked next/previous button, there no files viewed.
Dropdown must be selected for the 2nd time for view 2nd file set.
What should I do and how? I'm using Dreamweaver CS6.
Try this. You canno't search because you don't have a query to do that.
$deptName = $row['dept_Name'];
$sql = mysql_query("SELECT * FROM (table_name) WHERE (column_department_name) = '$deptName'");
while ($row_department = mysql_fetch_assoc($sql));
$rows = mysql_num_rows($row_department);
if($rows > 0) {
echo $rows['(your_column_name)'];
}else{
echo 'nothing';
}

listbox value goes 0 in database if not selected on edit mode in php

I have three tables
City: city_id, city_name
State: State_id, State_name
News: City_id, State_id, Headline, Story, Author etc.
I am Displaying data in textbox and listbox on edit button click Of Selected ID.
Data is displaying in listbox properly of selected id but when i click save button without change of listbox value it goes 0 in database.
If I don't change the listbox value and click save then it inserting 0 from listbox.
<?php
$data = 0;
if(isset($_GET['edit']))
{
$id = clean($_GET['edit']);
mysql_set_charset('utf8');
$sql="SELECT city_name,state_name,category_name,headline,author,story,source,photo,date from news left join
city on news.city_id=city.city_id left join state on news.state_id=state.state_id left join category on news.cat_id=category.category_id where id = '$id'";
$result=mysql_query($sql);
$data=mysql_fetch_array($result);
}
?>
<?php
$cat = $Admin->select($Admin->cat_table,'','','');
?>
<select name="cat_id" class="select" required="">
<option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>"><?php if(isset($_GET['edit'])){echo $data['category_name'];}?></option>
<?php
foreach($cat as $load_category)
{
?>
<option value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
<?php }?>
</select>
<?php
$errors = array();
$Admin = new admins;
if(isset($_POST['save']))
{
$table = $Admin->news_table;
if(isset ($_GET['edit']))
{
$id = clean($_GET['edit']);
$cond = "id = '$id'";
if($Admin->save_advance($table,$_POST,'',$cond))
{
$_SESSION['message'] = "News Updated Successfully";
header("Location:add_news.php");
exit;
}
}
}
?>
What am I doing wrong?
Tell me if I'm wrong but this line
<option value="<?php if(isset($_GET['edit'])){ echo $data['category_name']; }?>">
<?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option>
seems strange to me, it sets the category name as value for first option in the select. Basically, if you don't change the listbox, you are trying to update a row where the category_id is a category_name.
This should be:
<option value="0">
<?php if(isset($_GET['edit'])){echo $data['category_name'];}?>
</option>
But if you have to update something you can't do that without a value. So add some kind of validation that forbids to click the save button without setting a value in the select, or just do something like this (adds a default category to the listbox)
<select name="cat_id" class="select" required>
<?php
$counter = 0;
foreach($cat as $load_category){ ?>
<option <?php echo ($counter == 0) ? 'selected' : ''; ?> value="<?php echo $load_category["category_id"]; ?>"><?php echo $load_category["category_name"]; ?></option>
<?php
//increments counter
$counter++; }?>
</select>

Categories