Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ASP / Database Access / October 2007



Tip: Looking for answers? Try searching our database.

Page inserting blank

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
navin - 13 Oct 2007 14:57 GMT
Hi All,

i am trying to insert some records through ASP. In order to insert new
record, i have a link "Add New". Problem is when i click on link "Add
New" and go back to the view page,  without saving any data, my page
still inserts blank data into the database. i am using the below code:

<%
' *** Begin DB Setup ***
Dim strConnString
Dim RSConn
Dim DBConn
' Sample access OLEDB CONN String.
'strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
'    Server.MapPath("acsdata.mdb") & ";"
Dim strDBPath
strDBPath=Server.MapPath("acsdata.mdb")

Set DBConn = Server.CreateObject("ADODB.Connection")
DBConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
strDBPath & ";"
Set RSConn=Server.CreateObject("ADODB.Recordset")

Dim strSQL            ' String in which to build our SQL command

Dim weekTitle      ' Title
Dim weekPath   ' Path
Dim weekName  ' Week
Dim Validate_Form
Dim msg_Disp

'If Request.Form("action") <> "Save Form Data" Then
'    Response.Redirect("hps_supp_add.html")
    ' Show the form

    weekTitle = Trim(Request.Form("mtitle"))
    weekPath = Trim(Request.Form("mpath"))
    weekname = Trim(Request.Form("mWeek"))

    Validate_Form=true

    If weekTitle = "" or weekPath = "" Then
                msg_disp="AAA"
    end if

    IF NOT Validate_Form THEN

%>
 <HTML>
 <BODY>
<!-- Error. Click back in your browser, and fill it out properly!-->
<% Response.Write(msg_Disp)
%>

 </HTML>
 </BODY>
 <%

Else

strSQL = ""
        strSQL = strSQL & "INSERT INTO metricsLink "
        strSQL = strSQL & "(metricsTitle, metricsPath, metricsWeek) " &
vbCrLf
        strSQL = strSQL & "VALUES ("
        strSQL = strSQL & "'" & weekTitle & "'"
        strSQL = strSQL & ", "
        strSQL = strSQL & "'" & weekPath & "\'"
        strSQL = strSQL & ", "
        strSQL = strSQL & "'Metrics Wk " & weekname & "'"
        strSQL = strSQL & ");"

        RSConn.CursorType=2
        RSConn.LockType=3
        RSConn.Open strSQL,DBConn

        DBConn.Close
        Set DBConn = Nothing

        ' Display a verification message and we're done!

    End IF

    %>

    <form action="AddMetrics.asp" method="post">
    <input type="hidden" name="action" value="Save Form Data" />
    <table border="0" id="table1" align=center>
    <tr>
        <td align="right" colspan="2">
    <table border="0" width="100%" id="table2">
        <tr>
            <td>
            <p align="center"><u><b>Upload Metrics</b></u></td>
            <td width="87"><a href="MetricsLinks.asp">Back To List</a></td>
        </tr>
    </table>
        </td>
    </tr>
    <tr>
        <td align="right"><strong>Metrics Title:</strong></td>
        <td align="left"><input type="text" name="mtitle" maxlength="50" /></
td>
    </tr>
    <tr>
        <td align="right"><strong>Metrics Path:</strong></td>
        <td align="left"><input type="text" name="mpath" maxlenght="50" /></
td>
    </tr>
    <tr>
        <td align="right"><strong>Metrics Week:</strong></td>
        <td align="left"><select size="1" name="mweek">
        <%
        Dim weekNanme
            for weekName = 1 to 52 %>

            <option> <%= weekName %> </option>
            <% Next %>
            </select></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>
            &nbsp;&nbsp;&nbsp;
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="submit" value="Save" style="font-weight: 700" />
            <input type="reset" value="Clear" style="font-weight: 700" /></td>
    </tr>
    </table>

    </form>

</div>

</body>

</html>

Please help.

thanks,
navin
Jon Paal [MSMD] - 13 Oct 2007 16:45 GMT
validate your sql statement with a Response.Write to verify you have it correct before the sql is executed.
Daniel Crichton - 15 Oct 2007 11:31 GMT
navin wrote  on Sat, 13 Oct 2007 06:57:28 -0700:

> Hi All,

> i am trying to insert some records through ASP. In order to insert new
> record, i have a link "Add New". Problem is when i click on link "Add
> New" and go back to the view page,  without saving any data, my page
> still inserts blank data into the database. i am using the below code:

Are you sure you're using that code? It's missing some <% and %> tags so
won't compile, let alone run. More comments inline, including the reason why
it inserts blank rows.

> weekTitle = Trim(Request.Form("mtitle"))
> weekPath = Trim(Request.Form("mpath"))
> weekname = Trim(Request.Form("mWeek"))

> Validate_Form=true

> If weekTitle = "" or weekPath = "" Then
> msg_disp="AAA"
> end if

> IF NOT Validate_Form THEN

This part of the logic never gets run, because you set it to True earlier
and didn't set Validate_Form to False in your little validation check above.
So the next bit always runs, even if weekTitle or weekPath are blank ...

> Else

> strSQL = ""
> strSQL = strSQL & "INSERT INTO metricsLink "
[quoted text clipped - 7 lines]
> strSQL = strSQL & "'Metrics Wk " & weekname & "'"
> strSQL = strSQL & ");"

> RSConn.CursorType=2
> RSConn.LockType=3
> RSConn.Open strSQL,DBConn

> DBConn.Close
> Set DBConn = Nothing

When you request the page, the weekTitle and weekPath values are blank
(because you don't have those form variables filled in), and there's no
validation (you just force the validation variable value to True and never
change it), so voila! you get a blank row inserted.

Signature

Dan

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.