1

I'm pretty new to the whole PHP/HTML deal, and I've run into a problem that I don't know how to fix. It's a pretty simple form that lets you enter data into database. The PHP code is as following:

<?

    include("../sqlcontext.php");
    $foo = mysql_query("SELECT*FROM users WHERE checksum='".$_COOKIE['userCookie']."'");
    if($_COOKIE['userCookie'] == '' || !mysql_num_rows($foo)){
        echo 'Du er ikke logget ind :( <a href="login.php">log ind her</a>';
    }
    else{ 

    if($_POST['genreKnap']){
        $nameGenre = $_POST['nameGenre'];
        $textGenre = $_POST['textGenre'];       
        $succes = mysql_query("INSERT INTO genre VALUES('null','$nameGenre','$textGenre')");

        if($succes){
            echo 'Yay!';
        }else {
            echo 'Oh no';
        }
    } 

?>

The form is as following:

<form name="form1" method="post" enctype="text/plain" action="">
  <table>
    <tr>
        <td>Genre navn:</td>
        <td><input type="text" name="nameGenre" id="nameGenre" style="width:100%; padding-right: 1px" /></td>
    </tr>
    <tr>
        <td>Genre beskrivelse:</td>
        <td><textarea name="textGenre" id="textGenre" style="width:100%; padding-right: 1px"></textarea></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" name="genreKnap" id="genreKnap" value="Go!"/></td>
    </tr>
  </table>    
</form>

Whenever I press the submit button, it seems as though it acts as if it was a get method and not a post.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Amnestic
  • 650
  • 2
  • 8
  • 23
  • It's easy jus keep multiple of 4 spaces for tab, and spaces to manage that. :) – rptwsthi Feb 18 '13 at 15:34
  • Check this line `$succes = mysql_query("INSERT INTO genre VALUES('null','$nameGenre','$textGenre')");` – Basic Bridge Feb 18 '13 at 15:36
  • 1
    What do you mean it seems as though it acts as if it was a get method? – SeanWM Feb 18 '13 at 15:37
  • 2
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Feb 18 '13 at 15:39
  • What do you mean "It acts as if it was a get method" ?can you explain more what error you are getting, if there is any – samayo Feb 18 '13 at 15:42
  • Well, whenever I press the submit button, the URL changes as though i made a get request (i guess, I'm still new to this whole shebang). Anyway, for whatever reason the website is working now. No idea how it was fixed, but at least it is working now. – Amnestic Feb 18 '13 at 15:48
  • @Amnestic as Quentin mentioned above, although its not directly related to your problem at the moment you should think about using MySQLi or PDO. It will save you from worrying about sql injection in your mysql queries, I also find it much easier to work with too. – cosmicsafari Feb 18 '13 at 15:53

5 Answers5

1

Aha!!!

You are not posting the form correctly. Set the

action=""

to

action="code.php"

Assuming your php page is called code.php. Just change it to the name/path of the php page and the form will send the data to your php code to process.

When you leave action="" to blank, it posts the data to itself (the same page). It is not acting as GET, it is still acting as POST, but posting to the wrong place. I think you worded the title of the question wrong.


What do you mean it is acting like get instead of post. Can you not read $_POST variables in your PHP?

remove the 'enctype="text/plain"' in your form code.

Husman
  • 6,819
  • 9
  • 29
  • 47
  • My friend who is a more experienced PHP programmer put it in for the sake of debugging. I've taken it out now, but the page does still not react when I try to submit data. – Amnestic Feb 18 '13 at 15:45
  • Sorry didnt read your code correctly. Your form is not posting data to the correct php page. – Husman Feb 18 '13 at 15:51
0

enctype="text/plain"

Take that out. It is provided for debugging purposes only and doesn't generate anything that is sane to parse with a machine.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Valid form enctypes:

application/x-www-form-urlencoded: This is the default content type

multipart/form-data

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

Source: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.4

0

You're all ignoring the primary question and focusing on irrelevent items.

First of all more than anything he's using a short php opener <? not <?php now not every web server accepts short openers first up check that.

Echo out your $_POST vars and see if they're returning the correct items

echo "POSTS BELOW<br />";
echo $_POST['nameGenre']."<br />";
echo $_POST['textGenre']."<br />"; 
echo "<br />GETS BELOW<br />";
echo $_GET['nameGenre']."<br />";
echo $_GET['textGenre']."<br />"; 

Put this block of code directly below your php opener see what data it returns.

Also this if($_POST['genreKnap']){ is generally a bad way of doing it as its user input the safest way is a hidden field <input type="hidden" name="action" id="action" value="dopost" /> and change your if clause to if($_POST['action']=="dopost && isset($_POST['action'])){

Also set your form action="" to the actual page name not blank

Give all that a try and if its still not working we'll try something different

Dave
  • 3,280
  • 2
  • 22
  • 40
0

If you are send normal data without any files by the form . Then enctype is not always needed .

But even if you want to include it The correct way is :

enctype="multipart/form-data"

Also give a url in the action method of the form : <form action='example.php'>

I hope it solves the problem .

Tarun Kumar
  • 508
  • 3
  • 14