Having an issue with deleting entry from database on my website - php

I have echoed my database table onto my website and have tick option next to the table where you can tick it and delete any of the entries from the website, it should delete it from the database. For some reason its not working, I'm a beginner and any help would be greatly appreciated thanks.
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id = $checkbox[$i];
print_r($_GET['delete_these']);
$ids = implode(', ', $_POST['delete_these']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
}
}
mysql_close();
?>
</form>

you have add method in your form tag--
<form method="post" action="tester.php" >
and also replace if(isset($_GET['delete'])) to if(isset($_POST['delete']))

Replace your form tag by:
<form method="post" action="tester.php">
then these 3:
if(isset($_POST['delete']))
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE id=$id";
at the place of these 3:
if(isset($_GET['delete']))
$id = $checkbox[$i];
$sql = "DELETE FROM booking WHERE id IN($ids)";
Your $id = $checkbox[$i]; works only if register_globals directive is on, which is now deprecated (and removed as of PHP 5.4) because of security issues it causes, and if it exists in the version of PHP you are using most hosting services turn it off.
You'll be sending the data through post so you access them through $_POST. the (int) before the $_POST is to cast anything to integer so if the user try to send a string it will become 0, this will protect you from sql injection.
SIDE NOTE you should stop using mysql_* functions as they are deprecated. Check out PDO or mysqli

Related

PHP connection to a database (no output!)

I wrote a code to connect a html page to the database, but it does not search for all employees in the table. I'm not sure why the code seems to be correct. It does not give any error or warning.
Can anyone explain to me what is wrong?
<div>
<form id='searchform' action='index.php' method='get'>
<a href='index.php'>All employees</a> ---
Search by a last name:
<input id='search' name='search' type='text' size='20' value='<?php echo #$_GET['search']; ?>' />
<input id='submit' type='submit' value='Go!' />
</form>
</div>
<?php
// check if search view of list view
if (isset($_GET['search'])) {
$sql = "SELECT * FROM Employee WHERE LastName like '%" . #$_GET['search'] . "%'";
} else {
$sql = "SELECT * FROM Employee";
}
// execute sql statement
$stmt = oci_parse($conn, $sql);
oci_execute($stmt);
?>
<table style='border: 1px solid #DDDDDD'>
<thead>
<tr>
<th>Personal Number</th>
<th>Insurance Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Birth Date</th>
<th>Gender</th>
<th>Hire Date</th>
<th>Salary</th>
<th>Office ID</th>
</tr>
</thead>
<tbody>
<?php
// fetch rows of the executed sql query
while ($row = oci_fetch_assoc($stmt)) {
echo "<tr>";
echo "<td>" . $row['PersonalNumber'] . "</td>";
echo "<td>" . $row['InsuranceNumber'] . "</td>";
echo "<td>" . $row['FirstName'] . " " . $row['LastName'] . "</td>";
echo "<td>born on " . $row['BirthDate'] . "</td>";
echo "<td>" . $row['Gender'] . "</td>";
echo "<td>hired on " . $row['HireDate'] . "</td>";
echo "<td>" . $row['Salary'] . "</td>";
echo "<td>" . $row['OfficeID'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<div>There are found <?php echo oci_num_rows($stmt); ?> employees!</div>
<?php oci_free_statement($stmt); ?>
</body>

How to change mysql column from html table with php scripting

Hi coders <3 I am a beginner in php, mysql thing and now i am stuck in one place. I have a table with approve and reject button in it, i want whenever I will will click on approve button, it will change the status of it as approved in the mysql table. How can i do this....through button click
here is my mysql table
here is the UI
and the coding is like this
<?php
// Connect to the database
$dbLink = new mysqli('127.0.0.1', 'root', '', 'hct_db');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT `quote_id`, `name`, `mime`, `size`, `created`, `status` FROM `quote`';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
// Make sure there are some files in there
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {?>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Quote ID</th>
<th>File Name</th>
<th>File</th>
<th>Size</th>
<th>Created</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead><tbody>
<?php
while($row = $result->fetch_assoc()) {
if ($row["status"]=="Not Approved")
{
echo "<tr>";
echo "<td>" . $row['quote_id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['mime'] . "</td>";
echo "<td>" . $row['size'] . "</td>";
echo "<td>" . $row['created'] . "</td>";
echo '<td>Download</td>';
echo "<td><input type='submit' name='submit' value='Approved'></td>";
echo "<td><input type='submit' name='submit' value='Reject'></td>";
echo "</tr>";
}}
?>
</tbody>
</table>
</div>
</div>
<!-- /.table-responsive -->
<?php
$result->free();
}
// Close the mysql connection
$dbLink->close();}
?>
1) You need to use ajax.
2) For every button you can use a form such as:
<form method="post" action="approved.php" target="_self">
<td>
<input type='submit' name='submit' value='Approved'>
<input type='hidden' name='quoteID' value='<?php echo $row['quote_id']?>'>
</td>
</form>
approved.php:
mysqli_connect
$approvedElement = $_POST['quoteID'];
$query = 'UPDATE ... WHERE `Quote ID` = \''.$quoteID.'\' ';
mysqli_query($query);
So before ajax I suggest you to learn basics about GET and POST methods.
In this example you need:
• form, to redirect the user to another page (approved.php, rejected.php)
• $_POST, in the second page to retrieve the ID of the approved element, and use it in the next step
• mysql_query, after you have correctly coded the query and successfully connected to the DB

Deleting rows from table on web retrieved from database

I am having issue with deleting rows from a database that I echoed onto my website, I have used tick check boxes and when multiples are selected they should be deleted. But it's just NOT HAPPENING! Nothing is getting deleted from the database! please help!
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
if($result){
}
}
mysql_close();
?>
</form>
First off you can just implode() all the gathered ids from the form and from there build the query.
Sample code:
<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>
and while you still can, use mysqli or PDO, its free anyway

Mysql+php, how to make a link that shows all data from a record when you press the "headline"

As the title says, and this is not probably a question, more a hint how to do it.
I have table with four fields, id (auto+primary), firstname, lastname, age.
On my index page I select *, then I make the firstname to a link which goes to antoher page which I want to show all data for that record.
For the moment I have to do it manually "select * from " where id="2". (the other page)
How do I make the link to autodetect "the id from that record set" and only display data from that record.
You can see my curreny project here,
http://www.adamskymotorkylare.se/business/
as you can see, when you click the "firstname" it will always display data where id=2 ( paris hilton ), what I want it to do, it when I click "jack" it will select * from where id="1"
Thanks in advance, Jack
"index page"
$result = mysqli_query($con,"SELECT * FROM persons");
echo "<table width=100%>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td> <a href='view_more.php?id= . $id .'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
"viewmore page"
$id = $_get['id'];
$result = mysqli_query($con,"SELECT * FROM persons
WHERE id='$id'");
echo "<table width=100%>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <a href='#'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
Try this, on your page that indexes the users:
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$firstName = $row['firstName'];
echo ('' . $firstName . '');
}
This means if someone clicks the first name they will be sent to user_account.php (can replace this obviously) and you will pass in the ID via the URL (user_account.php?id=123).
On the User Account page you want to do the following:
// GET ID FROM THE URL
$id = $_GET['id'];
$result = mysqli_query($con,"SELECT (WHATEVER YOU WANT) FROM (YOUR TABLE) WHERE id = $id");
Notes:
Replace variables and query with the details you need.
I hope that goes some way to helping.
UserFirstName
$id is user record id first column of your table so when you send it to view_more.php page you can get clicked id $_GET['id']; and get user records for that id and link is user name
and $id you get form data base the way you get first name
index.php
<?php
$sql =mysql_query("select * from `table`");
while($row = mysql_fetch_array($sql)){
?>
<?php $id = $row['id']; ?>
<?php echo ucwords($row['Firstname']); ?>
<?php
}
?>
on index.php anchor tag will send the user id to detail.php
detail.php
<?php
$id = $_REQUEST['cid'];
$sql =mysql_query("select * from `table` where id='".$id."'");
while($row = mysql_fetch_array($sql)){
?>
<tr>
<th>First Name:</th>
<th>Last Name:</th>
<th>Age Name:</th>
<th>Gender:</th>
</tr>
<tr>
<td><?=$row['firstname']?></td>
<td><?=$row['lastname']?></td>
<td><?=$row['age']?></td>
<td><?=$row['gender']?></td>
</tr>
<?php
}
?>
on detail.php $id get the user id from index.php. After that id helps in query and while loop which is get and show the user data from database.

delete button on each row that echo

every time user insert information and click add button, new data will store to database and echo into this table
table
<tr>
<td colspan="4" align="right">
<input type="image" value="image" src="images/btn_add.gif" onclick="action_1()">
</td>
</tr>
<tr>
<td colspan="2" class="title_all_u">Family Member Summary</td>
</tr>
<tr>
<td>
<?php
$query = "SELECT * FROM family_child WHERE LAS_login_id = ($emp_id)";
$result = mysql_query($query) or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Family Type</th>
<th>Name</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['family_child_type'] . "</td>";
echo "<td>" . $row['family_child_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</td>
</tr>
and this is insert query
if ( $action ==3 ) {
$spouse_type = $_POST['spouse_type'];
$spouse_name = $_POST['spouse_name'];
$sql1 = "INSERT INTO family_spouse (LAS_login_id, spouse_type, spouse_name) VALUES ('$LAS_login_id', '".strtoupper($spouse_type)."','".strtoupper($spouse_name)."')";
this 2 code is working for insert into database and echo in the page.
How can I add delete button below echo "<td>" . $row['family_child_name'] . "</td>"; for each row that I echo so user can delete the wrong row in the display table.
echo "<td>" . $row['family_child_name'] . " <a href='page.php&action=delete&id=family_name_id'>Delete</a></td>
Then in your page get (if action == delete) > execute your query to delete the row where the ID is the id of the family child name table.
$query = "SELECT * FROM family_child WHERE LAS_login_id = ($emp_id)";
$result = mysql_query($query) or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Family Type</th>
<th>Name</th>
<th>Action</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>
<td>" . $row['family_child_type'] . "</td>
<td>" . $row['family_child_name'] . "</td>
<td>
<a href='../path/process.php?action=delete&id=".$row['family_child_id']."'>
DELETE RECORD
</a>
</td>
</tr>";
}
echo "</table>";
?>
on process page check action = delete and write query for delete there
family_child_id = ur primary key of table change according to ur table details
EDIT
PROCESS PAGE:
if($_GET["action"] == "delete")
{
$sql3="DELETE FROM family_child WHERE LAS_login_id =".$_GET["LAS_login_id"];
$result3 = mysql_query($sql3);
if (mysql_affected_rows() > 0)
{
header("location:dashboard.php?tab=1");
}
}
this coding working perfectly. i tried in my pc and posted this code here. it will insert the delete button automatically when u insert the new record. and then if u click delete button it will delete the row details in mysql db..
<body>
<?
echo "<tr>
<td>";
// your database connection
// select database
$query = ("SELECT * FROM family_child");
$result = mysql_query($query) or die(mysql_error());
echo "<table border=1>
<tr>
<th>Family Type</th>
<th>Name</th>
<th>Delete Record</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['family_child_type'] . "</td>";
echo "<td>" . $row['family_child_name'] . "</td>";
echo "<td><form method=post>
<input name=id type=hidden value='".$row['family_child_name']."';>
<input type=submit name=submit value=Delete>
</form></td>";
echo "</tr>";
}
echo "</table>";
echo "</td>
</tr>";
// delete record
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_POST['id']))
{
$id = mysql_real_escape_string($_POST['id']);
$sql = mysql_query("DELETE FROM family_child WHERE family_child_name ='$id'");
if(!$sql)
{
echo ("Could not delete rows" .mysql_error());
}
}
}
?>

Categories