0

I have a HTML form that stores the information on a MySQL database. It works! But I want an only file that do all with PHP extension. I filled the database when clicking on the button but turn to deploy a PHP file, and I do not want that to happen.

Here is my HTML code:

<html>
  <head></head>
  <body>
    <form id="form" name="form" action="insert.php" method="POST">
      <label id="lbluser">Name:</label>
      <input type="text" name="name" id="name" /><br/>
      <label id="lbllastaname">Lastname:</label>
      <input type="text" name="lstname" id="lstname" /><br/>
      <label id="lblmail">E-mail:</label>
      <input type="text" name="email" id="email" /><br/>
      <label id="lblpassword">Password:</label>
      <input type="password" name="password" id="password" /><br/>
      <label id="lblpassword">Repeat password:</label>
      <input type="password" name="passwordr" id="passwordr" /><br/>
      <button type=”submit” name=”button” value=”insert”>OK</button>
    </form>
  </body>
</html>

And this is my PHP code:

<?php
$mysqli = mysqli_connect("localhost", "user", "12345", "own_bd");

 if (mysqli_connect_errno()) {
     printf("Problem with connection: %s\n", mysqli_connect_error());
     exit();
 } 
else {
 $var_name = mysqli_real_escape_string($mysqli, $_POST['name']);
 $var_lst = mysqli_real_escape_string($mysqli, $_POST['lstname']);
 $var_mail = mysqli_real_escape_string($mysqli, $_POST['email']);
 $var_pwd = mysqli_real_escape_string($mysqli, $_POST['password']);
 $var_pwdr = mysqli_real_escape_string($mysqli, $_POST['passwordr']);
 $sql = "INSERT INTO users_tbl (Name,Lastname,Mail,Pwd,PwdR) VALUES ('".$var_name."','".$var_lst."','".$var_mail."','".$var_pwd."','".$var_pwdr."')";
 $res = mysqli_query($mysqli, $sql);

 if ($res === TRUE) {
    echo "User added.";
 } 
 else {
 printf("Error: %s\n", mysqli_error($mysqli));
 }

 mysqli_close($mysqli);
}
?>

How I do it? I hope your help.

isset function doesn't work for me in this script, but if I try this code, it works!

<form action="" method="post">
    <input type="submit" value="Click" name="addImg" />
</form>
<?
    if (isset($_POST['addImg'])) {echo "haaallloooo";}
?>

Help!!!

3
  • 2
    ". I filled the database when clicking on the button but turn to deploy a PHP file, and I do not want that to happen." I don't quite understand what you mean, sorry Commented Jul 17, 2014 at 23:24
  • yes you can put the html on the same page as the php - if that was the question. Commented Jul 17, 2014 at 23:26
  • serakfalcon, I don't want separate PHP code from HTML code, I want to say is that I want to locate both codes in one only file, as Dagon said. Commented Jul 18, 2014 at 0:51

3 Answers 3

1

If i understood good what you are asking, the HTML and the PHP can be on the same page.

Take in mind that the better practice is to put the processnig code in the top of the file.

<?php
if(isset($_POST['button'])){
    $mysqli = mysqli_connect("localhost", "user", "12345", "own_bd");

    if (mysqli_connect_errno()) {
        printf("Problem with connection: %s\n", mysqli_connect_error());
        exit();
    } 
    else {
        $var_name = mysqli_real_escape_string($mysqli, $_POST['name']);
        $var_lst = mysqli_real_escape_string($mysqli, $_POST['lstname']);
        $var_mail = mysqli_real_escape_string($mysqli, $_POST['email']);
        $var_pwd = mysqli_real_escape_string($mysqli, $_POST['password']);
        $var_pwdr = mysqli_real_escape_string($mysqli, $_POST['passwordr']);
        $sql = "INSERT INTO users_tbl (Name,Lastname,Mail,Pwd,PwdR) VALUES ('".$var_name."','".$var_lst."','".$var_mail."','".$var_pwd."','".$var_pwdr."')";
        $res = mysqli_query($mysqli, $sql);

        if ($res === TRUE) {
            echo "User added.";
            exit();
        } 
        else {
            printf("Error: %s\n", mysqli_error($mysqli));
        }

    }
    mysqli_close($mysqli);
}
?>
<html>
  <head></head>
  <body>
    <form id="form" name="form" action="" method="POST">
      <label id="lbluser">Name:</label>
      <input type="text" name="name" id="name" /><br/>
      <label id="lbllastaname">Lastname:</label>
      <input type="text" name="lstname" id="lstname" /><br/>
      <label id="lblmail">E-mail:</label>
      <input type="text" name="email" id="email" /><br/>
      <label id="lblpassword">Password:</label>
      <input type="password" name="password" id="password" /><br/>
      <label id="lblpassword">Repeat password:</label>
      <input type="password" name="passwordr" id="passwordr" /><br/>
      <button type="submit" name="button" value="insert">OK</button>
    </form>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answer, Bob0t! But, doesn't work for me. I try your code, but don't execute PHP script :( Do I need to do some configuration?
If I try this code, it works! <form action="" method="post"> <input type="submit" value="Click" name="addImg" /> </form> <? if (isset($_POST['addImg'])) {echo "haaallloooo";} ?>
Make sure this is in the .php file.
I don't realy understand why this is not executing the PHP code... you can try also try with isset($_POST['name'], $_POST['lstname'], $_POST['email'], $_POST['password'], $_POST['passwordr'])
@JuanGD you may have 'smart quotes' arroun the button name attribute. Note how they look a little different to the other attribute quotes. Delete the quotes and replace them with normal quotes. type=”submit” name=”button” value=”insert” should look like type="submit" name="button" value="insert"
0

After i understand that you are looking for a HTML and PHP code both in the same file and Answer from Bob0t. The only thing which you might need to change here is isset($_POST['button']) to isset($_POST['BtnSubmit']) and change the <button type=”submit” name=”button” value=”insert”> to <button type=”submit” value=”BtnSubmit”>

<?php
if(isset($_POST['BtnSubmit'])){
    $mysqli = mysqli_connect("localhost", "user", "12345", "own_bd");

    if (mysqli_connect_errno()) {
        printf("Problem with connection: %s\n", mysqli_connect_error());
        exit();
    } 
    else {
        $var_name = mysqli_real_escape_string($mysqli, $_POST['name']);
        $var_lst = mysqli_real_escape_string($mysqli, $_POST['lstname']);
        $var_mail = mysqli_real_escape_string($mysqli, $_POST['email']);
        $var_pwd = mysqli_real_escape_string($mysqli, $_POST['password']);
        $var_pwdr = mysqli_real_escape_string($mysqli, $_POST['passwordr']);
        $sql = "INSERT INTO users_tbl (Name,Lastname,Mail,Pwd,PwdR) VALUES ('".$var_name."','".$var_lst."','".$var_mail."','".$var_pwd."','".$var_pwdr."')";
        $res = mysqli_query($mysqli, $sql);

        if ($res === TRUE) {
            echo "User added.";
            exit();
        } 
        else {
            printf("Error: %s\n", mysqli_error($mysqli));
        }

    }
    mysqli_close($mysqli);
}
?>
<html>
  <head></head>
  <body>
    <form id="form" name="form" action="" method="POST">
      <label id="lbluser">Name:</label>
      <input type="text" name="name" id="name" /><br/>
      <label id="lbllastaname">Lastname:</label>
      <input type="text" name="lstname" id="lstname" /><br/>
      <label id="lblmail">E-mail:</label>
      <input type="text" name="email" id="email" /><br/>
      <label id="lblpassword">Password:</label>
      <input type="password" name="password" id="password" /><br/>
      <label id="lblpassword">Repeat password:</label>
      <input type="password" name="passwordr" id="passwordr" /><br/>
      <button type=”submit” value=”BtnSubmit”>OK</button>
    </form>
  </body>
</html>

1 Comment

It was likely from the quotes type=”submit” value=”BtnSubmit”, wich should be normal double quotes " ^^
0
<?php
error_reporting(0);
echo <<<EOD

<html>
  <head></head>
  <body>
    <form id="form" name="form" action="" method="POST">
      <label id="lbluser">Name:</label>
      <input type="text" name="name" id="name" /><br/>
      <label id="lbllastaname">Lastname:</label>
      <input type="text" name="lstname" id="lstname" /><br/>
      <label id="lblmail">E-mail:</label>
      <input type="text" name="email" id="email" /><br/>
      <label id="lblpassword">Password:</label>
      <input type="password" name="password" id="password" /><br/>
      <label id="lblpassword">Repeat password:</label>
      <input type="password" name="passwordr" id="passwordr" /><br/>
      <button type="submit" name="button" value="insert">OK</button>
    </form>
  </body>
</html>

EOD;

$mysqli = mysqli_connect("localhost", "user", "12345", "own_bd");

 if (mysqli_connect_errno()) {
     printf("Problem with connection: %s\n", mysqli_connect_error());
     exit();
 } 
else {
 $var_name = mysqli_real_escape_string($mysqli, $_POST['name']);
 $var_lst = mysqli_real_escape_string($mysqli, $_POST['lstname']);
 $var_mail = mysqli_real_escape_string($mysqli, $_POST['email']);
 $var_pwd = mysqli_real_escape_string($mysqli, $_POST['password']);
 $var_pwdr = mysqli_real_escape_string($mysqli, $_POST['passwordr']);

if ((isset($_POST['name'])) && (isset($_POST['lstname'])) && (isset($_POST['email'])) && (isset($_POST['password'])) && (isset($_POST['passwordr'])))
 {
     $sql = "INSERT INTO users_tbl (Name,Lastname,Mail,Pwd,PwdR) VALUES ('".$var_name."','".$var_lst."','".$var_mail."','".$var_pwd."','".$var_pwdr."')";
     $res = mysqli_query($mysqli, $sql);

 if ($res === TRUE) {
    echo "User added.";
 } 
 else {
 printf("Error: %s\n", mysqli_error($mysqli));
 }
}
 mysqli_close($mysqli);
}
?>

That's both of them combined in 1 file, I hope that's what you were looking for.

2 Comments

Thanks for answer Luther West! But doesn't work for me! Every time I check the page, runs the PHP script adding empty rows to my table :(
@JuanGD I've fixed the error and tested it, it should work. By default, PHP and HTML files are separated because PHP is back-end while HTML is front-end. I personally find it odd that you want both of them in 1 file and thus I'd like to know what's the purpose of combining them maybe we can achieve that purpose in a better way. The code I've provided above does work but it's by no means a good design, IMHO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.