Showing posts with label learning. Show all posts
Showing posts with label learning. Show all posts

Wednesday, March 21, 2012

deleting data from database

Ive just started learning ASP and i am using MySQL databases. So far ive managed to make a newsposting thing that allows me to insert news and read them. Now i want to have the option of deleting data from the database directly from the site, but whatever i try to write in the sql command it doesn't want to work. I don't know where i should ask this question but ill start here =)
ive read several guides but some of them are PHP guides or ASP but msaccess databases.
this is how my stuff looks at the moment:

(page name: newsremove.asp)
<%
if request("delid") <> "" then
delid=request("delid")
sql="DELETE * FROM news WHERE ID=" & delid
conn.execute(sql)
end if
%>
<form action="newsremove.asp" method="post">
Type in the ID of the post you wish to remove: <input type="text" name="delid" size=20><br>
<input type="submit" value="remove this post!">
</form>

I also have this little part where i show all the news but i don't think it has anything to do with the errors i get.

do until rs.eof
response.write "<hr align=left width=300 size=2 color=black noshading>"
response.write rs("time") & " by " & rs("poster") & ". ID: " & rs("id")
rs.moveNext
loop

The errors i get when i try to remove a entry is
"ADODB.Connection.1 (0x80004005)
SQLState: 42000 Native Error Code: 1064 [TCX][MyODBC]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM news WHERE ID=9' at line 1
/newsremove.asp, line 25"

ANY HELP AT ALL IS VERY THX!the error message points to exactly where the problem is

remove the asterisk :)

DELETE FROM news WHERE ID=937|||:O what!? i dont need the *? im gona try and see =)|||well, now i get my old friend "expected 'end' "

"Expected 'End'
/newsremove.asp, line 31, column 40"

Line 31 is the empty line between "if request("delid") = "" then" and "delid=request("delid")"

<form action="newsremove.asp" method="post">
Type in the ID of the post you wish to remove: <input type="text" name="delid" size=20><br>
<b>Make sure its the right one! Once you delete a post it can't be recovered!!</b><br>
<input type="submit" value="Im sure, remove this post!">
</form>
<%

if request("delid") = "" then

delid=request("delid")
sql="DELETE FROM news WHERE ID="& delid""
set rs=conn.execute(sql)

end if


sql="SELECT * FROM news ORDER BY tid DESC"
set rs=conn.execute(sql)

do until rs.eof
response.write "<hr align=left width=300 size=2 color=black noshading>"
response.write rs("TID") & " by " & rs("POSTER") & ". ID: " & rs("ID")
rs.moveNext
loop
%>

I get this friggin error all the time!! i know the answer is that i am missing a "end if" statement but i aint!! Please fast help is appreciated!|||i don't code in that particular scripting language, but it sure looks like you are trying to delete based on an id that isn't there

if request("delid") = ""
then
delid=request("delid")
sql="DELETE FROM news WHERE ID="& delid""|||hmm sounds very reasonable now that you mention it=) ill try it tomorrow now i gotta go to bed

Friday, February 24, 2012

Delete rows in relational database

Hi,
I have a relational database. Pretty simple I created it just for learning from it.

I have two tables "Categories" and "Subcategories".

I have a field called "CategoryID" in the Subcategories table that is a foreign key , and is related to the first table (categories).

Now what I want to do is, when I delete a row in the "Categories" table, I also want to delete the related rows in the other table (if any exist) at the same time.

I hope I explained the problem accurately, if you need any details please let me know

Thanks in advace,
WassimHi,

You need to delete the rows from the subcategories table first and then delete the corresponding rows from the categories table.

It should be pretty easy as the CategoryID should be in SubCategories table and that the field you need to use to delete rows from the subcategories table.

I hope this helps.

Aash.|||Look at the CASCADE DELETE capability defined when you set the relationship between the two tables. This is inherent and designed to provide for integrity as part of the db structure

Jim|||

Quote:

Originally Posted by Jim Doherty

Look at the CASCADE DELETE capability defined when you set the relationship between the two tables. This is inherent and designed to provide for integrity as part of the db structure

Jim


Hi Jim,

yes that's what I did exactly. That's amazing. if you force your tables to "Cascade Delete", and you delete a row in a parent table, all the rows in the childs tables will also be removed :)

Thanks|||

Quote:

Originally Posted by Tea Maker

Hi Jim,

yes that's what I did exactly. That's amazing. if you force your tables to "Cascade Delete", and you delete a row in a parent table, all the rows in the childs tables will also be removed :)

Thanks


You got it.... glad it helped you

Jim