0
<%
postit = request.querystring("thispost")
response.write(postit)
%> 

postit is the variable. The response.write works and this is all above the SQL statement below.

This is the SQL however when I add the postit variable I get this error message:

delCmd.CommandText="DELETE * FROM post WHERE (pos_ID = postit )"
Microsoft Access Database Engine error '80040e10'
No value given for one or more required parameters. 
/student/s0190204/wip/deleterecord.asp, line 32
user692942
  • 16,398
  • 7
  • 76
  • 175

6 Answers6

2

Add a parameter to the SQL:

delCmd.CommandText="DELETE * FROM post WHERE (pos_ID = ?)"
delCmd.Parameters.Append delCmd.CreateParameter("posid", adInteger, adParamInput)   ' input parameter
delCmd.Parameters("posid").Value = postit
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Parameter ?_1 has no default value. –  Dec 18 '13 at 14:09
  • @Jayyf9 well it should work but you may need to set the value on a separate line. See my updated answer. – D Stanley Dec 18 '13 at 14:23
  • The problem is the variable name - should be `postit` not `posid`. – Paul Dec 18 '13 at 15:02
  • 1
    @Jayyf9: It's always a good idea to turn [`Option Explicit`](http://classical-asp.blogspot.co.uk/2010/08/option-explicit.html) on at the beginning of your ASP page. – Paul Dec 18 '13 at 15:20
1

Couple of things that will help you in the future

  1. Use Option Explicit to avoid hiding issues that will come back to bite you later on
  2. Use ADODB.Command object, which is very versatile enabling to do a range of database calls, from simple dynamic SQL statements to Stored Procedures without the risk of SQL injection.

There are a few tips that can speed things up when using the ADODB.Command object in your code which will be demonstrated in the example below (assumes you already have a connection string stored in a global config call gs_connstr);

<%
Option Explicit

Dim postit
postit = Request.QueryString("thispost")
'Always do some basic validation of your Request variables
If Len(postit) > 0 And IsNumeric(postit) Then CLng(postit) Else postit = 0

Dim o_cmd, o_rs, a_rs, i_row, i_rows, l_affected
Dim SQL

'SQL statement to be executed. For CommandType adCmdText this can be any dynamic
'statement, but adCmdText also gives you an added bonus - Parameterised Queries
'instead of concatenating values into your SQL you can specify placeholders (?)
'that you will define values for that will get passed to the provider in the order
'they are defined in the SQL statement.
SQL = "DELETE * FROM post WHERE (pos_ID = ?)"

Set o_cmd = Server.CreateObject("ADODB.Command")
With o_cmd
  'ActiveConnection will accept a Connection String so there is no need
  'to instantiate a separate ADODB.Connection object the ADODB.Command object
  'will handle this and also open the connection ready.
  .ActiveConnection = gs_connstr
  .CommandType = adCmdText
  .CommandText = SQL
  'When using Parameters the most important thing to remember is the order you
  'appended your parameters to the Parameters collection as this will determine
  'the order in which they are applied to your SQL query at execution. Because
  'of this the name you give to your parameters is not important in terms of
  'execution but I find specifying a meaningful name is best (especially when
  'revisiting some code a few years down the line).
  Call .Parameters.Append(.CreateParameter("@pos_ID", adInteger, adParamInput, 4))
  'Parameter values can be passed in via the Execute() method using an Array
  'without having to define the parameter values explicitly. You can also specify
  'the records affected value to return number of rows affected by a DELETE,
  'INSERT or UPDATE statement.
  .Execute(l_affected, Array(postit))
End With
'Always tidy up after yourself, by releasing your object from memory, this will
'also tidy up your connection as it was created by the ADODB.Command object.
Set o_cmd = Nothing
%>
user692942
  • 16,398
  • 7
  • 76
  • 175
0

You're not passing the value of postit to Access; instead, you're telling Access to find & use a variable called postit. Of course, said variable doesn't exist in Access -- it only exists in your code. The fix is just a couple of quote marks and a pair of ampersands.

delCmd.CommandText="DELETE * FROM post WHERE (pos_ID = " & postit & " )"

(Naturally, you should validate postit before you go sending it off to your database. A simple CDbl() can do the trick, assuming it's a numeric value.)

user692942
  • 16,398
  • 7
  • 76
  • 175
Martha
  • 3,932
  • 3
  • 33
  • 42
  • Personally not a big fan of using concatenation in SQL queries, parameterised queries are far safer and easy to read. – user692942 Dec 20 '13 at 11:11
0

Try this code:

<% Dim postit, stringSQL, objectCon
   postit = request.querystring("thispost")

   Set objectCon = Server.CreateObject("ADODB.Connection")
   objectCon.ConnectionString  "Driver={SQL SERVER};Server=server_name;UID=user_name;PWD=password;Database=database_name" 'SET CONNECTION STRING OF YOUR DATABASE
   stringSQL = "DELETE FROM post WHERE pos_id='" & postit & "'"

   objectCon.Open
   objectCon.Execute(stringSQL)
   objectCon.Close() %>
Stuart
  • 711
  • 1
  • 11
  • 35
0

Here I'm trying to get the car_color of the car using the id of the car. Now I can use the car_color record set in my code. I would also recommend using CLng when passing in values, it'll prevent sql injections.

If the carID is not a number you'll get the following error:

"500 response from the server. Remember to open and close the sql connection."

Here is the code:

sql = "Select * from Cars Where ID = " & clng(carID)
    rs.open
    if not rs.eof then
      carID = rs("car_ID")
      carColor = rs("car_color")
    end if
    rs.close
Malekai
  • 4,765
  • 5
  • 25
  • 60
ana2140358
  • 19
  • 3
-1

More easy for delete, this way is useful when not need to check the recordset:

cn.open "yourconnectionstring"
cn.execute "DELETE * FROM post WHERE pos_ID = " & request.querystring("thispost")
cn.close
Frank N Stein
  • 2,219
  • 3
  • 22
  • 26
  • 3
    The problem with this method is that we don't know where the `thispost` (in your case) is coming from and opens the application to [SQL injection attacks](http://en.wikipedia.org/wiki/SQL_injection). – Paul Dec 18 '13 at 15:04
  • The validation of the parameters is not mentioned. I try to use the same request to explain the origin of the value. – Frank N Stein Dec 18 '13 at 15:19
  • 1
    Parametrization and validation are especially important in the case of a DELETE statement such as this. Imagine someone submitted a thispost of `foo OR 1 = 1`. That would delete every record in the table. – Mike Poole Jul 10 '19 at 17:04