Προς το περιεχόμενο

eisagwgh dedomenwn se database apo ASP


παπαδακης

Προτεινόμενες αναρτήσεις

Δημοσ.

kalhspera paidia. ayto pou thelw na kanw einai na mporw na eisagw genika dedomena mesw enos site ftiagmeno me ASP se mia database (Access kata protimish). vrika kapoia tutorials kai se ena apo ayto analytika ena mini-guestbook. to thema einai oti xwris na kanw epemvash ston kwdika tou tutorial, meta thn eisagwgh stoixeiwn se mia arxikh selida 'guestbook_form.htm' me odhgei se mia selida 'add_to_guestbook.asp' h opoia einai entelws leykh!!!! Ti mporei na leipei apo ton kwdika kapoias apo tis selides kai den mou emfanizetai tipota? sas eyxaristw prokatavolika...sas dinw parakatw ton kwdika twn 3 selidwn pou eixe to tutorial :

 

guestbook_form.htm

>
<html>
<head>
<title>Guestbook Form</title>
</head>
<body bgcolor="white" text="black">
<!-- Begin form code --> 
<form name="form" method="post" action="add_to_guestbook.asp">
    Name: <input type="text" name="name" maxlength="20"> 
    <br>
    Comments: <input type="text" name="comments" maxlength="50"> 
    <input type="submit" name="Submit" value="Submit">
</form> 
<!-- End form code -->
</body>
</html>

add_to_guestbook.asp

>
<%

Dim adoCon                  'Holds the Database Connection Object
Dim rsAddComments     'Holds the recordset for the new record to be added
Dim strSQL                    'Holds the SQL query to query the database

'Create an ADO connection object
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using DSN connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")

'Create an ADO recordset object
Set rsAddComments = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT tblComments.Name, tblComments.Comments FROM tblComments;"

'Set the cursor type we are using so we can navigate through the recordset
rsAddComments.CursorType = 2

'Set the lock type so that the record is locked by ADO when it is updated
rsAddComments.LockType = 3

'Open the recordset with the SQL query 
rsAddComments.Open strSQL, adoCon

'Tell the recordset we are adding a new record to it
rsAddComments.AddNew

'Add a new record to the recordset
rsAddComments.Fields("Name") = Request.Form("name")
rsAddComments.Fields("Comments") = Request.Form("comments")

'Write the updated recordset to the database
rsAddComments.Update

'Reset server objects
rsAddComments.Close
Set rsAddComments = Nothing
Set adoCon = Nothing

'Redirect to the guestbook.asp page
Response.Redirect "guestbook.asp"
%> 

 

guestbook.asp

>
<html>
<head>
<title>My First ASP Page</title>
</head>
<body bgcolor="white" text="black">

<%
'Dimension variables

Dim adoCon  		'Holds the Database Connection Object
Dim rsGuestbook 	'Holds the recordset for the records in the database
Dim strSQL              'Holds the SQL query to query the database

'Create an ADO connection object
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")

'Create an ADO recordset object
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database with
strSQL = "SELECT tblComments.Name, tblComments.Comments FROM tblComments;"

'Open the recordset with the SQL query 
rsGuestbook.Open strSQL, adoCon

'Loop through the recordset
Do While not rsGuestbook.EOF
   'Write the HTML to display the contents of the recordset
Response.Write ("<br>")
Response.Write (rsGuestbook("Name"))
Response.Write ("<br>")
Response.Write (rsGuestbook("Comments"))
Response.Write ("<br>")

'Move to the next record in the recordset
rsGuestbook.MoveNext

Loop	

'Reset server variables
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>

</body>
</html>

 

Edited by Dionisos: Perikleiete ton kwdika me

[/b]
Δημοσ.

Den exw xrono gia na dw olo ton kwdika analytika h akomh kalytera na ton treksw, alla mporw na pw ta ekshs: h selida add_to_guestbook.asp einai ontws leykh dioti den exei html kwdika, vevaia sto telos ayths ths selidas kaneis redirect sth guestbook.asp, h opoia prepei na sou emfanisei tis kataxwrhseis ths vashs, opote mallon vlepeis thn guestbook.asp. Kane ligo debugging an 8es monos sou, diladi: pes mas poia selida einai ayth pou fainetai leukh h add_to_guestbook.asp h h guestbook.asp; koitakse mesa sth vash ean ontws prosti8entai ta dedomena pou eishgages;

Δημοσ.

file Sta, to prwto provlima einai oti sthn vash den prostithetai tipota...to deytero provlima einai oti meta to Submit sthn guestbook_form.htm den paei sthn guestbook.asp opws fainetai apo to redirect alla sthn add_to_guestbook.asp kai h opoia einai leykh, profanws gia ton logo pou eipes kai sy...Elpizw na katalaves. Se eyxaristw

Δημοσ.

Koitakse na deis,

 

a) patwntas to Submit einai logiko na se paei sthn selida add_to_guestbook.asp. Auto fainetai apo to <form name="form" method="post" action="add_to_guestbook.asp"> pou brisketai sto guestbook_form.htm

 

B) Swsta h add_to_guestbook.asp einai leuki, afou den exei katholou kwdika html alla ena redirection sto guestbook.asp

 

H guestbok.asp to mono pou kanei einai na sou emfanisei ta periexomena tis bashs. An den yparxei tipota mesa se aythn den sou bgazei tipota.

 

Les oti den sou bazei ta dedomena sou sthn bash. Sigourepsou oti h bash sou guestbook.mdb einai sto root directory tou web server sou kai an einai dwse dikaiomata ths read/write .

Δημοσ.

ta koitaksa ola ayta pou mou les file mcp, kai den exw kanei kapoio lathos. ekana kai mia allh dokimh xwris polla polla. ston parakatw kwdika ton opoio ton ekana antigrafh apo vivlio xrhsimopoiw entolh SQL kai vazw timh sthn vash xwris formes opws prin. epixeirw dhladh twra na valw to onoma Test me thn vohtheia entolhs. Anoigontas omws thn vash mou meta den vlepw pali tiopta mesa!! Mipws leipei tipota allo ston Server mou?

 

 

addrecord.asp

>
<%
set conn = serverCreateObject("adodb.connector")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("products.mdb")

sqlstring = "INSERT INTO Products (Name) VALUES (Test)"
conn.Execute sqlstring
conn.Close
%>

Δημοσ.

Σιγουρέψου ότι το path σου είναι σωστό.

 

Θα σου πρότεινα αντί να χρεισιμοποιήσεις το Server.MapPath να βάλεις το πραγματικό path π.χ. c:\wwwroot\dbases\products.mdb

 

Επίσης το sqlstring = "INSERT INTO Products (Name) VALUES (Test)" εάν το Name είναι string θα πρέπει να το γράψεις σαν

 

sqlstring = "INSERT INTO Products (Name) VALUES ('Test')"

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...