473,698 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting rows in a database using select lists.

6 New Member
Hi all, this is my first time using this so i'm not really sure how it works. please bear with me.

i am trying to delete a row in a database using a select list. First of all i have a query to produce what i would like to be held in the select (dropdown) list. i then create the select list with a delete button. I then try to create the query to delete the selected option from the list from the database, but nothing happens and an error message appears. This error message however is not one of the error messages returned from one of my queries. Please Help!!!! what am i doing wrong?

<?php

$namequery = "SELECT DISTINCT projectnumber
FROM projects";

$result = mysql_query($na mequery) or die('Error, query failed');
$count = mysql_num_rows( $result);
if ($count > 0){

echo "<form action=\"<$self >\" method=\"post\" >";
echo "<select name=\"item\">" ;

while($result_r ow = mysql_fetch_arr ay($result)){
echo "<option name=\"item2\"> ";
echo ($result_row[0]);
echo "</option>";
}
echo "</select>";
echo "<input type=\"submit\" name=\"Delete\" id=\"Delete\" value=\"Delete\ " />";
if(isset($_POST['Delete'])){
foreach($_POST[item] as $del){
$query_delete = ("DELETE FROM projects WHERE projectnumber ='".$del."'") ;
$result = mysql_query($qu ery_delete) or die('Error, query failed');
}
echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"AdminProj ects.php\">";
echo "Row deleted!";
}
echo "</form>";
}
else{
echo "There are no project numbers in the system";
}
?>
Mar 14 '07 #1
6 2294
gauravgmbhr
107 New Member
Hi all, this is my first time using this so i'm not really sure how it works. please bear with me.

i am trying to delete a row in a database using a select list. First of all i have a query to produce what i would like to be held in the select (dropdown) list. i then create the select list with a delete button. I then try to create the query to delete the selected option from the list from the database, but nothing happens and an error message appears. This error message however is not one of the error messages returned from one of my queries. Please Help!!!! what am i doing wrong?

<?php

$namequery = "SELECT DISTINCT projectnumber
FROM projects";

$result = mysql_query($na mequery) or die('Error, query failed');
$count = mysql_num_rows( $result);
if ($count > 0){

echo "<form action=\"<$self >\" method=\"post\" >";
echo "<select name=\"item\">" ;

while($result_r ow = mysql_fetch_arr ay($result)){
echo "<option name=\"item2\"> ";
echo ($result_row[0]);
echo "</option>";
}
echo "</select>";
echo "<input type=\"submit\" name=\"Delete\" id=\"Delete\" value=\"Delete\ " />";
if(isset($_POST['Delete'])){
foreach($_POST[item] as $del){
$query_delete = ("DELETE FROM projects WHERE projectnumber ='".$del."'") ;
$result = mysql_query($qu ery_delete) or die('Error, query failed');
}
echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"AdminProj ects.php\">";
echo "Row deleted!";
}
echo "</form>";
}
else{
echo "There are no project numbers in the system";
}
?>
WHAT is the error that u r getting

i thing u r applying foreach on a non-aaray variable
which will not work
either u create multiple selection list box instead of single selection drop down and send multiple values or dont use foreach

but still lemme know the error message

if u need any help in creatin multiple selection list box, and how to send all multiple values to PHP then do write to me
Mar 15 '07 #2
ak1dnar
1,584 Recognized Expert Top Contributor
Hi Here is Solution.Just use this coding by changing the SQL Script.
Please Note that here i am using a Table called product.
Columns:
p_id, p_name

In the Select box I am displaying p_name and as the value of the each and every List menu item I am Passing the p_id for deleting purpose.

That means the from the PHP delete section it will fetch this value Attribute.and pass it to the DELETE query. for this you can use your any other column. :) but for better performance, this primary key in my table is a Good idea.

And also no need to use double quotes for PHP echo. use single quotes. Then No need to escape them.

change this line also
[PHP]echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"untitled3 .php\">";[/PHP]

[PHP]<?php
require 'dbcon.php';
$namequery = "SELECT p_name,p_id
FROM products";

$result = mysql_query($na mequery) or die('Error, query failed');
$count = mysql_num_rows( $result);
if ($count > 0)
{
echo '<form action="'.$PHP_ SELF.'" method="POST">' ;
echo '<select name="item">';
while($result_r ow = mysql_fetch_arr ay($result))
{
echo '<option value="'.$resul t_row[1].'" >';
echo ($result_row[0]);
echo '</option>';
}
echo '</select>';
echo '<input type="submit" name="Delete" id="Delete" value="Delete" />';
echo '</form>';
}
else
{
echo "There are no project numbers in the system";
}

if($_POST['Delete'])
{
$del = $_POST['item'];
$query_delete = ("DELETE FROM products WHERE p_id ='".$del."'") ;
$result = mysql_query($qu ery_delete) or die('Error, query failed');
echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"untitled3 .php\">";
}
?>[/PHP]
Mar 15 '07 #3
yc022
6 New Member
Hi, the error i am getting is Access Forbidden. You don't have permission to access the requested object so i will defienetly take out the for each loop. It also says the document is either read-protected or not readable by the server.
Mar 15 '07 #4
yc022
6 New Member
Hi, thank you for your help. I'm having a go at changing my code just now so will let you know how i get on.









Hi Here is Solution.Just use this coding by changing the SQL Script.
Please Note that here i am using a Table called product.
Columns:
p_id, p_name

In the Select box I am displaying p_name and as the value of the each and every List menu item I am Passing the p_id for deleting purpose.

That means the from the PHP delete section it will fetch this value Attribute.and pass it to the DELETE query. for this you can use your any other column. :) but for better performance, this primary key in my table is a Good idea.

And also no need to use double quotes for PHP echo. use single quotes. Then No need to escape them.

change this line also
[PHP]echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"untitled3 .php\">";[/PHP]

[PHP]<?php
require 'dbcon.php';
$namequery = "SELECT p_name,p_id
FROM products";

$result = mysql_query($na mequery) or die('Error, query failed');
$count = mysql_num_rows( $result);
if ($count > 0)
{
echo '<form action="'.$PHP_ SELF.'" method="POST">' ;
echo '<select name="item">';
while($result_r ow = mysql_fetch_arr ay($result))
{
echo '<option value="'.$resul t_row[1].'" >';
echo ($result_row[0]);
echo '</option>';
}
echo '</select>';
echo '<input type="submit" name="Delete" id="Delete" value="Delete" />';
echo '</form>';
}
else
{
echo "There are no project numbers in the system";
}

if($_POST['Delete'])
{
$del = $_POST['item'];
$query_delete = ("DELETE FROM products WHERE p_id ='".$del."'") ;
$result = mysql_query($qu ery_delete) or die('Error, query failed');
echo "<meta http-equiv=\"refresh \" content=\"0\"; URL=\"untitled3 .php\">";
}
?>[/PHP]
Mar 15 '07 #5
gauravgmbhr
107 New Member
Hi, thank you for your help. I'm having a go at changing my code just now so will let you know how i get on.

Well make sure that the username and password that u using to connect to the DataBase Server have permissions To delete. so contact UR DB-admin To change ur privelages to database
Coz Most of the time The username given to a programmer by the admins of DB does nOt have permission to delete Rows From Table


And Try Avoiding Using Foreach loop when u are not using an array coz PHP 5 Will produce A E-Warning if the argument supplied to Foreach Loop Is Not An array.
And I guess U R Using Php4.XX
Mar 16 '07 #6
yc022
6 New Member
Yeah, it's php 4. I managed to get it working so thank you to everyone for your help. The only problem is that my id's are now getting muddled up, but i have seen a thread on this site that will help me fix that. Thank you again.
Mar 16 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
7854
by: rishi | last post by:
Looking for tips on how to improve performance on deleting records. In our database we do dataloads daily that require us to purge millions of records a day so any improvement in speed would be welcomed. CREATE OR REPLACE PROCEDURE ETL_CUSTATTRIB_STGTOTRG_ALT1v2 AS TYPE cust_t IS TABLE OF customer_master.customer_id%TYPE INDEX BY BINARY_INTEGER; TYPE attrib_t IS TABLE OF attribute_master.attribute_id%TYPE
13
7775
by: Dixie | last post by:
How do I, in VBA from an access form module, add 5 rows to the top of a spreadsheet called MySpreadsheet.xls. The worksheet is called MyWorksheet and there is already data in the first 5 rows. I simply need to insert 5 blank rows at the top and move the rest of the data down so it starts at row 6. dixie
2
2193
by: Zak McGregor | last post by:
Hi all I have a table, for simplicity's sake containing one field, called unid. for example, select unid, oid from table gives me something like this: unid | oid ---------+---------
4
1648
by: Jim Michaels | last post by:
Is it safe to do what is below? deleting from a resultset while you are processing it? I don't know how dynamic the SQL database is. I assume you get a cursor to live dataset. Even if it is a cursor as opposed to a table, will the cursor's integrity be ruined if the current record is deleted? $q2=mysql_query("SELECT * FROM table1 WHERE id1=5", $link); while ($row2=mysql_fetch_array($q2)) { //do some stuff with resultset...
9
1732
by: Hamed | last post by:
Hello I have a DataGrid that a is bound to a DataTable. Some of the rows in the DataTable should not be deleted. How can I prohibit deleting of some identified rows? The problem could be specified in the following format too: There is a DataView object that I want to prohibit deleting of some of its DataRowView objects. that is: when drv.Delete() is called (drv is the
7
6606
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer include them in the set that is displayed to the user) but I don't want to delete the corresponding row from the database. I've tried using the .Rows.Remove() method but an exception is thrown to say I don't have a 'delete' command in the data...
24
21600
by: Frank Swarbrick | last post by:
We have a batch process that inserts large numbers (100,000 - 1,000,000) of records into a database each day. (DL/I database.) We're considering converting it to a DB2 table. Currently we have logic in place that, prior to inserting any data, reads the first input record and checks to see if it already exists in the table. If the record already exists there are two options: 1) Don't continue, because you already ran this job today! 2)...
2
1663
by: Brad Pears | last post by:
I am working on a new vb.net 2005 project using SQL server 2000 as the backend db. I have a listview where control I want a user to be able to select either just one or multiple rows in the listview and perform an action on them. If the user chooses to delete these rows I wonder what the best way to handle the deletion of multiple rows is... Right now I have a stored procedure for deleteing jsut opne row. i.e delete from {tablename}...
5
2047
flexsingh
by: flexsingh | last post by:
Hello there, I have been trying to delete a row in php for a long time now and its getting frustrating, can any see if they could possible help me please. My first page is <?php // this starts the session session_start(); ?>
0
8671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9016
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.