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";
}
?>
6 2272
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
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]
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.
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]
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
---------+---------
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |