I have the following code that I want to run only when the "Submit" button is pressed...I can't work out what I need to amend to make it work?

 <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "job_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$Email = $_POST['email'];
$password = $_POST['password'];
$rpassword = $_POST['rpassword'];
$sql = "INSERT INTO register(name,password,rpaswword)
VALUES ('$name' , '$Email' , '$password' , '$rpassword')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>

This code looks like it does what you want. Maybe except that gaping security hole for SQL injection.

1 Like

Welcome, @ahmedebue95. I have formatted your code for you to make it easier for us to read. In the future, when you post code here you can format it yourself by placing three backticks(```) on the line before the code, and three backticks on the line after the code. Alternatively, you can highlight your pasted code and select the <\> icon from the top of the edit area of your post.

It might help if we could see your form.

This won’t help:

$sql = "INSERT INTO register(name,password,rpaswword)
VALUES ('$name' , '$Email' , '$password' , '$rpassword')";

You specify three columns, but give four values. And is that second password column name spelled correctly?

If that’s not it, tell us in what way it doesn’t work.

1 Like

I’m guessing it’s some kind of “Register an Account” form. Though what you are doing doesn’t seem to make sense.
There is no validation of any of the inputs. Usually when there is a password field and a repeat password field, the two will be compared to check that they match, then only one will be stored, as they should be the same, but only after hashing the password, never store raw, unhashed passwords. The sql injection risk has already been mentioned, that combined with unhashed passwords makes the system very insecure.

<div id="cd-login" class="is-selected">
                  <div class="page-login-form">
                    <form role="form" action="index.php" class="login-form" method="post">
                      <div class="form-group">
                        <div class="input-icon">
                          <i class="ti-user"></i>
                          <input type="text" id="sender-email" class="form-control" name="email" placeholder="Username">
                        </div>
                      </div> 
                      <div class="form-group">
                        <div class="input-icon">
                          <i class="ti-lock"></i>
                          <input type="text" class="form-control"  name="password" placeholder="Password">
                        </div>
                      </div> 
                      <input type="submit" name "Login" value="submit" class="btn btn-common log-btn" />Login
                      <div class="checkbox-item">
                        <div class="checkbox">
                          <label for="rememberme" class="rememberme">
                          <input name="rememberme" id="rememberme" value="forever" type="checkbox"> Remember Me</label>
                        </div>                        
                        <p class="cd-form-bottom-message"><a href="#0">Lost your password?</a></p>
                      </div> 
                    </form>
                  </div>
                </div>

                <!-- Register -->
                  <div id="cd-signup">
                  <div class="page-login-form register">
                    <form role="form"  action="index.php"   class="login-form" method="post">
                      <div class="form-group">
                        <div class="input-icon">
                          <i class="ti-user"></i>
                          <input type="text" id="sender-email" class="form-control" name="name" placeholder="Username">
                        </div>
                      </div> 
                      <div class="form-group">
                        <div class="input-icon">
                          <i class="ti-email"></i>
                          <input type="text" id="sender-email" class="form-control" name="email" placeholder="Email">
                        </div>
                      </div> 
                      <div class="form-group">
                        <div class="input-icon">
                          <i class="ti-lock"></i>
                          <input type="password" name="password" class="form-control" placeholder="Password">
                        </div>
                      </div> 
                      <div class="form-group">
                        <div class="input-icon">
                          <i class="ti-lock"></i>
                          <input type="password" name="rpassword" class="form-control" placeholder="Repeat Password">
                        </div>
                      </div>
                       <input type="submit" name "Register" value="submit" class="btn btn-common log-btn" />
                    </form>
					                  </div>
                </div>
                <div class="page-login-form" id="cd-reset-password"> <!-- reset password form -->
                  <p class="cd-form-message">Lost your password? Please enter your email address. You will receive a link to create a new password.</p>
                  <form class="cd-form">
                    <div class="form-group">
                      <div class="input-icon">
                        <i class="ti-email"></i>
                        <input type="text" id="sender-email" class="form-control" name="email" placeholder="Email">
                      </div>
                    </div> 
                    <p class="fieldset">
                      <button class="btn btn-common log-btn" type="submit">Reset password</button> 
                    </p>
                  </form>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.