简体   繁体   中英

Send form to SQLdatabase ( 405 not allowed)

So i have this web page:

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/inputstyle.css">
  <title>Database Input</title>
</head>

<body>
    <h1> Database Input Page </h1>
    <p> Here you can input to the Database </p>
    <a href="View.html">View Database.</a>

    <form submit="submit.php" method="post">
        <p>First Name: <input type="text" name="firstname" /></p>
        <p>Surname: <input type="text" name="surname" /></p>
        <p>Age: <input type="text" name="age" /></p>
        <p><input type="submit" /></p>
    </form>
</body>

And this php script:

<?php

$username="root";
$password="password";
$database="posts";
$firstname=$_POST['firstname'];
$surname=$_POST['surname'];
$age=$_POST['age'];
mysql_connect(serverip,$username,$password);
@mysql_select_db($database) or die( "Unable to connect to Database");
$query = "INSERT INTO input  
VALUES('$firstname','$surname','$age')";mysql_query($query);mysql_close();

?>

In my mySQL database i have the database 'posts' and the table input . Withing the input table I only have 3 columns which in order are firstname , surname and age .

I need to get this working for a school project, but when I try and submit the form i get the error 405 Not Allowed . This is my first time using php and mySQL so im not sure if i've made any errors. Also all of my web files are located within /usr/share/nginx/html , although i do have a subfolder withing that for css.

Edit:

You have: ( submit )

<form submit="submit.php" method="post">
      ^^^^^^

change it to: ( action )

<form action="submit.php" method="post">

Original answer from OP's original question/code

POST variables are case-sensitive.

You have:

  • <input type="text" name="firstname" />
  • which is called using $firstname=$_POST['First Name'];

    that is wrong for two reasons.

    1. It's supposed to be $firstname=$_POST['firstname'];
    2. Your present variable contains as space.

You also have:

  • <input type="text" name="surname" />
  • is called using $surname=$_POST['Surname'];

which should be $surname=$_POST['surname'];

Then

  • <input type="text" name="age" />
  • which is called by $age=$_POST['Age']; but should be $age=$_POST['age'];

This line I'm unsure of mysql_connect(serverip,$username,$password); serverip is undefined.


Footnotes:

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/ .

Documentation for MySQL can be found at » http://dev.mysql.com/doc/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM