I have a couple of problems. I'm creating a form inside a table, from what I understand this is not a good idea. But because of the loop I want to make sure the table header is outside so it doesn't repeat. Is there a smarter way to do this?
Also more importantly I can't seem to get the delete button to remove the correct video. It seems to delete the last one in the list. Something wrong with how I'm looping over this?
<p>
<h3>Recorded Videos</h3>
<table id="webcam-table">
<thead>
<tr>
<td>Camera Name</td>
<td>Video Size</td>
<td>Date Created</td>
<td>Video Length</td>
<td>Video Options</td>
</tr>
</thead>
for($i=0;$i<$num_videos;$i++)
{
<form action="<?php htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<input type="hidden" name="video_id" value="<?php echo $result_videos[$i]["video_id"]; ?>" />
<tbody>
<tr>
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_size"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_datetime"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_length"]; ?>
</td>
<td>
<input type="submit" name="delete_video" value="Delete" onClick="javascript:return confirm('Delete this video?');"/>
</td>
</tr>
</tbody>
}
echo "</table>";
echo "</form>";
echo "</p>";
}
}
if (isset($_POST['delete_video'])) {
$video_id = $_POST['video_id'];
$query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
$db->setQuery($query_delete_video);
$db->query();
header("location: " . $_SERVER['REQUEST_URI']);
In your loop you are creating the 'form' tag. However you are not closing it each time. This is causing your deleting problem.
Move
echo "</form>";
Inside the loop.
looks good to me, the only issue I see is that the tag should be outside the loop (opening before, closing after).
Revised code
<?
if (isset($_POST['delete_video']))
{
$video_id = $_POST['video_id'];
$query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
$db->setQuery($query_delete_video);
$db->query();
header("location: " . $_SERVER['REQUEST_URI']); //you should not allow things to be echoed before a header()
}
?>
<script type="text/javascript">
function onMySubmit(video_id)
{
document.myform.video_id.value = video_id;
return confirm('Delete this video?');
}
</script>
<p>
<h3>Recorded Videos</h3>
<!-- you had <?php htmlentities(...) ?>, you should have had
<?php echo htmlentities(...) ?> or <?= ... ?> -->
<form name="myform" action="<?= htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<input type="hidden" name="video_id" value="" />
<table id="webcam-table">
<thead>
<tr>
<td>Camera Name</td>
<td>Video Size</td>
<td>Date Created</td>
<td>Video Length</td>
<td>Video Options</td>
</tr>
</thead>
<tbody>
<? for($i=0;$i < $num_videos;$i++) { ?>
<tr>
<td><?= $result_videos[$i]["camera_name"]; ?></td>
<td><?= $result_videos[$i]["video_size"]; ?></td>
<td><?= $result_videos[$i]["video_datetime"]; ?></td>
<td><?= $result_videos[$i]["video_length"]; ?></td>
<td><input type="submit" name="delete_video" value="Delete" onClick="javascript:return onMySubmit(<?=$result_videos[$i]["video_id"];?>);"/></td>
</tr>
<? } ?>
</tbody>
</table>
</form>
</p>
Related
Basically I need to have the columns with my delete and edit buttons visible only if the admin logs in, I know how to determine if an admin is logged in. Is there a way to echo the tags that need to be shown? Thanks in advance!
<table>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Email Address</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Status</th>
<th>Edit?</th>
<th>Delete?</th>
</tr>
<?php foreach ($users as $user) : ?>
<tr>
<td><?php echo $user->getUser_id();?></td>
<td><?php echo $user->getFirstName().' '.$user->getLastName();?></td>
<td><?php echo $user->getEmailAddress(); ?></td>
<td><?php echo $user->getAddress();?></td>
<td><?php echo $user->getCity(); ?></td>
<td><?php echo $user->getState(); ?></td>
<td><?php echo $user->getZipCode(); ?></td>
<td><?php echo $user->getActiveDescription();?></td>
these two <td> should only be shown if x is true:
<td>
<form action ="" method="post">
<input type="hidden" name="controllerRequest"
value="show_user_edit">
<input type="hidden" name="user_id"
value="<?php echo $user->getUser_id(); ?>"/>
<input type="submit" value="Edit"></form>
</td>
<td>
<form action="" method="post">
<input type="hidden" name="controllerRequest"
value="delete_user">
<input type="hidden" name="user_id"
value="<?php echo $user->getUser_id(); ?>"/>
<input type="submit" value="Delete" id="red">
</form>
</td>"
;
}
}
?>
</tr>
<?php endforeach; ?>
</table>
You need to use <?php if(x): ?> and <?php endif ?>see: https://www.php.net/manual/en/control-structures.if.php#112231
I am making the modification for the query and already add and everything but in modifying it does not work with 'id' that I put after putting the address of the modification page
this is the table
<!DOCTYPE html>
<html>
<head>
<title> TABLA </title>
</head>
<body>
<center>
<table bgcolor="#85C1E9" border="3">
<thead>
<tr>
<th colspan="1">Nuevo</th>
<th colspan="5">Lista de usuarios</th>
</tr>
</thead>
<tbody>
<tr>
<td>Id</td>
<td>Nombre</td>
<td>Apellido</td>
<td>Correo</td>
<td colspan="2"><center>Operaciones</center></td>
</tr>
<?php
include("conexiones.php");
$query="SELECT * FROM usuarios";
$resultado=$conexion->query($query);
while ($row=$resultado->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['nombre'];?></td>
<td><?php echo $row['apellido'];?></td>
<td><?php echo $row['correo'];?></td>
<td><a href="modificar.php?id=<?php echo $row['id'];
?>">Modificar</a></td>
<td><a href="eliminar.php?id=<?php echo $row['id']; ?
>">Eliminar</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</center>
</body>
</html>
here the modify page
<!DOCTYPE html>
<html>
<head>
<title>Guardar</title>
</head>
<body>
<center>
<form action="guardar.php" method="POST" name="guardar"><br><br><br>
<?php
$id=$_REQUEST['id'];
include("conexiones.php");
$query="SELECT * FROM usuarios";
$resultado=$conexion->query($query);
$row=$resultado->fetch_assoc();
?>
<input type="text" required="" name="nombre" placeholder="Nombre..."
value="<?php echo $row['nombre'];?>"><br><br>
<input type="text" required="" name="apellido"
placeholder="Apellido..." value="<?php echo $row['apellido'];?>">
<br><br>
<input type="text" required="" name="correo"
placeholder="Correo..." value="<?php echo $row['correo'];?>"><br>
<br>
<input type="submit" value="Aceptar">
</form>
</center>
</body>
</html>
the error is that when I press modify in the query I get like this:
http://localhost/modificar.php?id=%3C?echo%20$row[%27id%27];?%3E
and the way it should go (for example) is like this:
http://localhost/modificar.php?id=1
Try this:
<?php
include("conexiones.php");
$query="SELECT * FROM usuarios";
$resultado=$conexion->query($query);
?>
<?php while ($row=$resultado->fetch_assoc()): ?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['nombre'] ?></td>
<td><?= $row['apellido'] ?></td>
<td><?= $row['correo'] ?></td>
<td><a href="modificar.php?id=<?= $row['id'] ?>">
Modificar</a></td>
<td><a href="eliminar.php?id=<?= $row['id'] ?>">
Eliminar</a></td>
</tr>
<?php endwhile; ?>
hope it helps.
I have the problem with update function in CodeIgniter. The cart is not updating and don't understand why. Can you help me to find a solution for this issue?
This is my Update function
public function update($in_cart = null) {
$data = $_POST;
$this->cart->update($data);
//show cart page
redirect('cart','refresh');
}
This is my form inside sidebar.php
<form action="cart/update" method="POST">
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items) : ?>
<input type="hidden" name="<?php echo $i.'[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" />
<tr>
<td><input type="text" style="color:black; text-align:center;margin-bottom:5px;" name="<?php $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size="3"></td>
<td><?php echo $items['name']; ?></td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td></td>
<td class="right"><strong>Total</strong></td>
<td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<br>
<p><button class="btn btn-default" type="submit">Update Cart</button>
<a class="btn btn-default" href="cart">Go to Cart</a></p>
</form>
Try using $this->input->post() to get all form posted data.
https://ellislab.com/codeigniter/user-guide/libraries/input.html
So I'm creating a database for my website and I want to create an admin section that allows you to add or delete from the table. Here is a snapshot of what I want to achieve...
In my php file I have if($_POST['delete_category']) which correctly gets the delete button clicks, but then I'm not sure how to distinguish which delete button was actually clicked. I'm sure this is a very simple solution, but I'm stuck. Thanks!
You can discern which button is submitted by this following markup (based on your example fetched results from DB):
<?php
if(isset($_POST['delete_category'])) {
$id = $_POST['delete_category']; // the value="1" or value="3" goes in here
echo $id;
}
?>
<form method="POST" action="">
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Delete</th>
</tr>
<tr>
<td>1</td>
<td>Senior Pictures</td>
<td><button typpe="submit" name="delete_category" value="1">Delete</button></td>
<!-- each delete button has the same name, but different values -->
</tr>
<tr>
<td>3</td>
<td>Engagements</td>
<td><button typpe="submit" name="delete_category" value="3">Delete</button></td>
</tr>
</table>
</form>
If I had to guess this makes sense on the fetching: (Note: Just a sample!)
<form method="POST" action="">
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Delete</th>
</tr>
<?php while($row = $results->fetch_assoc()): ?> <!-- assuming mysqli() -->
<?php while($row = mysql_fetch_assoc($result)): ?> <!-- assuming on mysql (bleh) -->
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>
<button typpe="submit" name="delete_category" value="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
<form name="myForm" action="doControlAcc.php" method="post" onsubmit="return validateForm(this);">
<table cellpadding="0" cellspacing="0" border="20" class="display" id="acc" width="200">
<thead>
<tr><h2>Accessories summary</h2></tr>
<tr>
<th>Type</th>
<th>Currently - Ex Factory</th>
<th>Minimum Required Stock</th>
<th>Update by</th>
<th>Last update</th>
<th>Re-Stock Amount</th>
<th>Stock Out</th>
</tr>
</thead>
<tbody>
<?php
$accessories = array()
$query = "SELECT description, current_stock, min_required_stock, c_ex_factory, last_update from Accessories”;
$result= mysqli_query($link, $query) or die(mysqli_error($link));
$row = mysqli_fetch_array($result);
while($row = mysqli_fetch_array($result)){
$order = $row['c_ex_factory'] - $row['min_required_stock'];
if ($order < 0){
$order = $row['min_required_stock']-$row['c_ex_factory'];
$color = "#FF0000";
}else{
$order = 0;
$color = "#00FF00";
}
?>
<tr>
<td name="accessories[]" id="accessories" value=”<?php echo $row['description'] ?>” <?php echo in_array($row['description', $ accessories) > <?php echo $row['description']; ?> </td>
<td align="center" name="accessories[]" id="accessories" value=”<?php echo $row[''c_ex_factory '] ?>” <?php echo in_array($row[''c_ex_factory ', $ accessories) > <?php echo $row['c_ex_factory']; ?> </td>
<td align="center" name="accessories[]" id="accessories" value=”<?php echo $row[''min_required_stock '] ?>” <?php echo in_array($row[''min_required_stock ', $ accessories) > <?php echo $row['min_required_stock']; ?> </td>
<td align="center" name="accessories[]" id="accessories" value=”<?php echo $row[' name '] ?>” <?php echo in_array($row[' name ', $ accessories) > <?php echo $row['name']; ?> </td>
<td align="center" name="accessories[]"id="accessories" value=”<?php echo $row['description'] ?>” <?php echo in_array($row['description', $ accessories) > <?php echo $row['up_when']; ?> </td>
<td align="center" name="accessories[]" bgcolor="<?php echo $color; ?>" id="accessories" <?php echo in_array($row[' re_stock ', $ accessories) > <input type="text" size="5" name="re_stock" value=" <?php echo $order ?>"/> </td>
<td align="center" name="accessories[]"id="accessories" <?php echo in_array($row[' out_stock ', $ accessories) > <input type="text" size="5" name="out_stock" value ="0"/></td>
</tr>
<?php
}
?>
<tr>
<td> </td>
<td> </td>
<td></td> <td></td> <td></td>
<td><button data-theme="b" id="submit" type="submit">Send order</button></td>
</tr>
</tbody>
</table>
</form>
Hello,
I'm new to web programming.
The code above will fetch the data from database to create a table in PHP, then it will take the user inputs for stock-out and restock and send those to doControlAcc.php file.
The problem I'm facing is I need to create multiple input lines so that users can enter stock-out and re-stock for different products. And I don't know how to pass these multiple lines to doControlAcc.php file after the form is filled. (i.e how can I access accessories array in doControlAcc.php?).
THank you for your help.
This may not be the best way, but you can always use session variables. I can't quite understand the program you are writing, so I can't say whether or not sessions would be safe enough, but if you wanted to use sessions, do something like this:
At the beginning of your file:
<?php
session_start();
?>
And later, when the variable is all set:
$_SESSION['accessories'] = $accessories;
To access it in another code file, you must start the session as shown above in each code file. Then, just do this;
$accessories = $_SESSION['accessories'];