I have written some code to echo my table:
<?php while($product = mysqli_fetch_assoc($result)) : ?>
<tr>
<td><?php echo $product['id']; ?></td>
<td><?php echo $product['customer_id']; ?></td>
<td><?php echo $product['total_price']; ?></td>
<td><?php echo $product['created']; ?></td>
<td><?php echo $product['modified']; ?></td>
<td><span class="glyphicon glyphicon-remove"></span></td>
</tr>
<?php endwhile?>
Last td element is created for specific table row removal. However I don't know how to get that row id. This is my delete.php file:
<?php
require_once 'core/init.php';
$id = $_GET['id'];
mysqli_query($db,"DELETE FROM orders WHERE id='".$id."'");
mysqli_close($db);
header("Location: details-modal-orders.php");
?>
I assume i should change something in this line:
<td><span class="glyphicon glyphicon-remove"></span></td>
After delete.php there should be somekind of id recognizer or something. Please help. I also don't know how to create that because it is inside of a while loop. I'm afraid something is not going to work well.
Pass the id as a parameter via the href attribute
href="delete.php?id=<?php echo $product['id']; ?>">
Also do not forget to use prepared statements while performing this operation as it is totally unsafe to pass $_GET or $_POST or $_REQUEST parameters directly into your query
Using prepared statments
$id = (int) $_GET['id']; //cast to integer
$stmt = mysqli_stmt_prepare($db,"DELETE FROM orders WHERE id=?"); //prepare the statement returns true/fasle
mysqli_stmt_bind_param($stmt, "i", $id); //bind placeholder to variable
mysqli_stmt_execute($stmt); //execute (returns true/false)
Is it serious ?
<td><a href="delete.php?id=<?php echo $product['id']; ?>">
<span class="glyphicon glyphicon-remove"></span>
</a></td>
Related
For now I have this code which passes variables based on an id in the URL:
foreach ($allUnsteadyState as $list) {
?>
<tr>
<td><?php echo $list['project_name'] ?></td>
<td><?php echo $list['project_date'] ?></td>
<td>Go to project</td>
</tr>
<?php
}
?>
And catch it on another page through an function with this code:
$id = (int)$_GET['id'];
$db = $this->dbconn;
try {
$stmt = $db->prepare("SELECT * FROM unsteady_state WHERE id = :id ");
$stmt->bindParam(':id', $id);
$stmt->execute();
return $stmt->fetchAll();
And so on.
For security reasons I don't want to show the id in the url, so I tried to use $_POST and $_SESSION without succes. Can someone help me with this please?
You can do this with $_POST. You'll need a form. You can wrap one around your table.
<form action="unsteady_state_user.php" method="post">
<table>...</table>
</form>
Then use buttons instead of links.
<?php foreach ($allUnsteadyState as $list): ?>
<tr>
<td><?php echo $list['project_name'] ?></td>
<td><?php echo $list['project_date'] ?></td>
<td>
<button type="submit" name="id" value="<?php echo $list['id'] ?>">
Go to project
</button>
</td>
</tr>
<?php endforeach; ?>
The ID won't be in the URL. You can get it from $_POST['id']
However, it will still be in the page source so depending on what your security concerns are this may not work for you.
I have a table that is displaying data to the user and each row in the table has a unique id, in the table I am generating a button for each row to get more information on the content in the row. However, i need to generate a unique href for each button based on the Unique ID of the data in the row. The way I have done it it correctly goes to the link in the href but without passing the Unique ID.
<tbody>
<?php while($row = $query->fetch()) { ?>
<tr>
<td><?php echo $row['car_id']; ?></td>
<td><?php echo $row['car_make']; ?></td>
<td><?php echo $row['car_model']; ?></td>
<td><?php echo $row['number_available']; ?></td>
<td><?php echo $row['rent_cost_per_day']; ?></td>
<td><a class="btn btn-default" href="cars.php?"<?php $row['car_id']; ?>>View</a></td>
</tr>
<?php } ?>
</tbody>
Any help?
This is because you put the php after you closed the href quotes. You also have not put an echo function for the php, and have not put a get variable. You have:
href="cars.php?"<?php $row['car_id']; ?>
What you want to have is:
href="cars.php?id=<? echo $row['car_id']; ?>"
This is actually very easy but yet I am confused how to do this:
I have Reseller that can request X-number of users to admin. So that value gets saved to database. Now when admin logs in he can see the requests of resellers. Like this :
Now Admin Can either approve or reject the request. If He approves the request the code creates users in users table mapped to that reseller. The mapping is done with a unique field called Key Now Creating users works perfectly but my problem is :
When I click on Approve it creates users but for only for the reseller wit id = 1. I want to create something like when I click on first record it creates users for 1st reseller and when on 2nd same as above[This is as per the image].
This is my view:
<?php if(count($users)): foreach($users as $user): ?>
<tr class="odd gradeX">
<td><?php echo $user->id; ?></td>
<td><?php echo $user->user_requested; ?></td>
<td><?php echo $user->key; ?></td>
<td><?php echo $user->date_requested; ?></td>
<td>Yes <i class="glyphicon glyphicon-ok"></i></td>
<td>No <?php echo'<i class="glyphicon glyphicon-trash"></i>';?></td>
<td><?php echo $user->status; ?></td> </td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">There are no new Requests.</td>
</tr>
<?php endif; ?>
The Controller
public function create_user()
{
$this->load->model('more_m');
$id= $this->session->userdata('id');
$rajan =$this->more_m->get(array('user_requested',$id));
$request=$rajan->user_requested;
$this->load->model('reseller_m');
$query=$this->db->select('key');
$query=$this->db->get('reseller');
if($query->num_rows() > 0)
{
$row = $query->row_array();
$key= $row['key'];
}
for($i=1; $i<=$request;$i++)
{
$userdata=array('key'=>$key);
$this->db->insert('users',$userdata);
}
$this->load->model('more_m');
$id = $this->session->userdata('id');
$this->db->set('status', "'approved'",FALSE);
$this->db->where('id',$id);
$this->db->update('user_request');
redirect('admin/new_user');
}
My mistake is in controller where I define $id. As when admin logs in his id would be fetched I don't know how to fetch the id of the reseller as per the record.
Its so simple with Codeigniter
What you have to do is pass the ID with the URL (You are wrong Use base_url()as well with the pointing controller)
ex <td><a href="<?php echo base_url()?>reseller/create_user/<?php echo $user->key; ?>"
so now your URL looks like (assume id is equal to 2)
http://localhost/<path to project>/reseller/create_user/2
so in Controller
function create_user($id)//So this know incoming Value to this
{
//then in here
echo $id; //this will show 2
//Continue rest of your code with $id
}
If you wanna do it in php way,You can pass key in your url and get key in your controller using $this->uri->segment(n)
<?php if(count($users)): foreach($users as $user): ?>
<tr class="odd gradeX">
<td><?php echo $user->id; ?></td>
<td><?php echo $user->user_requested; ?></td>
<td><?php echo $user->key; ?></td>
<td><?php echo $user->date_requested; ?></td>
<td>Yes <i class="glyphicon glyphicon-ok"></i></td>
<td>No <?php echo'<i class="glyphicon glyphicon-trash"></i>';?></td>
<td><?php echo $user->status; ?></td> </td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">There are no new Requests.</td>
</tr>
<?php endif; ?>
If you want jquery+php than use ajax.
add value attr. to button pass your key.
<button id="some_id" value="YOURKEY">Click Me</button
This is your script to update :
<script>
$('body').on('click',"#some_id", function(){
var key =$(this).attr("value");
//Now you got ur key you can use ajax to update and change location of page on success
});
</script>
i have a table with id, name, address, sector, financiar, link
on the link i when i press it i want to show me 2 tables from the id of row selected, ex: id 1.
http://postimg.org/image/khelg1m0z/
and the result: http://s28.postimg.org/srvcwj065/Capture2.jpg
now it's a static page with search clause where by id 1, but i need an automatically link show by id on each row.
<?php
include "connect.php";
$sql = "select * from studenti where id='1'";
$query = mysql_query($sql) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Nume</td>
<td>Localitate</td>
<td>Judet</td>
<td>Sector Financiar</td>
<td>Link</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
<?php } ?>
</table>
<?php
include "connect.php";
$sql1 = "select * from certificari where id='1' ";
$query = mysql_query($sql1) or die (mysql_error());
?>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Id</td>
<td>Denumire certificare</td>
<td>Serie si numar certificare</td>
<td>Data certificarii</td>
<td>Valabilitate certificare</td>
<td>Sector Financiar</td></tr>
<?php while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['serie_numar']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['valabilitate']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<?php } ?>
</table>
You should not use the mysql_ functions since they are depricated and are very vulnerable to SQL injection attacks. Instead, I will use MySQLi in this answer, but you could also use PDO if you want to.
To display records for different ID's based on what the user selects you can pass an ID in the page URL. When you link to the page you add the desired ID to the address like this:
Link
The value of the variable id will now be available in page.php as $_GET['id'].
The actual PHP in your page would then look something like the code below. I have left out some of your HTML for brevity, but you can just add it int.
//Connect to the DB. Might want to put this in your connect.php and include it.
$db = new mysqli('localhost', 'user', 'pass', 'db');
//Prepare the statement. The ? will be your ID.
$statment = $db->prepare("SELECT * FROM studenti WHERE id = ?");
//Bind the parameters, so that the first (and only) question mark is turned into your ID.
//The 'i' means integer, if you store the id as a string your should use 's' instead.
$statement->bind_param('i', $_GET['id']);
//Execute the query.
$statement->execute();
//Get the results.
$result = $statement->get_result();
//Iterate over it to create the output.
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['nume']; ?></td>
<td><?php echo $row['localitate']; ?></td>
<td><?php echo $row['judet']; ?></td>
<td><?php echo $row['sector_financiar']; ?></td>
<td><?php echo $row['link']; ?></td>
</tr>
<?
}
//Then do the same thing for the table certificari.
//Note that you only need to connect to the DB once.
This beginners guide to MySQLi is very helpful if you need some more guidance.
I'm trying to delete a row in a table in my database, but it isn't working... The connection works. I've echoed the table row id to ensure it is working and still it won't delete...
Let me know if you need the connection code too.
code:
<table>
<tr>
<td>Recent Posts</td>
</tr>
<?php while($row = mysql_fetch_array($result)) : ?>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['phone number']; ?></td>
<td>Schedule</td>
</tr>
<?php endwhile; ?>
</table>
delete.php code:
<?php
$b=$_GET['id'];
$sql='DELETE FROM on call WHERE id=$b';
//echoing just to make sure it is working correctly...
echo $b;
?>
</br>
</br>
<a href='index2.php'>back to list</a>
You didn't execute your DELETE sql command using mysql_query
$sql = "DELETE FROM ...";
mysql_query($sql);
Please note that the mysql functions are now deprecated. Also, you should protect your code against SQL injections.
I don't understand your sql syntax. if 'on call' is your table, please rename your table to on_call and so your sql should :
$sql = "DELETE FROM on_call WHERE id = '" . $b . "'";
You did not execute your sql.
mysql_query($sql); use mysql function to prevent injection.