Click here to Skip to main content
15,887,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the product upload section where the user can select the category:
HTML
<p class="admin-estate-add-section-input">
   Parent category
 </p>
 
 <option value=""<?=(($parent == '')?' selected':'')?>>
 <?php while($p = mysqli_fetch_assoc($parentQuery)): ?>
 <option value="<?=$p['id'];?>"<?=(($parent == $p['id'])?' selected':'')?>><?=$p['category'];?>
 <?php endwhile; ?>
 
 <br>
 <br>
 <p class="admin-estate-add-section-input">
    Child category
 </p>
 
 <br>
 <br>


This is the jQuery function that calls the child_categories.php file

PHP
function get_child_options(selected){
 if(typeof selected === 'undefined'){
 var selected = '';
 }
 var parentID = jQuery('#parent').val();
 jQuery.ajax({
 url: '/tartalomkezelo/admin/parsers/child_categories.php',
 type: 'POST',
 data: {parentID : parentID, selected: selected},
 success: function(data){
 jQuery('#child').html(data);
 },
 error: function(){alert("Something went wrong! Can't select category.")},
 });

 }

 jQuery('select[name="parent"]').change(get_child_options);




This is the child_categories.php file:
PHP
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/tartalomkezelo/core/init.php';
$parentID = (int)$_POST['parentID'];
$selected = sanitize($_POST['selected']);
$childQuery = $db->query("SELECT * FROM categories WHERE parent = '$parentID' ORDER BY category");
ob_start(); ?>

 <?php while($child = mysqli_fetch_assoc($childQuery)): ?>
 <option value="<?=$child['id'];?>"<?=(($selected == $child['id'])?' selected':'');?>><?=$child['category'];?>
 <?php endwhile; ?>
<?php echo ob_get_clean(); ?>


And then the script for the product upload file:

JavaScript
jQuery('document').ready(function(){
 get_child_options('&lt;?=$category;?>');
 });


What I have tried:

I have tried to fetch data from database for editing but child category data did not fetched at the sametime with parent category
Posted
Updated 22-Jul-18 2:35am
v2

1 solution

PHP
$childQuery = $db->query("SELECT * FROM categories WHERE parent = '$parentID' ORDER BY category");

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900