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.
Related
I'm trying to bind the id from a row to the href output. that is getting a url something?id=*** in order to use $_GET and bring the id on the next page.
I need to be the id on the same row that is clicked on a table I'm displaying.
If I try to bind it by stating href=" wahtever?id=<php echo $row['id'] ?> the id will return as empty. If I use a loop it works but give me all the id's on the table.
I tried different solutions I found on internet like stating echo '<td> <a href="****?id='.$row['id'].' </a></td>' or making a new selection using php code on the href link... nothing seems to work.
I'm confused, how can I make a link on a table that will include the id of the clicked row?
My code looks like this now:
<td bgcolor="#FAB1CA"><a href="view_topic.php?id=<?php $sql="SELECT * FROM forum_question ORDER BY id DESC";
$result = mysqli_query($link, $sql);
while($rows = mysqli_fetch_assoc($result){
echo $rows['ID'] ; ?>">
Just to make it clearer, it is a simple table displaying 4 columns with different data using a loop, the first column is the id and the second one would be the topic, where I trying to build the links.
It sounds like you have 5 columns in a database table and you want to show them on the page, and link the topic cell to the topic page and pass the id of that topic.
I cleaned your code up a little and gave an example of how to do that. Keep in mind, I'm using an associative array so you'll need to be sure it matches what the columns are called in your database.
<table>
<tr>
<th>ID</th>
<th>Topic</th>
<th>Answers</th>
<th>Views</th>
<th>Date</th>
</tr>
<?php
$sql="SELECT * FROM forum_question ORDER BY id DESC";
$result = mysqli_query($link, $sql);
while($row = myslqi_fetch_assoc($result)) : ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td bgcolor="#FAB1CA">
<a href="view_topic.php?id=<?php echo $row['id']; ?>">
<?php echo $row['topic']; ?>
</a>
</td>
<td><?php echo $row['answers']; ?></td>
<td><?php echo $row['views']; ?></td>
<td><?php echo $row['theDate']; ?></td>
</tr>
<?php endwhile; ?>
</table>
I cant see $row variable in your code
you can use myslqi_fetch_assoc for get $row variable
i think this answer its true
<?php
$sql="SELECT * FROM forum_question ORDER BY id DESC";
$result = mysqli_query($link, $sql);
while($row = mysqli_fetch_assoc($result)){
?>
<td bgcolor="#FAB1CA"><a href="view_topic.php?id=<?php echo $row['id']?>go to view_topic</a></td>
<?php
}
?>
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>
Dear please help me
i am getting some error in my code
this is my view.php page
<html>
<body>
<table style="border:#333 solid" border="2">
<tr>
<td>ID:</td>
<td>NAME:</td>
<td>EMAIL:</td>
<td>MOBILE:</td>
<td>EDIT:</td>
<td>DELETE:</td>
</tr>
<?php
include_once('connect.php');
$query="select * from emp";
$conn=mysqli_query($con,$query);
if(isset($conn))
{
echo"This is all data of table.";
}
else
{
die("query is not execute". mysqli_error());
}
while($row=mysqli_fetch_array($conn))
{
?>
<tr>
<td><?php echo $row['emp_id']; ?></td>
<td><?php echo $row['emp_name']; ?></td>
<td><?php echo $row['emp_address'] ?></td>
<td><?php echo $row['emp_salary'] ?></td>
<td><font color="#FF0000">EDIT</font></td>
<td><font color="#FF0000">DELETE</font></td>
</tr>
<?PHP
}
?>
</table>
</body>
</html>
and this is my delete.php file:
<?php
include_once('connect.php');
$id=$_GET['emp_id'];
$query="delete from emp where emp_id='$id'";
$del=mysqli_query($con,$query);
if($del)
{
echo"record has been deleted";
}
else
{
die("Record is not deleted it is query error.".mysqli_error());
}
?>
How can I access the ID of the view.php in the delete.php file and how to delete a single row in my db table.
I don't understand how to access the id of view.php.
In this program I don't delete the selected row. Please tell me how to delele data.
please anyone help me.
You are sending edit.php?id= so need to get id from query string using GET['id']
$id = $_GET['id'];
change the code in delete.php as follow
<?php
include_once('connect.php');
$id=$_GET['id'];
$query="delete from emp where emp_id='$id'";
$del=mysqli_query($con,$query);
if($del)
{
echo"record has been deleted";
}
else
{
die("Record is not deleted it is query error.".mysqli_error());
}
?>
Please also realize that using $_GET data within your queries is only asking for trouble, especially when it's not escaped properly. You're opening your self up to SQL injection attacks.
Make use of mysql_real_escape_string if you're going to be using mysql_* functions, just realize that it has been deprecated in PHP 5.5.
You also have the option of using MySQLi or PDO would be preferable since they both make use of prepared statements as mentioned in this previous question How can I prevent SQL-injection in PHP?
When a was creating a little code in php/mysql I wanted to know how to build a php form to delete an item from database.
The first code is index.php the goal of the script is to gather information about the visitor's of my website because users who want to deface my website.
<?php
$getting_ips = mysql_query("SELECT * FROM ip_capture_module");
if ($getting_ips) {
while ($row = mysql_fetch_array($getting_ips)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><strong><? echo $row['country']; ?></strong></td>
<td><?php echo $row['browser']; ?></td>
<td><?php echo $row['ipaddr']; ?></td>
<td><?php echo $row['method']; ?></td>
<td><strong><?php echo $row['sploit']; ?></strong></td>
<td><?php echo $row['date']; ?></td>
<td>delete</td>
</tr>
<?php
// The Second Code is capture.php
#------------------------------------------------------------------------------------------
...
....
mysql_query("INSERT INTO ip_capture_module(country,browser,ipaddr,method,date) VALUES(
'$country_name',
'$browser',
'$ip',
'$method',
CURRENT_TIMESTAMP())") or die ("<br>Error in inserting in IP: ".mysql_error()."<br>");
if(isset($_POST['delete'])){
//INSERT CODE HERE TO CONNECT TO DATABASE
//DO MORE ERROR CHECKING NECESSARY
mysql_query("DELETE FROM `table` WHERE row='value' LIMIT 1");
//OR YOU CAN USE MYSQLI
$mysqli->query("DELETE FROM `table` WHERE row='value' LIMIT 1");
mysql_close();
//MYSQLI AGAIN
mysqli_close();
exit();
}
I can't give you anymore since your question was so broad. Try using PHP.NET for more info.
DELETE FROM `table` WHERE `some_row`='row_value'
This is the best i can do, since you haven't given enought informations to help you more
I'm trying to figure out what's wrong with my mysqli code below. The data is definitely in the table. I have 2 questions:
Why isn't this giving me any results?
Could I have the echo <table></table> section in a separate php file, if yes, how would I call $name,$partner,$cell etc.?
<?php
$mysqli = new mysqli($host, $uname, $pword, $db);
if ($mysqli->connect_errno)
{
echo "Connection failed: %s\n", $mysqli->connect_error;
exit();
}
$query = ("SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor') AND leadstatus = 'New' ORDER BY date DESC");
$result = $mysqli->query($query);
while ($row = $result->fetch_array()) {
$space = (!empty($row['firstname']) && !empty($row['lastname'])) ? ' ' : '';
$name = $row['firstname'].$space.$row['lastname'];
$partner = $row['spousefirst'];
$cell = (!empty($row['phonecell'])) ? " {$row['phonecell']} (cell)" : '';
$email = (!empty($row['email'])) ? " {$row['email']} (email)" : '';
$ID = $row['ID'];
echo'<table>
<tbody>
<tr>
<td><input type="checkbox" name="" id="" value="<? echo $ID; ?>"></td>
<td><strong><? echo $name; ?></strong></td>
<td><? echo $partner; ?></td>
<td><? echo $phonecell; ?></td>
<td><? echo $email; ?></td>
<td><? echo date("M jS, g:i A", strtotime($date)); ?></td>
<td><? echo $contacttype; ?></td>
<td><? echo $agentassigned; ?></td>
<td><? echo $leadstatus; ?></td>
<td>View + </td>
<td>View + </td>
<td>D</td>
</tr>
</tbody>
</table>';
}
?>
EDIT:
It's showing the <table> but just not giving me values for $name, $email...ect.
The final echo does not look well constructed. I would prefer to use a concatenated string to output the variables, just like this( I put only a few lines as example):
echo'<table>
<tbody>
<tr>
<td><input type="checkbox" name="" id="" value="'. $ID .'"></td>
<td><strong>'. $name.' </strong></td>
The query looks solid as does the rest of your code. Here are a few troubleshooting steps to try out:
echo out $query and run it directly against your database. Do you get results? If you don't get any results from a direct query then that means the query is returning 0 rows and your tables will end up empty as well.
Do a var_dump($result) to verify there is information being stored in there. You could also add change $result = $mysqli->query($query); to $result = $mysqli->query($query) or die $mysqli->error;
The results from these 2 steps should give you a good idea on where things are going wrong. In general you want to stop through each part of the query process and look at every variable involved to ensure that it has the data you expect stored inside of it.
To answer your 2nd question: yes you can. If you put the code for the table in a separate file and then include()it, the variables will still be available to the included code.
See the PHP manual on include()