Jump to content
Html

How do I create a database in phpmyadmin + Php form problem

Recommended Posts

Hello,

I have this code here, I watched a video on how to create a form for php posting. In phpmyadin, the database is called 'page', so, what happens after I post an email and password on my page, it goes to a 404 on the web host I'm using.

Quote

<? php

$con= mysqli_connect('IP for database'.'root','');

if (!$con)

{
echo 'Not connected To Server';
}

if(!mysqli_select_db($con, 'page'))
{
echo 'Database Not selected';
}
$Email = $Post('email'};
$Password = $Post('Password'};

$sql = "INSERT INTO Person(Email, password) ValueS ('$Email', '$password')";

if(!mysqli_query(!$con,sql))
{
echo 'Not inserted';
}
else
{
echo 'Not inserted';
}
header("refresh:1; url=formhtml.html");
?>

Quote

<body>
 <form action="/post.php" method="post">
   name:<br>
  <input type="text" value="username"><br>
  Password:<br>
  <input type="text" name="lastname" value="password"><br><br>
  <input type="submit" value="Submit">
</form> 
</body>

 

So the php file, and the web page above.

Here is screen of the phpmyadmin.

Thank you.

 

 

 

 

 

pagedb.jpg

Edited by Html

Share this post


Link to post
Share on other sites

Is your php file located at the root directory of the webroot?  Try opening the PHP file in a browser, and if it is indeed at the root, you should be able to access it at http://<your-domain>.com/post.php.  Otherwise, you have an incorrect path to post.php in your form.  If the form and the PHP file are in the same directory, you could just use

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

 

Share this post


Link to post
Share on other sites

These

Email = $Post('email'};
$Password = $Post('Password'}; 

are supposed to be $_POST['email']; AND $_POST['Password']; and the coloured names are suppose to match the values of name attributes of inputs you are trying to read

 

<input type="text" value="username"><br> Password:<br> <input type="text" name="lastname" value="password"><br><br>

AS you can see, they match neither, and one does not even have a name attribute which is required for ALL inputs, AND name values must match exactly as 'password' is treated differently to 'Password'.

 

  • Like 1

Share this post


Link to post
Share on other sites
<form method="post" action="/post.php"> -> this is wrong.
  <form method="post" action="post.php"> -> this is right ( if the post.php is in the same directory as the page you are calling on.
    <form method="post" action="../post.php"> -> goes with a directory up.

 

Share this post


Link to post
Share on other sites

Hello,

Thank you for the responses, tried the following edits. And the files are located in htdocs folder in the online file manager?

And no, form.php just goes to a 404.

Quote

<body>
 <form action="post.php" method="post">
   Email:<br>
  <input type="text" value="Email"><br>
  Password:<br>
  <input type="text" name="Password" value="password"><br><br>
  <input type="submit" value="Submit">
</form>
</body>

 

Quote

<? php

$con= mysqli_connect('service ip'.'root','');

if (!$con)

{
echo 'Not connected To Server';
}

if(!mysqli_select_db($con, 'page'))
{
echo 'Database Not selected';
}
$Email = $POST('email'};
$Password = $POST('Password'};

$sql = "INSERT INTO Person(Email, password) ValueS ('$Email', '$password')";

if(!mysqli_query(!$con,sql))
{
echo 'Not inserted';
}
else
{
echo 'Not inserted';
}
header("refresh:1; url=formhtml.html");
?>

 

Edited by Html

Share this post


Link to post
Share on other sites

What's the problem, you submit the form and get a 404?  If that's the case, then make sure the file with the form and the file that you're submitting to are in the same folder.

Share this post


Link to post
Share on other sites

What does that link point to?  Your code above says post.php, but the screenshot shows form.php.  Are you pointing to the right file?  And, if so, why would your server claim that a file isn't there if it is?  If that's the case then you probably need to ask your host why it's claiming that files which exist don't.

Share this post


Link to post
Share on other sites

Ah okay, I see. I did change that.

It doesn't redirect back to the login page, as well, there is no record in the mysql database.

Share this post


Link to post
Share on other sites

That custom 404 page doesn't give a lot of information, it doesn't help that it redirects to 404.html.  Maybe the redirection isn't working.  The normal way to redirect though is to send a Location header with the new URL.  But, for testing, I would suggest not redirecting and making sure the PHP code works.  Your PHP code has some errors in it, so maybe the server sees the PHP errors and redirects to 404 for some reason.

Here are some of the issues with your PHP code:

You're trying to call a function called $POST, but $POST isn't defined.  If you want to get submitted values you get them from $_POST, which is an array, not a function.  So, for example, you would use $_POST['name'].

If your database connection fails you don't stop the script, the rest of the code still runs without a database connection.

When you call mysqli_query, you used !$con which is going to send a boolean value instead of the actual connection.

You need to use prepared statements when you're sending data to the database.  The mysqli extension supports prepared statements, you need to use them to make sure your queries run and protect against SQL injection attacks.

You send the same text ('Not inserted') regardless of whether or not the record was inserted in the database.

If you use echo to send any output, sending a header after that will not work.

  • Like 1

Share this post


Link to post
Share on other sites

Hmm, well the example I got of this was from a tutorial on a video site. So I'm not knowledgeable in MySql, or php. I'm also not a computer programmer, so I'm not versed in the subject of web languages. I only ever got as far as html 4.01, that was ten years back. So therefore I returned here to get some answers for my problem. The clips jump from here to there, and so on. Its great, but it is all bit messy.

Okay, now I get a post.php, but no values in the database. So from what you have stated, about !$con is a problem. I doubt I'd have to be concerned about any scramble mysql code or so. I'm not going very far with this yet. Some sort of login page/comment form.

Thank you.

<? php

$con= mysqli_connect('mysql service db ip'.'root','');

if (!$con)

{
echo 'Not connected To Server';
}

if(!mysqli_select_db($con, 'page'))
{
echo 'Database Not selected';
}
$Email = $_POST('email'};
$Password = $_POST('Password'};

$sql = "INSERT INTO Person(Email, password) ValueS ('$Email', '$password')";

if(!mysqli_query(!$con,sql))
{
echo '';
}
else
{
echo '';
}
header("");
?>

 

 

Share this post


Link to post
Share on other sites

What happens when you run that code?  There are several syntax errors, are you seeing those error messages?  If not, they're probably going to an error log somewhere, maybe in the same directory as that file.  The first step is to figure out how to see the error messages, all of those options are configured in php.ini.

Share this post


Link to post
Share on other sites

There is no Php.ini file, where would be if not in the file manager?

Nothing occurs. Blank screen once I tested the email form. The code doesn't go back to the form page, I removed that, but the Mysql db should contain the record, but nothing.

  • Like 1

Share this post


Link to post
Share on other sites

formhtml.html

<!DOCTYPE html>
<html>
    <body>
        <form action="form.php" method="post">
            name:<br>
            <input type="text" name="email" value="Email"> <!-- value of 'name' attribute (which is required) will be used by $_POST['email']; to retrieve 'Email' value--><br>
            Password:<br>
            <input type="text" name="Password" value="password"><!-- value of 'name' attribute (which is required) will be used by $_POST['Password']; to retrieve 'password' value--><br><br>
            <input type="submit" value="Submit">
        </form>
    </body>
</html>

form.php

<?php // NO space between <? and php

$con = mysqli_connect('mysql service db ip' . 'root', '');

if (!$con) {
    echo 'Not connected To Server';
}

if (!mysqli_select_db($con, 'page')) {
    echo 'Database Not selected';
}
$Email = $_POST['email']; // changed to square braces, ['email'] should match same case used in the value of name="email" in form.
$Password = $_POST['Password']; // changed to square braces, ['Password'] should match same case used in the value of name="Password" in form.

$sql = "INSERT INTO Person(Email, password) VALUES ('$Email', '$Password')"; // ValueS (changed to 'VALUES') should all be the same case
lower or upper (preferably UPPERCASE), '$Email' and '$password' should match exactly the variable name that the specific $_POST[] were
assigned to, (NOTICE your code shows all lowercase for '$password' when the $variable name is '$Password' these are treated as different
variables).

if (!mysqli_query(!$con, sql)) {
    echo '';
} else {
    echo '';
}
header("");

 

Share this post


Link to post
Share on other sites
Quote

Not connected To ServerDatabase Not selected

This is now what I get from having typed in some values into the form, I did correct the php file. As I had stated, I had watched a tutorial clip on youtube a few months back now, that would be, and it showed not using brackets or the fact that there was a space between php and the question mark.

Share this post


Link to post
Share on other sites

That is nothing to do with form submitted data, that is to do with database connection, they are the warnings set out by

if (!$con) {
    echo 'Not connected To Server';
}

if (!mysqli_select_db($con, 'page')) {
    echo 'Database Not selected';
}

Share this post


Link to post
Share on other sites

Nothing occurs. Blank screen once I tested the email form.

The page is blank because, like I said, there are syntax errors.  If your file has syntax errors then nothing gets executed at all, PHP can't even figure out the structure of your code to even start running it.  The lack of error messages means that you either have error reporting turned off, or it's going to an error log.

There is no Php.ini file

Yes there is.  If you want to figure out which one your server is using, create a phpinfo page and look for the line that says "loaded configuration file" or something similar.  If PHP somehow got installed on that server with no php.ini file at all then it's going to use default values for every option.

<?php phpinfo();

Share this post


Link to post
Share on other sites

I tried that, the free host I am using has Php 7, dates back to May.

And this is load configuration /etc/php70/php.ini

I did watch a clip on youtube about this file, I was puzzled.:wacko:

Share this post


Link to post
Share on other sites

Check the settings for error reporting and logging to see what they're set to.  Depending on your host, you may be able to make changes to those settings in a .htaccess file or other file.  You should definitely try to understand the error reporting settings on that server so that you can find the actual error messages instead of trying to guess what the problems are, you just can't effectively debug problems if you can't find the error messages.

http://php.net/manual/en/configuration.php

Share this post


Link to post
Share on other sites

Where would I find that? I don't think the free host gives that access, a free host may restrict php edits, may be its for forum use only as there is a phpbb forum available to use therefore the mysql access.

Share this post


Link to post
Share on other sites

Right okay that brings up what I viewed before, error logs are on. What am I going to find to get some information on what is going on?

Share this post


Link to post
Share on other sites

The error_log option should tell you where the error log is.  According to this:

http://php.net/manual/en/ini.list.php

The error_log option is settable in any location, so you can create a .htaccess file (assuming your server supports those) to set the error log to a file in your directory.  Make sure that the server has permission to write to the file, and then you can find your error messages there.

Share this post


Link to post
Share on other sites

Hmm, I may be of been correct, you may be onto a fact, that there could be limitations for use with the free web host, perhaps full use of the php/mysql is for paid only.

I read about a htaccess file, It can be created as you stated,

AuthName "Member's Area Name"
AuthUserFile /path/to/password/file/.htpasswd
AuthType Basic
require valid-user
ErrorDocument 401 /error_pages/401.html
AddHandler server-parsed .html 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×