form to database

tonytony ModeratorAdministrator
ok i have a form which is currentlylocated here:www.sinerfy-media.com/New/addlink.html it has 4 fields (1 is a drop down menu) which are category(drop down menu), URL, title and description my code is here:
[code:1:412c5ca3c0]<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>

<form name="form1" id="form1" method="post" action="">
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="124">Category:</td>

<td width="376">
<select NAME="category">
<option selected value="Hosting">Hosting
<option value="SEO">SEO
<option value="Tutorials">Tutorials
<option value="Forums">Forums
<option value="Other">Other
</select>
</td>
</tr>
<tr>
<td>URL:</td>

<td><input name="textfield" type="text" value="http://" /></td>
</tr>
<tr>
<td>Text:</td>
<td><input type="text" name="text" /></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="description"></textarea></td>

</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>

</form>
<div align="center"><b>If you are not authorised to be here please use this link
to go to our homepage: <a href="index.html">home</a></b> </div>
</body>
</html>[/code:1:412c5ca3c0]

i want these fields to be written to the database when the submit button is clicked, i was told this would work to do that: [code:1:412c5ca3c0] $qry="INSERT INTO my_table (category, url, test, description) VALUES (\"$category\", \"$url\", \"$text\", \"$description\")"; [/code:1:412c5ca3c0]

But as i havnt learnt PHP yet i was wondering if anyone would be able to show me where to put it?

Cheers
tony

Comments

  • PythonPython Forum Leader The Royal RAM
    the action of your form on your html page should point to sometning like process.php

    then inside a php file you would do something like this:

    [code:1:720728b9d1]<?php

    $category=$_POST['category'];
    $url=$_POST['url'];
    $test=$_POST['test'];
    $description=$_POST['description'];


    // connect to database using the mysql_connect function()
    $db = mysql_connect("localhost","username","password");
    //The function mysql_select_db() sets the current
    //database to the one specified in this case "test"
    mysql_select_db("test" ,$db);

    $qry="INSERT INTO my_table (category, url, test, description) VALUES (\"$category\", \"$url\", \"$text\", \"$description\")";
    //NOTE - IM NOT SURE IF THOSE SLASHES SHOULD BE THERE

    ?>
    [/code:1:720728b9d1]

    And i think thats it. im also just learning PHP so this code might be slightly/totally wrong, lol

    The Royal Ram

  • tonytony Moderator Administrator
    ok so that snippet of code i was given i cant just add that into my form page and save the form page as .php?
  • PythonPython Forum Leader The Royal RAM
    its not quite that simple. you can however copy that bit of code i wrote and paste it into your page and then set the form action to something like

    <php echo $php_self; ?>

    then it will use itself as the processor

    The Royal Ram

  • tonytony Moderator Administrator
    ah youve lost me now if id have known it was gonna be this hard i dont think id have started
  • PythonPython Forum Leader The Royal RAM
    its not hard. just rename the form page to something.php and as the form action put <?php echo $php_self; ?>

    then under the form copy and paste that bit of PHP i wrote

    The Royal Ram

  • tonytony Moderator Administrator
    so the page will look like:

    <?php echo $php_self; ?>
    FORM HTML HERE
    then your code here?

    thanks ill have a go later if thats rite
  • PythonPython Forum Leader The Royal RAM
    something like this:

    [code:1:b1bf4724fa]<?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>

    <body>

    <form name="form1" id="form1" method="post" action="php echo $php_self; ?> ">
    <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td width="124">Category:</td>

    <td width="376">
    <select NAME="category">
    <option selected value="Hosting">Hosting
    <option value="SEO">SEO
    <option value="Tutorials">Tutorials
    <option value="Forums">Forums
    <option value="Other">Other
    </select>
    </td>
    </tr>
    <tr>
    <td>URL:</td>

    <td><input name="textfield" type="text" value="http://" /></td>
    </tr>
    <tr>
    <td>Text:</td>
    <td><input type="text" name="text" /></td>
    </tr>
    <tr>
    <td>Description</td>
    <td><textarea name="description"></textarea></td>

    </tr>
    <tr>
    <td>Password:</td>
    <td><input type="password" name="password" /></td>
    </tr>
    <tr>
    <td colspan="2"><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
    </table>

    </form><br><br>
    <?php

    $category=$_POST['category'];
    $url=$_POST['url'];
    $test=$_POST['test'];
    $description=$_POST['description'];


    // connect to database using the mysql_connect function()
    $db = mysql_connect("localhost","username","password");
    //The function mysql_select_db() sets the current
    //database to the one specified in this case "test"
    mysql_select_db("test" ,$db);

    $qry="INSERT INTO my_table (category, url, test, description) VALUES (\"$category\", \"$url\", \"$text\", \"$description\")";
    //NOTE - IM NOT SURE IF THOSE SLASHES SHOULD BE THERE
    echo 'There were no errors. All was succesful';
    ?> <br>

    <div align="center"><b>If you are not authorised to be here please use this link
    to go to our homepage: <a href="index.html">home</a></b> </div>
    </body>
    </html>[/code:1:b1bf4724fa]


    This code might not be perfect because im not too sure about the PHP part of it. Try it

    The Royal Ram

  • tonytony Moderator Administrator
    ok i had to change it slightly to
    [code:1:a841d72f72]<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>

    [/code:1:a841d72f72] at the very top

    ok it loads no mysql errors after i made a couple of changes
    the problem comes when i fill in the fileds and hit submit

    you can try it yourself

    http://sinergy-media.com/New/addlink.php
    fill in the fields hit submit and it comes up with a 404 error:
    The requested URL /New/php echo $php_self; was not found on this server.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
  • PythonPython Forum Leader The Royal RAM
    the reason that is happening is because your form action isnt correct.

    it should be:

    <?php echo $php_self; ?>


    sorry about that - it was my mistake for typing it wrong above

    The Royal Ram

  • tonytony Moderator Administrator
    thats exactly the same as how you typed it originally
    but it didnt work thats why i had to change the top line
  • tonytony Moderator Administrator
    finally got it working i think :D
    atleast it doesnt come up with any errors before or after the submit, im just working on getting the password thingy to work now
  • PythonPython Forum Leader The Royal RAM
    ahh rite, ok, cool

    The Royal Ram

Sign In or Register to comment.