HTTP Error 405.0 - Method Not Allowed - php

I have a have rented a server at www.unoeuro.com (I dont know if this matters)
When i submit the form it gives me "HTTP Error 405.0 - Method Not Allowed"
<?php
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '')
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' .mysql_error());
}
$db_selected = mysql_slect_db(DB_NAME, $link);
if (!$db_selected) {
die('Could not connect: ' .mysql_error());
}
$value = $_POST['username'];
$sql = "INSERT INTO test (username) VALUES ('$value')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
<!doctype HTML>
<form action="test.php" method="post">
<p>Username: <input type="text" name="username" /><p>
<input type="submit" value="Submit" />
</form>
Can anyone tell me what i am doing wrong?
Just ask me for screenshots of anything you can use to help!
Sorry if the issue is obvious. This is my first time working with any kind of database, PHP and such.
Thanks!

First of all, if those are your actual database username and password, you're going to want to be changing those sharpish before someone else changes them for you :)
It's most likely down to how your server is configured to handle PHP files.
The first thing I would check is that the package you are leasing is designed to host PHP scripts. Looking at the UnoEuro package details (https://en.unoeuro.com/products.php#specs) it states 'ASP or PHP'. Is it possible that you are using a server setup for ASP?
If you are definitely on a PHP package, I would contact the service provider. They should know everything there is to know about configuring your server.
Also; there is a typo in your code 'mysql_slect_db'.

Related

Unable to connect to MySQL database but able to connect to database server

I am trying to build a dynamic website for connecting pages with database I used the code as follows. connection with server is Ok but unable to select database. data base name, user id, password, ip of host all gave but not working. please help....
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
else {
echo 'connected to server..................'; }
$db_selected = mysql_select_db($DB_NAME, $link);
if ($db_selected) {
print "Database Found";
}
else {
print "Database NOT Found";
}
Mysql has been deprecated and will eventually be removed. Consider using PDO or MySqli.
Here is a link to the PHP documentation page for making connections to a database using MySqli.
http://php.net/manual/en/mysqli.quickstart.connections.php
If you follow the instructions carefully you 'should' be able to connect. If not, perhaps you can post the error message you receive.
Try to give proper credential to connect to mysql server. mysqli_connect("localhost","root","password","db_name");

Why is my data not being submitted into my database?

I need to connect an html form to my sql database and I'm starting out with a simple form so I can understand how it works. I can't figure out what I am doing wrong. I have the form created, the php script, and the database created. Whenever I submit the form it takes me to a blank page and nothing has been added to my database. I've written error messages if the connections fail but I'm not seeing those either. I'm not as advanced in programming can someone please help me?
index1.html
<html>
<body>
<form action="info.php" method="post">
<p>Username:<input type="text" name="username" /></p>
<p>Email:<input type="text" name="email" /></p>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
info.php
<?php
define('DB_NAME' , 'users' );
define('DB_USER' , 'root');
define('DB_PASSWORD' , '');
define('DB_HOST' , 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
//connection to host
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//error if not connected to host
$db_selected = mysql_select_db(DB_NAME, $link);
//select the database
if(!$db_selected){
die('Can\'t use ' . DB_NAME . ':' . mysql_error());
}
echo 'Connected successfully';
$value = $_POST['username'];
$value2 = $_POST['email'];
$sql = "INSERT INTO guests (username, email) VALUES ('$value', '$value2')";
if(!mysql_query($sql)){
die('Error: ' . mysql_error());
}
//error check to see if connected to tables
mysql_close();
//close connection
?>
As I stated in comments; your mysql_ code checks out, so chances are you need to use either mysqli_ or PDO, since mysql_ functions may very well not be available for you to use.
If the following mysqli_ rewrite does not work for you, then the problem goes deeper and would be out of scope of the question.
Using error reporting will/should confirm that. Consult "Footnotes" on how to use it in your PHP file(s).
<?php
define('DB_NAME' , 'users' );
define('DB_USER' , 'root');
define('DB_PASSWORD' , '');
define('DB_HOST' , 'localhost');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
//connection to host
if (!$link) {
die('Could not connect: ' . mysqli_error($link));
}
echo 'Connected successfully';
$value = mysqli_real_escape_string($link,$_POST['username']);
$value2 = mysqli_real_escape_string($link,$_POST['email']);
$sql = "INSERT INTO guests (username, email) VALUES ('$value', '$value2')";
if(!mysqli_query($link, $sql)){
die('Error: ' . mysqli_error($link));
}
//error check to see if connected to tables
else{
echo "Data entered";
}
mysqli_close($link);
//close connection
?>
References:
MySQLi: http://php.net/manual/en/book.mysqli.php
PDO: http://php.net/manual/en/ref.pdo-mysql.php
Error reporting: http://php.net/manual/en/function.error-reporting.php
Footnotes:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Make sure your server is running and properly configured for PHP/MySQL/Apache.
Make sure your columns are indeed VARCHAR and long enough to accommodate the data.
Try this query:
INSERT INTO guest set username=$value1,email=$value2
hope it will solve .
Try this, it should work for you.
$sql = "INSERT INTO guests (username, email) VALUES (".$value.", ".$value2.")";

My form is submitting multiple database entries and I don't want it to

I have a simple html form and a php file to execute a database insertion. The problem I am having is that when I press the submit button, my database table receives 3 copies of the same submission and I only need one. Below is the code.
html:
<!DOCTYPE html>
<html>
<form action="demo.php" method="post">
<p>
Input 1: <input type="text" name="input1" />
<input type="submit" value="Submit" />
</p>
</form>
</html>
php:
<?php
define('DB_NAME', 'phpmyadminName');
define('DB_USER', 'phpmyadminUser');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$values')";
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
The DB_NAME, DB_USER, and DB_PASSWORD have all been changed for obvious reasons, but the code does work.
It just submits too many copies of the form data to the database table. Way back when I was in school, I had this issue, but it seemed like the problem was on the server's end and not our code. The server used here is mine and I do have full control over it. If the server is the issue at fault, I need help correcting that (as I am doing this to learn how to admin these tools, I do not know much more than basic level administration).
Kenneth, the code you have provided here honestly needs some work. First of all, please don't use the mysql API anymore. It's deprecated, will no longer be supported in future PHP versions, and is insecure. For all database operations use the mysqli or PDO API's, preferrably with prepared statements.
Secondly, do not ever INSERT $_POST or $_GET variables directly into the database without validating/sanitizing them first as someone could delete your data or even worse your whole database. PHP has numerous functions to make this very easy such as ctype depending on the data type.
Maybe try something like this in your code:
if (!empty($_POST['input1'])) { //checks if data was received//
$value = $_POST['input1'];
mysql_real_escape_string($value);
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
} else {
echo "form was not received";
exit;
}
I also noticed that your variable names were different, which is corrected above.
EDIT :
Mistakenly used wrong syntax for PHP ctype function.
You are taking the POST input value in the variable named $value and in query you are sending $values
I have corrected the code.
Can you please try the below code
<?php
define('DB_NAME', 'phpmyadminName');
define('DB_USER', 'phpmyadminUser');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value = $_POST['input1'];
if($value!=''){
$sql = "INSERT INTO demo (input1) VALUES ('".$value."')";
}
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
mysql_close();
?>
Below is correct code for the issue. I have checked that when you refresh your page it will create new blank entry in database and also the variable name is wrong.
You have to check for the Request method. This
$_SERVER['REQUEST_METHOD'] === 'POST'
will check the form method and it will prevent the blank entries in database.
<?php
define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', 'mysqldba');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
//Test for request method
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
//echo $sql;die;
if (!mysql_query($sql)){
die('Error: ' . mysql_error());
}
}
mysql_close();
?>

Issue with form

I'm new with mysql and php so please bear with me.
I'm trying to connect my first form to a table and I keep running into a new issue every time I "fix" something. I'm trying to test my form to make sure it connects before I move forward.
This is the form:
<form action="demo.php" method="post">
<p>input 1: <input type="text" name="input1"/></p>
<input type="submit" value="Submit" />
</form>
and this is my "demo.php" file:
<?php
define('BD_NAME', 'DEMO');
define('DB_USER', 'DEMO');
define('DB_PASSWORD', 'PASSWORD');
define('DB_HOST', 'HOST');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
echo 'Connected successfully';
mysql_close();
?>
I keep getting this error:
Can't use DB_NAME: Access denied for user 'USER'#'%' to database 'DB_NAME'
Again I'm new at this and I'd appreciate any help. Thanks!!
You have a typo.
define('BD_NAME', 'DEMO'); should be define('DB_NAME', 'DEMO');
Notice the DB_NAME global variable that is not resolve :
Can't use >>>>DB_NAME<<<<: Access denied for user 'USER'#'%' to database 'DB_NAME'
It means that either DB_NAME is not declared OR that the value of DB_NAME is "DB_NAME".
always use error_reporting(E_ALL) and display errors while coding to avoid these mistakes

iPage Connect PHP to my Database

I am new to programming. I have a website on iPage. Now, I am learning PHP and one of the things I am learning is to connect PHP to mySql database. I am using the following:
mysql_connect(host name, username, password)
My question is, why I am not getting an error?
no matter what username and password and even host name i enter, it just accepts it!
This is the code I am trying (the username and password are just as example)
<?php
mysql_connect('ipage','admin','password');
echo 'Connected!';
?>
when I run it, it just says connected even though my username and password are not admin, password.
Use mysqli_*, beacuse mysql_* is deprecated and will be removed in the future:
$conn = mysqli_connect('localhost', 'username', 'password');
if(mysqli_connect_errno($conn))
{
die('Error in connection to MySQL: ' . mysqli_connect_error());
}
else
{
echo 'Connected successfully';
}
You are not checking if you are connected.
You need to use something like this:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
My suggestion is to start reading some docs, they have some great examples and you can really learn a lot. DOCUMENTATION

Categories