473,385 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Deleting rows in a database using select lists.

6
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($namequery) 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_row = mysql_fetch_array($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($query_delete) or die('Error, query failed');
}
echo "<meta http-equiv=\"refresh\" content=\"0\"; URL=\"AdminProjects.php\">";
echo "Row deleted!";
}
echo "</form>";
}
else{
echo "There are no project numbers in the system";
}
?>
Mar 14 '07 #1
6 2272
gauravgmbhr
107 100+
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($namequery) 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_row = mysql_fetch_array($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($query_delete) or die('Error, query failed');
}
echo "<meta http-equiv=\"refresh\" content=\"0\"; URL=\"AdminProjects.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 Expert 1GB
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($namequery) 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_row = mysql_fetch_array($result))
{
echo '<option value="'.$result_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($query_delete) or die('Error, query failed');
echo "<meta http-equiv=\"refresh\" content=\"0\"; URL=\"untitled3.php\">";
}
?>[/PHP]
Mar 15 '07 #3
yc022
6
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
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($namequery) 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_row = mysql_fetch_array($result))
{
echo '<option value="'.$result_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($query_delete) or die('Error, query failed');
echo "<meta http-equiv=\"refresh\" content=\"0\"; URL=\"untitled3.php\">";
}
?>[/PHP]
Mar 15 '07 #5
gauravgmbhr
107 100+
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
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
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...
13
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...
2
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
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...
9
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...
7
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...
24
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...
2
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...
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.