PHP Multiple Checkbox; - php

I have problem, I am trying to create a form with a few checkboxes, each assigned a different value, i just can update value first and last, between not working, can you help me?
Check.php
<div class="top-on">
<div class="top-on1">
<p class="text-center"> <?php echo $row['username'];?></p>
<br>
<select class="form-control col-sm-12" name="edit_level">
<?php
global $pdo;
$sql = $pdo->query("SELECT * FROM level");
while ( $row_c = $sql->fetch(PDO::FETCH_ASSOC) ) {
?>
<option <?php if($row_c["level"]==$row["level"])
{
echo "selected=\"selected\"";
}?>
value="<?php echo $row_c['level']?>"> <?php echo $row_c['name'];?> </opition>
<?php } ?>
</select>
</div>
<label style="float: right;">
<input type="checkbox" class="checkbox" name="idlevel[]" value="<?php echo $row['id'];?>"> </label>
<div class="clearfix"> </div>
</div>
page Control.php
function edit_level(){
global $pdo;
$sql1="SELECT * From user ";
$stmt1 = $pdo->query($sql1);
if(isset($this->btnlevel))
{
for($i=0;$i<$stmt1->rowCount();$i++){
$elve=$this->idlevel[$i];
$sql ="UPDATE user SET level='$this->editlevel' WHERE id='".$elve."'";
$upt = $pdo->prepare($sql);
$upt->execute();
}
}
}

Related

All the checkboxes checked by previous user must be disabled

I want all the records(i.e seats selected by the user) should be disabled but this code only fetches the last record and disables it.
<?php
$sqlcheck = "select * from booknow where hotel ='$test'";
$resultcheck = mysqli_query($conn,$sqlcheck);
while($rowcheck = mysqli_fetch_array($resultcheck))
{
$checkedcheck = $rowcheck['book'];
$expcheck=explode(",",$checkedcheck);
}
?>
<?php $i=1;
while ($i<=$tab)
{?>
<div class="TWO">
<div class="check"style="height:40px;width:120px;">
<div class="seconda">
</div>
<div class="secondb">
<input type ="checkbox"name="checky[]"class="tabtwo"style="width:30px;height:30px;"
value="<?php echo "Two-seater A".$i;?>"
<?php if(in_array("Two-seater A".$i, $expcheck)) {?>
onclick="this.checked=this.defaultChecked"<?php } else {echo "none";}?>>
</div>
<div class="secondc">
</div>
<label for="c1"style="margin-left:40px;"><b>A<?php echo$i?></b>
</label>
</div>
</div>
<?php
$i++;
}
?>
Try below code
while($rowcheck = mysqli_fetch_array($resultcheck))
{
$checkedcheck = $rowcheck['book'];
$expcheck[] =explode(",",$checkedcheck);
}

Displaying search results with pagination in php

How do make my search work from a class... i can display my search results on the major page but i want it to display all results and if it is more than the paginated result, client can easily move to another page.
<?php
// if it's going to need the database, then it's
//probably smart to require it before we start.
#require_once(LIB_PATH.DS.'database.php');
class Product extends DatabaseObject {
protected static $table_name="products";
protected static $db_fields=array('id', 'product_id', 'image', 'title', 'slug', 'description', 'price');
public $id;
public $product_id;
public $image;
public $title;
public $slug;
public $description;
public $price;
public static function search(){
global $database;
$sql = "SELECT * FROM products WHERE image LIKE '{%$search%'";
$sql .= " OR title LIKE '{%$search%}'";
$sql .= " OR slug LIKE '%$search%'";
$sql .= " OR description LIKE '%$search%'";
$sql .= " OR price LIKE '%$search%'";
$total_count = count($sql);
$result_set = $database->query($total_count);
$row = $database->fetch_assoc($result_set);
return array_shift($row);
}
?>
Please check my code... i am a bit confused.
<?php if(empty($_POST['search'])){
$session->message("<div class='error-msg'>Search cannot be empty</div>");
redirect_to('photos.php');
}
?>
<?php include_layout_template('header2.php'); ?>
<div class="container">
<div class="row">
<?php
if(isset($_POST['submit'])){
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page ($per_page)
$per_page = 10;
// 3. total record count ($total_count)
//$total_count = Product::count_all();
$total_count = Product::search();
// Find all photos
// use pagination instead
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
$search = $database->escape_value($_POST['search']);
// $sql = "SELECT * FROM products WHERE image LIKE '%$search%'";
// $sql .= " OR title LIKE '%$search%'";
// $sql .= " OR slug LIKE '%$search%'";
// $sql .= " OR description LIKE '%$search%'";
// $sql .= " OR price LIKE '%$search%'";
// $sql .= " LIMIT {$per_page} ";
// $sql .= "OFFSET {$pagination->offset()}";
$search = new Product();
$search->search = $search;
$photos = Product::find_by_sql($search);
//$total_count = count($photos);
//echo $numresults = '<p class="error-msg">There are '.$total_count.' results in your search</p><br/><br/>';
foreach ($photos as $photo): ?>
<div class="col-md-4 col-sm-6">
<div class="row">
<div id="pagination" style="clear: both;">
<nav aria-label="Page navigation example">
<ul class="pagination">
<?php
for($i=1; $i <= $pagination->total_pages(); $i++){
if($i == $page) {
// echo " <span class=\"selected\">{$i}</span> ";
// } else{
// echo " {$i} ";
}
}
if($pagination->total_pages() > 1) {
if($pagination->has_previous_page()) {
echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
echo $pagination->previous_page();
echo "\">« Previous</a></li> ";
}
if($pagination->has_next_page()){
echo "<li class='page-item'><a class='page-link' href=\"search.php?page=";
echo $pagination->next_page();
echo "\">Next »</a></li>";
}
}
?>
</ul>
</nav>
</div>
</div>
<div class="thumbnail">
<form method="post" action="cart.php?action=add&id=<?php echo $photo->id; ?>" role="form" class="form-vertical">
<a href="order_review.php?id=<?php echo $photo->id; ?>"><img src="<?php echo $photo->image_path();
?>" class="img-thumbnail" alt="responsive image"></a>
<div class="caption">
<h3 class="text-info text-center"><?php echo $photo->title; ?></h3>
<p class="text-muted text-center price card-header"><span class="currency">N</span><?php echo $photo->price; ?></p>
<p class="text-center"><em><?php echo $photo->description; ?></em></p>
<input type="hidden" name="id" class="form-control" value="<?php echo $photo->id; ?>" />
<input type="hidden" name="title" class="form-control" value="<?php echo $photo->title; ?>" />
<input type="hidden" name="slug" class="form-control" value="<?php echo $photo->slug; ?>" />
<input type="hidden" name="description" class="form-control" value="<?php echo $photo->description; ?>" />
<input type="hidden" name="price" class="form-control" value="<?php echo $photo->price; ?>"/>
<div class="col-sm-4 mx-auto d-block">
<select name="quantity" class="form-control" name="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<div class="col-sm-4 col-sm-push-3 mx-auto d-block">
<input type="submit" name="add_to_cart" class="btn bg-success" value="Add to cart" />
</div><br/>
</form>
</div>
</div>
<?php endforeach; }
?>
</div>
</div>
PLease take note of the code i commented out... I am a bit confused.

convert array object from database to string

I'm currently coding a blog to get experience with php.
In the edit.php I want to give the category of a post if a post has one.
This is the edit.php:
<div class="categories-heading">
<?php if (!empty($entry->c_Id)): ?>
<div class="category-heading">
<h3>Category: <?php echo e($categoryFromId); ?></h3>
</div>
<div class="category-heading">
<h3>change category</h3>
</div>
</div>
<form method="post">
<div class="categories-post-edit">
<?php foreach($categories as $cat): ?>
<div class="category-post-edit">
<input type="radio" name="category" value="<?php echo e($cat->id); ?>">
<label> <?php echo e($cat->category); ?></label>
</input>
</div>
<?php endforeach; ?>
</div>
</form>
<?php else: ?>
<div class="category-heading">
<h3>choose category</h3>
</div>
</div>
<form method="post">
<div class="categories-post-edit">
<?php foreach($categories as $cat): ?>
<div class="category-post-edit">
<input type="radio" name="category" value="<?php echo e($cat->id); ?>">
<label> <?php echo e($cat->category); ?></label>
</input>
</div>
<?php endforeach; ?>
This is the part of the function edit() in the PostsAdminController.php which is relevant for this:
if(!empty($_POST['title']) AND !empty($_POST['subheading']) AND
!empty($_POST['content'])
AND !empty($_POST['category'])) {
$entry->title = $_POST['title'];
$entry->subheading = $_POST['subheading'];
$entry->content = $_POST['content'];
$entry->c_Id = $_POST['category'];
$this->postsRepository->update($entry);
$savedSuccess = true;
}
$categoryFId = $this->categoryRepository->oneCategory($entry->c_Id);
$categoryFromId = $categoryFId['category'];
var_dump($categoryFromId);
$this->render("post/admin/edit", [
'entry' => $entry,
'savedSuccess' => $savedSuccess,
'categories' => $categories,
'categoryFromId' => $categoryFromId
]);
}
And this is the function oneCategory in the CategoryRepository.php that interacts with the database.
public function oneCategory($id)
{
$table = $this->getTableName();
$model = $this->getModelName();
$stmt = $this->pdo->prepare("SELECT `category` FROM `$table`
WHERE id = :id");
$stmt->setFetchMode(PDO::FETCH_CLASS, $model);
$oneCategory = $stmt->fetch(PDO::FETCH_CLASS);
return $oneCategory;
}
oneCategory() gives out an array no matter if I put SELECT * or SELECT category in the query, that's why I put $categoryFId['category'] into a variable after.
It works, but I think there must be a more easy or quicker way (like special functions or so), I just didn't manage to find what I search for

How to get parent name from its child?

I am going to build a dynamic product category with their subs for my final year project. I tried the tree way but somehow it make me confused so i decided to make it as simple. I want to display Clothe's parent name.I want to display it like this
Can i do it so ?
Here is my PHP code
<form method="post" action="product_category_add_exec.php" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-level" class="control-label"> Parent Category</label>
<select class="form-control" name="admin_lid" required="">
<option></option>
<?php
$sql_pcat = "SELECT * FROM product_category";
$select_pcat = mysqli_query($db,$sql_pcat) or die (mysqli_error().$sql_pcat);
$x =1;
while($list_pcat = mysqli_fetch_array($select_pcat))
{
$product_cat_id = $list_pcat['product_cat_id'];
$parent_id = $list_pcat['parent_id'];
$product_cat_name = $list_pcat['product_cat_name'];
?>
<?php
if ($parent_id == 0)
{
?>
<option value = "<?php echo $product_cat_id;?>"><?php echo $product_cat_name; ?></option>
<?php
} else
{
$sql_cat = "SELECT * FROM product_category WHERE parent_id= $parent_id ORDER BY product_cat_name ASC";
$select_cat = mysqli_query($db,$sql_cat) or die (mysqli_error().$sql_cat);
$list_cat = mysqli_fetch_array($select_cat);
$product_cat_id = $list_cat['product_cat_id'];
$parent_id = $list_cat['parent_id'];
$product_cat_name = $list_cat['product_cat_name'];
?>
<option value = "<?php echo $parent_id;?>">--<?php echo $parent_id;?><?php echo $product_cat_name; ?></option>
<?php
}
?>
<?php
$x++;
}
?>
</select>
</div>
<div class="form-group">
<label for="recipient-category" class="control-label">Product Name </label>
<input type="text" class="form-control" id="recipient-category" name="product_cat_name">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-info">Add</button>
<button type ="reset" class ="btn btn-danger">Reset</button>
</div>
</form>
This is my database table
I want to make it appear like this

PHP Update Query using mysqli function

Here I have to update only category,sd,fd,assignto,reviewed by and file upload. So when the user clicks on update button it will go to the update.php and updates the necessary fields. But its not executing and its showing an a warning:
Warning: mysqli_stmt::bind_param(): Number of variables doesn't match
number of parameters in prepared statement
Can somebody tell me what's wrong?
updateview.php
<form action="update.php" method="post" enctype="multipart/form-data" novalidate>
<?php
include_once('dbconn.php');
$srn = $_GET['srn'];
if($stmt = $mysqli->prepare("SELECT srn, client, type, fy, category, sd, fd, assignto, edoc, reviewed, upload FROM main WHERE srn=?")){
$stmt->bind_param("s",$_GET["srn"]);
$stmt->execute();
$stmt->bind_result($srn,$client,$type,$fy,$category,$sd,$fd,$assignto,$edoc,$reviewed,$upload);
$stmt->fetch();
$stmt->close();
}
?>
<label> <span>SRN</span>
<input name="srn" type="text" id="srn" size="15" readonly="readonly" maxlength="40" value="<?php echo $srn; ?>"/>
</label>
<label> <span>Client</span>
<select class="required" name="client" disabled="disabled"/>
<?php
if($stmt = $mysqli->prepare("SELECT cname FROM client")){
$stmt->execute();
$stmt->bind_result($cname);
while($stmt->fetch()){
?>
<option value="<?php echo $cname; ?>" <?php if($cname==$client){ echo "selected"; } ?>> <?php echo $cname; ?> </option>
<?php
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF PREPARED STATEMENT OF CLIENT */
?>
</select>
<input type="hidden" name="client" value = "<?php echo $row['client']; ?>" />
</label>
<label> <span>Type</span>
<select class="required" name="type" disabled="disabled"/>
<?php
if($stmt = $mysqli->prepare("SELECT type FROM entity")){
$stmt->execute();
$stmt->bind_result($type);
while($stmt->fetch()){
?>
<option value="<?php echo $type; ?>" <?php if($type==$type){ echo "selected"; } ?>> <?php echo $type; ?> </option>
<?php
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF PREPARED STATEMENT */
?>
</select>
<input type="hidden" name="type" value = "<?php echo $row['type']; ?>" />
</label>
<label> <span>Financial Year</span>
<select class="required" name="fy" disabled="disabled"/>
<?php
if($stmt = $mysqli->prepare("SELECT year FROM fy")){
$stmt->execute();
$stmt->bind_result($year);
while($stmt->fetch()){
?>
<option value="<?php echo $year; ?>" <?php if($year==$fy){ echo "selected"; } ?>> <?php echo $year; ?> </option>
<?php
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF PREPARED STATEMENT OF */
?>
</select>
<input type="hidden" name="fy" value = "<?php echo $row['fy']; ?>" />
</label>
<label> <span>Category</span>
<select class="required" name="category"/>
<?php
if($stmt = $mysqli->prepare("SELECT name FROM category")){
$stmt->execute();
$stmt->bind_result($name);
while($stmt->fetch()){
?>
<option value="<?php echo $name; ?>" <?php if($name==$category){ echo "selected"; } ?>> <?php echo $name; ?> </option>
<?php
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF PREPARED STATEMENT OF CATEGORY */
?>
</select>
</label>
<label> <span>Short Description</span>
<textarea required="required" name='sd'><?php echo $sd; ?></textarea>
</label>
<label> <span>Full Description</span>
<textarea required="required" name='fd'><?php echo $fd; ?></textarea>
</label>
<label> <span>Assign To</span>
<select class="required" name="assignto"/>
<?php
if($stmt = $mysqli->prepare("SELECT name FROM assignto")){
$stmt->execute();
$stmt->bind_result($name);
while($stmt->fetch()){
?>
<option value="<?php echo $name; ?>" <?php if($name==$name){ echo "selected"; } ?>> <?php echo $name; ?> </option>
<?php
}
$stmt->close();
}
?>
</select>
</label>
<label> <span>EDOC</span>
<input name="edoc" type="text" id="edoc" size="15" readonly="readonly" maxlength="40" value="<?php echo $edoc; ?>"/>
</label>
<label> <span>Reviewed By</span>
<select class="required" name="reviewed"/>
<?php
if($stmt = $mysqli->prepare("SELECT type FROM entity")){
$stmt->execute();
$stmt->bind_result($type);
while($stmt->fetch()){
?>
<option value="<?php echo $type; ?>" <?php if($type==$reviewed){ echo "selected"; } ?>> <?php echo $type; ?> </option>
<?php
}
$stmt->close();
}
?>
</select>
</label>
<label>
<span>Input Attachment</span>
<input type="file" name ="filename" required>
</label>
<button id='cancel' type='cancel'>Cancel</button>
<button id='send' type='submit'>Update</button>
</form>
update.php
<?php
include('dbconn.php');
$srn = $_POST['srn'];
$client = $_POST['client'];
$type = $_POST['type'];
$fy = $_POST['fy'];
$category = $_POST['category'];
$sd = $_POST['sd'];
$fd = $_POST['fd'];
$assignto = $_POST['assignto'];
$edoc = $_POST['edoc'];
$reviewed = $_POST['reviewed'];
$stmt = $mysqli->prepare("UPDATE main SET category=?,sd=?,fd=?,assignto=?,reviewed=?,upload=? WHERE srn=?");
$stmt->bind_param('sssssb',$srn,$category, $sd,$fd,$assignto,$reviewed,$upload);
$stmt->execute();
?>
dbconn.php
<?php
$host = "localhost";
$user = "root";
$pwd = "root";
$db = "eservice";
$mysqli = new mysqli($host,$user,$pwd,$db);
/* ESTABLISH CONNECTION */
if (mysqli_connect_errno()) {
echo "Failed to connect to mysql : " . mysqli_connect_error();
exit();
}
?>
$stmt->bind_param('sssssb',$category, $sd,$fd,$assignto,$reviewed,$upload);
here you got 1 types string and 6 params;
$stmt = $mysqli->prepare("UPDATE main SET category=?,sd=?,fd=?,assignto=?,reviewed=?,upload=? WHERE srn=?");
and here you got 7 ? Thats the problem, it should be :
$stmt->bind_param('sssssbd',$category, $sd,$fd,$assignto,$reviewed,$upload, $srn);
There are few problems in the code. Try solving them and see if the problem is solved.
select is not a self ending tag. Please correct that.
You are not initializing variable $uploads in update.php, It should be initialized with $_POST['filename'] This seems to be the actual problem
The select elements are readonly so the values won't be included in the posted form. Try adding hidden fields next to them and use these hidden fields to get the data instead in update.php

Categories