View Full Version : A little help with a database problem
Rapt0r
01-20-2004, 03:31 PM
ok, im new at asp and stuff and ive been trying for ages to get this code to work with my database, the database is working correctly but the code isnt, id be very grateful for any help.
<%
User=Request.cookies("username")
img=trim(Request.Form("img"))
cap=trim(Request.Form("cap"))
cat=trim(Request.Form("cat"))
txt=trim(Request.Form("txt"))
if img<>"" then o=o+1
if cap<>"" then o=o+1
if cat<>"" then o=o+1
if txt<>"" then o=o+1
if o=4 then
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\lamescripter\db\ads.mdb"))
sSQL = "SELECT * From ads"
Set oRS = oConn.Execute(sSQL)
sSQL = "INSERT INTO ads (username,msg,img,thedate,cap,cat,txt) VALUES ('" & username & "',0,'" & img & "',0,'" & cap & "','" & cat & "','" & txt & "')"
Set oRS = oConn.Execute(sSQL)
Response.write("Congratulations, your post has been added to Ad-Lines Database, it can stay there for aslong as you want it for, but you must delete it when it is not needed")
%>
<%
sSQL = "UPDATE ads"
Set oRS = oConn.Execute(sSQL)
Else
Response.Write("Unknown error occurred, Email:Crazy_and_weird_dude_2003@hotmail.com and show him this number 11442266 and your post information.")
End If
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
%>
<%
<form name="form" action="post.asp" method="post">
<p> </p>
<p>Image Url:
<input type="text" name="textfield" width="178">
(If you have an image, enter a link to it here)</p>
<p> Caption :
<input name="textfield2" type="text" maxlength="20" width="178">
(A small caption to describe you product, 20 charactors max)</p>
<p> Details :
<textarea name="textfield3"></textarea>
(Enter details of your advert here)</p>
<p>Category :
<select name="select">
<option value="test">Test</option>
</select>
</p>
<p align="center">
<input type="submit" name="Submit" value="Post Ad">
<br>
</p>
</form>
%>
Please help if you can, thanks.
Roy Sinclair
01-20-2004, 04:34 PM
Er, what kind of errors are you getting and where in the code are you getting them at?
Rapt0r
01-20-2004, 05:47 PM
Well ive been working with the code and changed alot of stuff.
<style>table{font-family:arial;font-size:10px;}</style>
<table width="1003" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td height="93" colspan="3" valign="top"><img src="logo.jpg" width="293" height="93"></td>
</tr>
<tr>
<td height="4" colspan="3" valign="top"><img src="spacer.jpg" width="1004" height="4"></td>
</tr>
<tr>
<td width="192" height="14"></td>
<td width="644"></td>
<td width="168"></td>
</tr>
<tr>
<td height="379"></td>
<td valign="top">
<%
user=Request.cookies("username")
img=trim(Request.Form("img"))
cap=trim(Request.Form("cap"))
cat=trim(Request.Form("cat"))
txt=trim(Request.Form("txt"))
if img<>"" then o=o+1
if cap<>"" then o=o+1
if cat<>"" then o=o+1
if txt<>"" then o=o+1
if o=4 then
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\lamescripter\db\ads.mdb"))
sSQL = "SELECT * FROM ads"
Set oRS = oConn.Execute(sSQL)
Do While Not oRS.EOF
If lcase(oRS("username").Value) = lcase(username) Then stopall=1
oRS.MoveNext
Loop
If stopall="" Then
sSQL = "SELECT * FROM ads"
Set oRS = oConn.Execute(sSQL)
Do While Not oRS.EOF
If 1=2 then
end if
oRS.MoveNext
Loop
sSQL = "INSERT INTO ads (username,msg,img,thedate,cap,cat,txt) VALUES ('" & user & "',0,'" & img & "',0,'" & cap & "','" & cat & "','" & txt & "')"
Set oRS = oConn.Execute(sSQL)
response.write("blah")
Else
Response.Write("Sorry that caption has been taken please try again")
Response.cookies("pay")=yes
End If
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
Else
%>
<form action="post.asp" method="post">
<p> </p>
<p>Image Url:
<input type="text" name="img" width="178">
(If you have an image, enter a link to it here)</p>
<p> Caption :
<input name="cap" type="cap" maxlength="20" width="178">
(A small caption to describe you product, 20 charactors max)</p>
<p> Details :
<textarea name="txt"></textarea>
(Enter details of your advert here)</p>
<p>Category :
<select name="cat">
<option value="1">Test</option>
<option value="2">Test2</option>
</select>
</p>
<p align="center">
<input type="submit" name="Submit" value="Post Ad">
<br>
</p>
</form>
<%
end if%>
can anyone spot any new errors please.
ghell
01-20-2004, 06:50 PM
normaly im on the other side of these comments but...
only post the part that is showing errors and say what the errors are :thumbsup:
Rapt0r
01-20-2004, 07:24 PM
im not sure which part is showing errors, basically when i submit the form all i get is the sorry this caption has already been taken message.
Roy Sinclair
01-20-2004, 08:23 PM
This bit of code is obviously just waste:
sSQL = "SELECT * FROM ads"
Set oRS = oConn.Execute(sSQL)
Do While Not oRS.EOF
If 1=2 then
end if
oRS.MoveNext
Loop
Since 1 will never equal 2 and there's nothing else inside that loop all that code accomplishes nothing except wasting server resources.
It's not apparent that the variable "o" is ever initialized and neither is the "stopall" variable. Both should be initialized to some default value. It's also apparent that youre using "stopall" as both numeric and alphabetic, you should use any variable in a consistent fashion as much as possible.
If you haven't initialized "stopall" somewhere else on your page then that's likely the reason it doesn't equal "", since you didn't initialize it to an empty string you can't count on it being equal to an empty string.
Rapt0r
01-20-2004, 08:55 PM
Ok,thanks i took out that bit that was useless but im unsure of what to do next, dont mean to be a pain but could u help me pls.
<style>table{font-family:arial;font-size:10px;}</style>
<table width="1003" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td height="93" colspan="3" valign="top"><img src="logo.jpg" width="293" height="93"></td>
</tr>
<tr>
<td height="4" colspan="3" valign="top"><img src="spacer.jpg" width="1004" height="4"></td>
</tr>
<tr>
<td width="192" height="14"></td>
<td width="644"></td>
<td width="168"></td>
</tr>
<tr>
<td height="379"></td>
<td valign="top">
<%
user=Request.cookies("username")
img=trim(Request.Form("img"))
cap=trim(Request.Form("cap"))
cat=trim(Request.Form("cat"))
txt=trim(Request.Form("txt"))
if img<>"" then o=o+1
if cap<>"" then o=o+1
if cat<>"" then o=o+1
if txt<>"" then o=o+1
if o=4 then
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\lamescripter\db\ads.mdb"))
sSQL = "SELECT * FROM ads"
Set oRS = oConn.Execute(sSQL)
Do While Not oRS.EOF
If lcase(oRS("username").Value) = lcase(username) Then stopall=1
oRS.MoveNext
Loop
If stopall="" Then
sSQL = "INSERT INTO ads (username,msg,img,thedate,cap,cat,txt) VALUES ('" & user & "',0,'" & img & "',0,'" & cap & "','" & cat & "','" & txt & "')"
Set oRS = oConn.Execute(sSQL)
response.write("blah")
Else
Response.Write("Sorry that caption has been taken please try again")
Response.cookies("pay")=yes
End If
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
Else
%>
<form action="post.asp" method="post">
<p> </p>
<p>Image Url:
<input type="text" name="img" width="178">
(If you have an image, enter a link to it here)</p>
<p> Caption :
<input name="cap" type="cap" maxlength="20" width="178">
(A small caption to describe you product, 20 charactors max)</p>
<p> Details :
<textarea name="txt"></textarea>
(Enter details of your advert here)</p>
<p>Category :
<select name="cat">
<option value="1">Test</option>
<option value="2">Test2</option>
</select>
</p>
<p align="center">
<input type="submit" name="Submit" value="Post Ad">
<br>
</p>
</form>
<%
end if%>
Roy Sinclair
01-20-2004, 08:59 PM
<style>table{font-family:arial;font-size:10px;}</style>
<table width="1003" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td height="93" colspan="3" valign="top"><img src="logo.jpg" width="293" height="93"></td>
</tr>
<tr>
<td height="4" colspan="3" valign="top"><img src="spacer.jpg" width="1004" height="4"></td>
</tr>
<tr>
<td width="192" height="14"></td>
<td width="644"></td>
<td width="168"></td>
</tr>
<tr>
<td height="379"></td>
<td valign="top">
<%
user=Request.cookies("username")
img=trim(Request.Form("img"))
cap=trim(Request.Form("cap"))
cat=trim(Request.Form("cat"))
txt=trim(Request.Form("txt"))
o=0
stopall=""
if img<>"" then o=o+1
if cap<>"" then o=o+1
if cat<>"" then o=o+1
if txt<>"" then o=o+1
if o=4 then
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\lamescripter\db\ads.mdb"))
sSQL = "SELECT * FROM ads"
Set oRS = oConn.Execute(sSQL)
Do While Not oRS.EOF
If lcase(oRS("username").Value) = lcase(username) Then stopall=1
oRS.MoveNext
Loop
If stopall="" Then
sSQL = "INSERT INTO ads (username,msg,img,thedate,cap,cat,txt) VALUES ('" & user & "',0,'" & img & "',0,'" & cap & "','" & cat & "','" & txt & "')"
Set oRS = oConn.Execute(sSQL)
response.write("blah")
Else
Response.Write("Sorry that caption has been taken please try again")
Response.cookies("pay")=yes
End If
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
Else
%>
<form action="post.asp" method="post">
<p> </p>
<p>Image Url:
<input type="text" name="img" width="178">
(If you have an image, enter a link to it here)</p>
<p> Caption :
<input name="cap" type="cap" maxlength="20" width="178">
(A small caption to describe you product, 20 charactors max)</p>
<p> Details :
<textarea name="txt"></textarea>
(Enter details of your advert here)</p>
<p>Category :
<select name="cat">
<option value="1">Test</option>
<option value="2">Test2</option>
</select>
</p>
<p align="center">
<input type="submit" name="Submit" value="Post Ad">
<br>
</p>
</form>
<%
end if%>
Rapt0r
01-20-2004, 09:03 PM
ok thanks,but im still getting
Sorry that caption has been taken please try again
im really confused, my mate did the database for me so it could be that but he said he did it right.
Can you possibly help me cause im not sure howto make databases.
Roy Sinclair
01-20-2004, 10:07 PM
That's probably because you've also got this misalignment of variable names:
...
<%
user=Request.cookies("username")
img=trim(Request.Form("img"))
...
If lcase(oRS("username").Value) = lcase(username) Then stopall=1
...
Rapt0r
01-20-2004, 11:14 PM
Hmm i changed that but still it doesnt work, this is a real pain.
Roy Sinclair
01-20-2004, 11:28 PM
Time to add a few diagnostic "response.write" statements to your page to display the variables you're checking at key points.
Rapt0r
01-20-2004, 11:34 PM
im new at this sorry, im not quite sure what you mean.
Roy Sinclair
01-21-2004, 12:25 AM
What I mean is to add some code which will display the value of some key variables at a point during the execution of the web page where the values they contain will give you a clue as to what might be not working.
user=Request.cookies("username")
img=trim(Request.Form("img"))
cap=trim(Request.Form("cap"))
cat=trim(Request.Form("cat"))
txt=trim(Request.Form("txt"))
o=0
stopall=""
if img<>"" then o=o+1
if cap<>"" then o=o+1
if cat<>"" then o=o+1
if txt<>"" then o=o+1
if o=4 then
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\lamescripter\db\ads.mdb"))
sSQL = "SELECT * FROM ads"
Set oRS = oConn.Execute(sSQL)
Do While Not oRS.EOF
If lcase(oRS("username").Value) = lcase(username) Then stopall=1
oRS.MoveNext
Loop
response.write "<hr>Stopall=" & stopall & "<br>Username=" & username & "<hr>"
If stopall="" Then
sSQL = "INSERT INTO ads (username,msg,img,thedate,cap,cat,txt) VALUES ('" & user & "',0,'" & img & "',0,'" & cap & "','" & cat & "','" & txt & "')"
Set oRS = oConn.Execute(sSQL)
response.write("blah")
Else
Response.Write("Sorry that caption has been taken please try again")
Response.cookies("pay")=yes
End If
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
Else
%>
<form action="post.asp" method="post">
<p> </p>
<p>Image Url:
<input type="text" name="img" width="178">
(If you have an image, enter a link to it here)</p>
<p> Caption :
<input name="cap" type="cap" maxlength="20" width="178">
(A small caption to describe you product, 20 charactors max)</p>
<p> Details :
<textarea name="txt"></textarea>
(Enter details of your advert here)</p>
<p>Category :
<select name="cat">
<option value="1">Test</option>
<option value="2">Test2</option>
</select>
</p>
<p align="center">
<input type="submit" name="Submit" value="Post Ad">
<br>
</p>
</form>
<%
end if%>
Rapt0r
01-21-2004, 12:28 AM
--------------------------------------------------------------------------------
Stopall=1
Username=
--------------------------------------------------------------------------------
Sorry that caption has been taken please try again
thats what i get
Rapt0r
01-21-2004, 12:29 AM
ill login then try
Rapt0r
01-21-2004, 12:30 AM
ah thats the problem i werent logged in :o
Thanks for all your help mate.:)
Roy Sinclair
01-21-2004, 12:44 AM
What that tells you is that you need to CHECK ALL USER INPUT for incorrect data, failure to provide input, oversized or undersized input and other common or uncommon input problems.
The GIGO rule still applies (Garbage In = Garbage Out), unchecked data cannot be accepted as valid data, I can't begin to tell how many database fields I've seen filled with hard to use or unusable garbage when some simple input checks would have resulted in valuable and easy to use data.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.