how can I edit/remove info in MySQL database via bootstrap table - php

I'm creating an admin panel for small ecommerce website (to practice) and I have no idea how to make php understand which id product I want to update after I click this little <i></i> element because I already use $_POST['<i> element name'] in order to detect whether or not it's ready to be updated, I know you cannot assign 2 names to one element otherwise I could use $id = $results['id']; as second name... any advices? I'm completely new in php. sorry if this post is stupid I just need some help...
$query="select * FROM productadd";
$result= mysqli_query($connection, $query);
$results = mysqli_fetch_array($result);
if(!empty($results['id'])) {
while($results = mysqli_fetch_array($result)){
$id = $results['id'];
$name = $results['name'];
$text = $results['text'];
$price = $results['price'];
$image = $results['image'];
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td>'.$text.'</td>
<td>$'.$price.'</td>
<td>
<img src="'.$image.'" style="style="display:block;" width="100%" height="100%" ">
</td>
<td>
<a class="add" value="asd" title="Add" name="edit" data-toggle="tooltip">
<i class="material-icons"></i>
</a>
<a class="edit" name="edit" title="Edit" data-toggle="tooltip">
<i class="material-icons"></i>
</a>
<a class="delete" title="Delete" data-toggle="tooltip">
<i class="material-icons"></i>
</a>
</td>
</tr>';
}
}

Related

Need help passing two php variables through a url to the next page

I'm new to Php but what I need this php code to do is to pass two variables through the URL like this
<td align="center"><a class="btn btn-danger" href="reserve.php?distroid=<?php echo $row["distroid"] echo $row['orderid']; ?>">Place Order</a></td>
When we try and do it with just distroid, it works completely fine. But I'm unsure how to pass both values.
Any help would be appreciated.
EDIT: SOLVED IT THANK YOU FOR THE HELP ALL
Try to add more key for the query string like this :
<td align="center">
<a class="btn btn-danger"
href="reserve.php?distroid=<?php echo $row["distroid"]; ?>&orderid=<?php echo $row['orderid']; ?>">
Place Order
</a>
</td>
Then you can get both distroid and orderid
Try this query string like this :
<td align="center">
<a class="btn btn-danger"
href="reserve.php?distroid=<?php echo $row["distroid"]."&orderid=".$row['orderid']; ?>">
Place Order
</a>
</td>
Then you can get both variable distroid and orderid
Hi you can try this anchor tag, it will work for you:
<a class="btn btn-danger" href="reserve.php?distroid=<?php echo $row['distroid'];
?>&orderid=<?php echo $row['orderid']; ?>">
Place Order
</a>

"Issue with updating a record using a button”

When I click on the update button, the SQL query updates the last record not the wanted one
when displaying several courses, I would like to give the user the right to confirm one of the courses, when I click on the update button, it updates the last record
<?php
$cc = mysqli_query($mysqli, "SELECT * from course WHERE c_email ='$emailu' AND course_situation='pending'");
while($ccc = mysqli_fetch_array($cc)) {
$idcourse = $ccc['idcourse'];
$city = $ccc['idville'];
$pickup = $ccc['course_depart'];
?>
<tr>
<td><?= $idcourse ?></td>
<td><?= $pickup ?></td>
<td><?php echo $ccc['course_date'] ?>, <?php echo
$ccc['course_heure'] ?></td>
<td>
<?php
if($ccc['course_situation'] == "pending") { ?>
<span class="badge badge-danger">Pending</span>
<?php } else { ?>
<span class="badge badge-success">Confirmed</span>
<?php } ?>
</td>
<td><?php echo $ccc['c_phone'] ?></td>
<td><?php echo $ccc['c_phone'] ?></td>
<td><?php echo $ccc['idclient'] ?></td>
<td>
<button type="submit" name="cancelcourse" class="btn btn-danger btn-xs"><i class="icon md-check" aria-hidden="true"></i> Update</button>
</tr>
<?php }
if(isset($_POST["cancelcourse"])) {
$query = "UPDATE course SET id='$idu' AND course_situation='confirmed' WHERE idcourse='$idcourse' ";
if(mysqli_query($mysqli, $query)) {
echo "<div class=' alert alert-success' style='padding-left:150px'>
<strong>Success!</strong> Event page updated.</a>.
</div>";
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=index.php">';
} else {
echo "<div class='alert alert-danger'>
<strong>ERROR!</strong> We invite you to try Again Later.
</div> " . mysqli_error($mysqli);
}
}
You generate button for each course, but all your buttons are equal, they don't contains any information about which course you want to update. You use variable $idcourse, but it is assigned inside while-loop that generates table - for each row that is shown this variable is changed, so after showing whole table it will contains id for last row.
Please remember, that each time your page is reloaded (like after clicking button) whole script is executed from beginning and no variables are stored, unless you pass it yourself.
Simplest solution:
1.Add id to button
<button type="submit" name="cancelcourse" class="btn btn-danger btn-xs" value="<?=htmlspecialchars($idcourse)?>">
2.Change query
$idcourseToRemove = mysqli_real_escape_string($mysqli, $_POST["cancelcourse"]);
$query = "UPDATE course SET id='$idu' AND course_situation='confirmed' WHERE idcourse='$idcourseToRemove' ";
And last advice: learn about SQL Injection and how to avoid it (like for example by mysqli_real_escape_string function)
UPDATE course SET id='$idu',course_situation='confirmed' WHERE
idcourse='$idcourse'

Show row collapsed in Bootstrap

$i = 0;
while($row = $statement->fetch()) {
$current_id = 'id-' . $i;
$i++;
echo "
<tr>
<td>
<p>
<button class='btn btn-primary'
type='button'
data-toggle='collapse'
data-target='#<?=$current_id?>'
aria-expanded='false'
aria-controls='Collapse'>
Button with data-target
</button>
</p>
<div class='collapse' id='<?=$current_id?>'>
<div class='card card-block'>
Here is the content for block which will be shown when the button. This uses button with data-target attribute for collapsing.
</div>
</div>
</td>
";
}
Where is my error? It doesn't collapse but I need the unique ids for my rows.
now it work. What i have to do now:
$ccid = "#$current_id";
$ccid2 = "$current_id";
And then i have put the variables inside of ID and Href Like this:
href='".$ccid."'
id='".$ccid2."'
That fixxed this problem. Thanks to all!

hide button after saving data to database

i created a group and what i wanted to do is when i join the group the join button should hide. but i dont know how to do it.. tbl_group is the list of created group and when you join the group the id will be saved on a diff table.. this code is for showing all list of groups.
group.php
<?php
$db = new Group($conn);
$res = $db->g_viewlist();
foreach ($res as $key => $value){
?>
<div class="col-lg-12" align="center" style="border:1.5px solid #59960b;padding-bottom:10px;padding-top:10px;">
<button class="btn2 btn-2 join" data-id="<?php echo $value['g_id']; ?>" data-toggle="modal" data-target="#joinModal" style="padding: 2px 2px;margin-left:50%"><strong> Join</strong></button>
<img src="./<?php echo $value['g_image']; ?>"class="pull-left" class="img-square" height="70" width="70" alt="Avatar">
<p align="left">
<strong class="font-1" style="color:#59960b;"><?php echo $value['g_name'];?> </strong><br>
<small style="font-family:courier,'new courier';" class="text">Member Since 2008<br></small>
<small style="font-family:courier,'new courier';" class="text-muted">Description:<?php echo $value['g_desc']; ?></small><br>
</p>
</div>
<?php
}
?>
SQL Query for showing all list of groups.
public function g_viewlist(){
$sql = "SELECT * FROM tbl_group ";
$result = $this->dbh->prepare($sql);
$result->execute();
$data = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)){
$data[] = $row;
}
return $data;
}
this table is where i store the user id and the group id this is a diff table..assuming the g_id = 25 from tbl_group and group_id =25 from tbljoingroup
You need to retrieve the array of groups user is a member of and inside your foreach loop just search through this list. If user is a member of $value['g_id'] just skip loop iteration with continue.
If you want to hide only the button and still display the rest of the code then do something like this:
<?php
$db = new Group($conn);
$res = $db->g_viewlist();
foreach ($res as $key => $value) {
?>
<div class="col-lg-12" align="center" style="border:1.5px solid #59960b;padding-bottom:10px;padding-top:10px;">
<?php if (!$user.memberOf($value['g_id'])) { ?>
<button class="btn2 btn-2 join" data-id="<?php echo $value['g_id']; ?>" data-toggle="modal" data-target="#joinModal" style="padding: 2px 2px;margin-left:50%"><strong> Join</strong></button>
<?php } ?>
<img src="./<?php echo $value['g_image']; ?>"class="pull-left" class="img-square" height="70" width="70" alt="Avatar">
<p align="left">
<strong class="font-1" style="color:#59960b;"><?php echo $value['g_name'];?> </strong><br>
<small style="font-family:courier,'new courier';" class="text">Member Since 2008<br></small>
<small style="font-family:courier,'new courier';" class="text-muted">Description:<?php echo $value['g_desc']; ?></small><br>
</p>
</div>
<?php
}
?>
If you have user object saved in $user then create memberOf() method which will return true if user is a member of the group or false otherwise.
Note: Do not search database on each memberOf() execution. Do it once and save results in object's property for reuse.

How to find attribute on a row table using jquery

if I have html that integrated on php like this :
foreach ($data_pc as $data) {
?>
<tr>
<td class="center"><?php echo $no++ . ". "; ?> </td>
<td class="center" data-no="<?php echo $data['id_pc'] ?>"><?php echo $data['nama_user']; ?></td>
<td class="center"><?php echo $data['perusahaan']; ?></td>
<td class="left">
<a class="btn btn-info" name="edit" title="Edit User" req_id="<?php echo $data['nama_user']; ?>" data-toggle="modal" data-target="#myModal1" role="button">
<i class="halflings-icon white edit"></i>
</a>
<td>
<a class="btn btn-danger" id="btn-hapus" title="Hapus User" req_nama="<?php echo $data['nama']; ?>">
<i class="halflings-icon white trash" ></i>
</a>
</td>
</td>
</tr>
<?php } ?>
that would be representated row on table html.
I want to take the attribute data-no on a row.
So, I write jquery code like this :
$(document).on('click', '#btn-hapus', function() {
var $row = $(this).closest("tr"); // Find row
var data = $row.find("data-no").text(); // Find text
alert(data);
});
It gives me a blank value ? How to find attribute on a row table using jquery ?
You need to use Has Attribute Selector [name] to target the element that have the required attribute:
var data = $row.find("[data-no]").text(); // get text of element that have attr data-no
if you want to get the attribute value of element then use .data() selector
var data = $row.find("[data-no]").data('no'); //get data attribute value

Categories