|
|
|
| Web Hosting |
![]() ![]() |
How Can I Make A Php Form To Save Info In Mysql |
Feb 14 2005, 10:58 PM
Post
#1
|
|
|
Premium Member Group: Members Posts: 292 Joined: 26-September 04 From: Bogotá, Colombia Member No.: 868 |
I need to write the php script to creat a form where visitor are able to wirte their name, telephone, adress... etc...
And that info has to be saved in a simple table on a MySQL databes that i also have to create (i know how to creat it) Anyone? |
|
|
|
Feb 15 2005, 09:16 AM
Post
#2
|
|
|
PsYcheDeLiC dR3aMeR Group: Admin Posts: 2,242 Joined: 29-January 05 From: Nakorn Chaisri, Thailand Member No.: 2,411 myCENTs:84.36 |
Here's a short tip. You'll need to setup 3 things:
1. The MySQL DB that your form data is getting written to. For our example lets just take two fields in a table, Name and Email in table Guests in a database named guestdb. 2. A html page with the form 3. A php file to process the form and write out the data to the MySQL db. Step 1 - Create the form File: index.html QUOTE <html> ... <body> <form action="./processform.php" method="post"> Name:<input type="text" name="name"> Email:<input type="text" name="email"> <input type="submit" value ="Submit"> </form> .... </html> This code just sets up a form with 2 fields to enter your name and email. The form post is redirected to your php code when you press submit. Step 2 - Create the form processor php file File: processform.php QUOTE <?php // Create the query string - be careful about all those single(') and double(") // quotes $query = "INSERT INTO guestdb.Guests ( Name, Email ) VALUES ( " $query .= " '$_POST['Name']' , '$_POST ['Email']' )"; mysql_query ($query, $conn_id); echo "Your details have been saved.\n"; ?> This shouldn't be hard to understand - it uses the environmental variable $_POST to fetch the form fields and constructs your query accordingly. It's a very basic piece of code...Just make sure both files are in the same directory. That's it. Try and let me know if it works. All the best ----------------------------------------------------------- This topic deals with both scripting language and database - but is more script oriented. So I'm moving it to the Programming > Scripting section - and leaving a link in here too. m^e |
|
|
|
Feb 15 2005, 11:44 PM
Post
#3
|
|
|
Premium Member Group: Members Posts: 292 Joined: 26-September 04 From: Bogotá, Colombia Member No.: 868 |
Ok, i have created this html page:
QUOTE <h1>COMUNIDAD</h1><br /><br /> <form action="activmysql.php" method="post"> Nombre:<input type="text" name="nombre" /> <br />Documento de Identificación:<input type="text" name="documento" /> <br />Email:<input type="text" name="email" /> <br />Telefono:<input type="text" name="telefono" /><br /> <input type="submit" value="Enviar" /> </form> With this php proccesor: QUOTE <?php $query = "INSERT INTO cursoweb.alumno ( documento, nombre, email, telefono ) VALUES ( " $query .= " '$_POST['documento']' , '$_POST ['nombre']' , '$_POST ['email']' , '$_POST ['telefono']' )"; mysql_query ($query, $conn_id); echo "Sus datos han sido guardados.\n"; ?> and this database and table: QUOTE ![]() And i'm getting this error: QUOTE Parse error: parse error, unexpected T_VARIABLE in W:\www\v2\activmysql.php on line 5 WHAT0S WRONG???? Thanks!!!! PS: great post microscopic^earthling, thanks a lot! |
|
|
|
Feb 16 2005, 12:00 AM
Post
#4
|
|
|
Premium Member Group: Members Posts: 292 Joined: 26-September 04 From: Bogotá, Colombia Member No.: 868 |
I'm almost shure that the problem is in the .php
PLEASE HELP ME!!! |
|
|
|
Feb 16 2005, 12:02 AM
Post
#5
|
|
|
Premium Member Group: Members Posts: 208 Joined: 6-September 04 From: England Member No.: 315 |
I believe your problem is you are using the single quotes (') within single quotes to enter the values into the database. This is why it is parsing and error, this is not allowed.
So your code would become: CODE $query .= " '$_POST[documento]' , '$_POST [nombre]' , '$_POST [email]' , '$_POST [telefono]' )"; you can see that i have removed the single quotes from within all of the values you want to enter into the database. instead of $_POST['fieldname'] you would have $_POST[fieldname]. I think that is the answer to your problem. If it is not then i am sure someone will have the answer for you. if you have any more errors feel free to post them here. good luck overture. |
|
|
|
Feb 20 2005, 03:54 PM
Post
#6
|
|
|
Premium Member Group: Members Posts: 292 Joined: 26-September 04 From: Bogotá, Colombia Member No.: 868 |
Ok, thanks everybopdy for helping me aout!!!!
Finally i had to use another code.. the following: note: in the line $connection = mysql_connect ("localhost", "rapco_sena", "virtual"); localhost =server rapco_sena = database username virtual= password ACTIVIDADMYSQL.PHP CODE <?php if ($submit == "click"){ // The submit button was clicked! // Get the input for fullname and email then store it in the database. $connection = mysql_connect ("localhost", "rapco_sena", "virtual"); if ($connection == false){ echo mysql_errno().": ".mysql_error()."<BR>"; exit; } $query = "insert into alumno values ('$documento', '$nombre', '$email', '$telefono')"; $result = mysql_db_query ("rapco_cursoweb", $query); if ($result){ echo "Success!"; } else{ echo mysql_errno().": ".mysql_error()."<BR>"; } mysql_close (); } else{ echo " <html><body> <DISABLED-FORM method=\"post\" action=\"activmysql.php\"> Escriba su documento de identidad: <DISABLED-INPUT type=\"text\" name=\"documento\"></DISABLED-INPUT><br> Escriba su Nombres y apellidos completos: <DISABLED-INPUT type=\"text\" name=\"nombre\"></DISABLED-INPUT><br> Escriba su direccion de e-mail: <DISABLED-INPUT type=\"text\" name=\"email\"></DISABLED-INPUT><br> Escriba su telefono con indicativo de ciudad en Colombia: <DISABLED-INPUT type=\"text\" name=\"telefono\"></DISABLED-INPUT><br> <DISABLED-INPUT type=\"submit\" name=\"submit\" value=\"click\"></DISABLED-INPUT> </DISABLED-FORM> </body></html> "; } ?> MOSTRAR.PHP CODE <?php $connection = mysql_connect ("localhost", "rapco_sena", "virtual"); if ($connection == false){ echo mysql_errno().": ".mysql_error()."<BR>"; exit; } $query = "select * from alumno"; $result = mysql_db_query ("rapco_cursoweb", $query); if ($result){ echo "<table border=1>"; echo "<tr><td><b>Documento de ID</b></td><td><b>Nombre completo</b></td><td> <b>Email</b></td><td><b>Telefono</b></td></tr>"; $numOfRows = mysql_num_rows ($result); for ($i = 0; $i < $numOfRows; $i++){ $doc = mysql_result ($result, $i, "documento"); $name = mysql_result ($result, $i, "nombre"); $emaila = mysql_result ($result, $i, "email"); $tel = mysql_result ($result, $i, "telefono"); echo "<tr><td>$doc</td><td>$name</td><td>$emaila</td><td>$tel</td></tr>"; } echo "</table>"; } else{ echo mysql_errno().": ".mysql_error()."<BR>"; } mysql_close (); ?> This script works perfectly!!!!!!!!!!!! |
|
|
|
Feb 20 2005, 05:14 PM
Post
#7
|
|
|
Premium Member Group: [HOSTED] Posts: 336 Joined: 22-September 04 Member No.: 798 |
tnx micro for unlocking
all i wanted to say is that there is a good reason php introduced the functions addslashes and stripslashes (more info: www.php.net/addslashes) let's watch this simple query: $query='instert into users(name) values("'.$_POST["name"].'")'; and let's say some evil user ( the query would become: instert into users(name) values("d"any") which would give you errors. replace $_POST["name"] with addslashes($_POST["name"]. this will add slashes when necessary, so all your queries work fine. when getting data out of the database, do stripslashes($data), and you get your nice username. it's a matter of security actually. if you don't do it, one could inject sql and obtain info they shouldnt be able to get. that would be all |
|
|
|
Feb 20 2005, 05:35 PM
Post
#8
|
|
|
Premium Member Group: Members Posts: 292 Joined: 26-September 04 From: Bogotá, Colombia Member No.: 868 |
Thanks for the aclaration...
when i saw the php.net url i remebered i should give credit for the script i posted before.. it's from http://phpworld.com/ EXCELENT SITE! |
|
|
|
Feb 23 2005, 12:27 PM
Post
#9
|
|
|
End Of Computer Group: Members Posts: 346 Joined: 1-September 04 From: .:: MARS ::. Member No.: 28 |
below you learn how to create a table,form,and display page!
first create a database and table: CREATE DATABASE form; USE DATABASE form; Now create a table like the following on the MySQL command line ( if you use phpMyAdmin, you can copy and past it.) CODE CREATE TABLE `form` ( `id` MEDIUMINT( 10 ) DEFAULT '0' NOT NULL AUTO_INCREMENT , `name` VARCHAR( 80 ) , `lastname` VARCHAR( 80 ) , `email` VARCHAR( 80 ) , `url` VARCHAR( 80 ) , `submitted` DATE, PRIMARY KEY ( `id` ) ); Ok i will now create a form for you to enter information , the info we want to submit is name , last name , url and email . Here is the form . name it "form.php". CODE <form action ="send.php" method = "post"> <table> <tr><td>Name:</td><td><input type ="text" name = "name" size="20"></td></tr> <tr><td>Last name:</td><td><input type ="text" name = "lastname" size="20"></td></tr> <tr><td>Email:</td><td><input type ="text" name = "email" size="20"></td></tr> <tr><td>Url:</td><td><input type ="text" name = "url" size="20"></td></tr> <tr><td><input type="submit"></td><td><input type = "reset"></td></tr> </table> </form> Now you should create a script page and name "send.php". QUOTE <?php //connect using your host , username and password info $connection = mysql_connect("localhost" , "username" , "password"); //select the form database $db = mysql_select_db("form" , $connection); //create or query inserting the appropriate values into the mysql //table $query = "INSERT INTO form(name , lastname ,email ,url, submitted ) VALUES('$name','$lastname','$email','$url', now())"; //execute query and store result as variable $result = mysql_query($query); //displays a message , you could redirect back to the form //instead , if you wished echo ("Thanks, your data entered."); ?> and finally your display page, name it "display.php". QUOTE <? mysql_pconnect("localhost","username","password"); mysql_select_db("form"); if(!isset($start)) $start = 0; $query = "SELECT * FROM form LIMIT " . $start . ", 10"; //do database connection $result = mysql_query($query); //you should do error checking $result = mysql_query($query); //start creating our table echo ("<table cellspacing='5' bgcolor='#006699' cellpadding='5' style='border-collapse: collapse' width='20'>"); //loop through rows in the database while ($rows = mysql_fetch_row($result)) { echo ("<table cellspacing='5' cellpadding='5' style='border-collapse: collapse' width='50%'>"); //in the next row we display the description which is $rows[2] echo ("<tr><td colspan ='2' bgcolor='#fbfbfb'><font color='#006699' size='2' face='Tahoma'>Name: $rows[1]</font></td></tr>"); echo ("<tr><td colspan ='2' bgcolor='#fbfbfb'><font color='#006699' size='2' face='Tahoma'>Last Name: $rows[2]</font></td></tr>"); echo ("<tr><td colspan ='2' bgcolor='#fbfbfb'><font color='#006699' size='2' face='Tahoma'>Email: $rows[3]</font></td></tr>"); echo ("<tr><td colspan ='2' bgcolor='#fbfbfb'><font color='#006699' size='2' face='Tahoma'>Url: $rows[4]</font></td></tr>"); echo "</table>"; } //finish or table echo "</table>"; //this code was wrong, I did not have the second query. // need another query to get the total amount of rows in our table $query = "SELECT count(*) as count FROM form"; $result = mysql_query($query); $row = mysql_fetch_array($result); $numrows = $row['count']; if($start > 0) echo "<div align='center'><< <a href=\"" . $PHP_SELF . "?start=" . ($start - 10) . "\">Previous Page</a>"; echo " "; echo " "; if($numrows > ($start + 10)) echo "<div align='center'> <a href=\"" . $PHP_SELF . "?start=" . ($start + 10) . "\">Next Page</a> >></div></div>"; ?> ------------------------- Be Successful ! Soleimanian, |
|
|
|
Feb 23 2005, 11:07 PM
Post
#10
|
|
|
Premium Member Group: Members Posts: 292 Joined: 26-September 04 From: Bogotá, Colombia Member No.: 868 |
soleimanian thanks!, but i have already solved my problem, if you see my posts tou'll see i posted the script.....
If someone is reading this to learn how to do it, go to: http://www.phpworld.com/articles/2000.02/mysql_000.html for a step by step explanation. |
|
|
|
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Expand / Collapse Navigation



Feb 14 2005, 10:58 PM


