1

Hey there sorry if this question may have been asked too much but none of the questions I saw on the website helps me, And as the title pointed out I have an insert query that would create an account for a website I am making but when I press the submit button It would do nothing ... Just refresh the page.

NOTE: database included. This part of the website is included inside signup.php and in signup.php the connect file is included. And after testing I could fetch from connect.php so I do not think that that is the problem. Even though That does not justify why the code would not always run.

After troubleshooting and testing I figured out that by removing this part of the code :

        if (
        $username_error_message_status = false
        and $password_error_message_status = false
        and $email_error_message_status = false
        and $bot_check_error_message_status = false
    ) {

The code works yet the insert query statement do not. So any kind of help would be appreciated and thanks for reading.

Full code :

    <?php
    $sign_up_button = isset($_POST["sign_up_button"]);

    $username_error_message = $password_error_message = $email_error_message = $bot_check_error_message = "";
    $username_error_message_status = $password_error_message_status = $email_error_message_status = $bot_check_error_message_status = "";
    $username_field = $email_field = "";

    if ($sign_up_button) {

        $username_field = $_POST["sign_up_username"];
        $password_field = $_POST["sign_up_password"];
        $email_field = $_POST["sign_up_email"];
        $bot_check_field = isset($_POST["sign_up_bot_check"]);

        if ($_SERVER["REQUEST_METHOD"] == "POST") {

            if (empty($username_field)) {
                $username_error_message_status = true;
                $username_error_message = "This field is required.";
            } 
            else {
                $username_error_message_status = false;
            }

            if (empty($password_field)) {
                $password_error_message_status = true;
                $password_error_message = "This field is required.";
            } 
            else {
                $password_error_message_status = false;
            }

            if (empty($email_field)) {
                $email_error_message_status = true;
                $email_error_message    = "This field is required.";
            } 
            else {
                $email_error_message_status = false;
            }       

            if ($bot_check_field < 100) {
                $bot_check_error_message_status = true;
                $bot_check_error_message    = "Please slide the slider to the end.";
            } 
            else {
                $bot_check_error_message_status = false;
            }           

        }
        if (
            $username_error_message_status = false
            and $password_error_message_status = false
            and $email_error_message_status = false
            and $bot_check_error_message_status = false
        ) {
            /* --- --- --- --- --- --- --- --- Check username availability --- --- --- --- --- --- --- --- */

            $check_username_query = "SELECT `Username` FROM `users` WHERE `Username` = '$username_field' ";

            $check_username_query_result = mysqli_query($GLOBALS["___mysqli_ston"], $check_username_query);
            $numrows_check_username_query_result = mysqli_num_rows($check_username_query_result);
            if ($numrows_check_username_query_result == 1) {
                $username_availability_status = false;
            ?>
                <p><span class = "sign-up-error-tag">That username already exists in our database. Please choose another.</span></p>
            <?php
            }
            else {
                $username_availability_status = true;
            }

            /* --- --- --- --- --- --- --- --- Check email availability --- --- --- --- --- --- --- --- */

            $check_email_query = "SELECT `Email` FROM `users` WHERE `Email` = '$email_field' ";

            $check_email_query_result = mysqli_query($GLOBALS["___mysqli_ston"], $check_email_query);
            $numrows_check_email_query_result = mysqli_num_rows($check_email_query_result);
            if ($numrows_check_email_query_result == 1) {
                $email_availability_status = false;
            ?>
                <p><span class = "sign-up-error-tag">That email already exists in our database. Please choose another.</span></p>
            <?php
            }
            else {
                $email_availability_status = true;
            }

            /* --- --- --- --- --- --- --- --- Check email validity --- --- --- --- --- --- --- --- */

            if (filter_var($email_field, FILTER_VALIDATE_EMAIL)) {
                $email_validity_status = true;
            }
            else {
                $email_validity_status = false;
            ?>
                <span class = "sign-up-error-tag">That email is invalid. Please choose another.</span>
            <?php
            }

            /* --- --- --- --- --- --- --- --- Hashing password --- --- --- --- --- --- --- --- */

            function better_crypt($input, $rounds = 7) {
            $salt = "";
            $salt_chars = array_merge(range('A','Z'), range('a','z'), range(0,9));
            for($i=0; $i < 22; $i++) {
            $salt .= $salt_chars[array_rand($salt_chars)];
            }
            return crypt($input, sprintf('$2a$%02d$', $rounds) . $salt);
            }
            $password_field_hash = better_crypt($password_field);
            $password_field_hash = better_crypt($password_field, 10);
            $password_field_hash = better_crypt($password_field, 15);       

            /* --- --- --- --- --- --- --- --- Create account in database --- --- --- --- --- --- --- --- */

            $sign_up_date = date("Y-m-d h:i:sa");
            $sign_up_code = rand();
            $sign_up_active_status = "0";

            $create_account_query = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO `users`(`Username`, `Email`, `Password`, `RegisterCode`, `Date`, `ActiveStatus`) VALUES ('$username_field', '$email_field', '$password_field_hash', '$sign_up_code', '$sign_up_date', '$sign_up_active_status')"); 

            /* --- --- --- --- --- --- --- --- Send activation email --- --- --- --- --- --- --- --- */

            $email_to = $email_field;
            $WebsiteName = "ForTheHodor";
            $email_message = "
            <table width='100%' border='0' cellspacing='0' cellpadding='3'>
               <tr>
                <td colspan=2><b>".$WebsiteName."</b></td>
              </tr>
               <tr>
                <td colspan=2>Thank you for creating an account.</td>
              </tr>
                </table>
                <hr><h3>Account info</h3><hr>
                <table width='100%' border='0' cellspacing='0' cellpadding='3'>
              <tr>
                <td width='28%' >Username : </td>
                <td width='72%'>".$username_field."</td>
              </tr>
              <tr>
                <td>Email : </td>
                <td>".$email_field."</td>
              </tr>
            </table>
            <p>".$WebsiteName." advise you not to delete this email for it contains informations that could be in use later.</p>
                <hr><h3>Account activation</h3><hr>
                <p>If you wish to activate your account please use the following link : </p>
                <p><a href = 'localhost/SignalShare/activate.php' style = 'text-decoration: none; color: blue;' >Activate account</a></p>
                <p>Activiation code :</p>
                <p>".$sign_up_code."</p>
                <hr> 
                <p>If this email does not concern you please ignore it.</p>
                <p>All regards.</p>
                <p>".$WebsiteName."</p>
                ";
            $email_subject = "Accout activation";                               
            $email_headers  = "From:".$WebsiteName."\r\n";
            $email_headers .= "Content-type: text/html\r\n"; 

            mail($email_to, $email_subject, $email_message, $email_headers);    

            echo $email_message;    

        }

    }
?>
<div id = "SignupForm">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Sign up</h3>
        </div>
        <div class="panel-body">
            <span>
                <form method = "POST" action= "signup.php">
                    <table class = "table table-borderless">
                        <thead>
                            <tr>
                                <th></th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>                 
                            <tr>
                                <td>
                                    <span class = "Sign-up-tag">Username</span>
                                </td>
                                <td>
                                    <input type = "text" name = "sign_up_username" class = "sign-up-field" value = "<?php echo $username_field; ?>"></input>
                                </td>
                            </tr>
                            <tr>
                                <td>

                                </td>
                                <td>
                                    <span class = "sign-up-error-tag"><b><?php echo $username_error_message; ?></b></span>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <span class = "Sign-up-tag">Password</span>
                                </td>
                                <td>
                                    <input type = "password" name = "sign_up_password" class = "sign-up-field"></input> 
                                </td>
                            </tr>
                            <tr>
                                <td>

                                </td>
                                <td>
                                    <span class = "sign-up-error-tag"><b><?php echo $password_error_message; ?></b></span>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <span class= "Sign-up-tag">Email</span>
                                </td>
                                <td>
                                    <input type = "text" name = "sign_up_email" class = "sign-up-field" value = "<?php echo $email_field; ?>"></input>
                                </td>
                            </tr>
                            <tr>
                                <td>

                                </td>
                                <td>
                                    <span class = "sign-up-error-tag"><b><?php echo $email_error_message; ?></b></span>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <span class= "Sign-up-tag">Bot check</span>
                                </td>
                                <td>
                                    <input id= "defaultSlider" type="range" min="0" max="100" value = "0" name = "sign_up_bot_check"/>
                                </td>
                            </tr>
                            <tr>
                                <td>

                                </td>
                                <td>
                                    <span class = "sign-up-error-tag"><b><?php echo $bot_check_error_message; ?></b></span>
                                </td>
                            </tr>
                            <tr>
                                <td>

                                </td>
                                <td>
                                    <input type = "submit" name = "sign_up_button" class="btn btn-success" value = "Sign up"></input>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form>             
            </span>
        </div>
    </div>
</div>

Table shema :

    -- phpMyAdmin SQL Dump
-- version 4.5.5.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 12, 2016 at 07:59 AM
-- Server version: 5.7.11
-- PHP Version: 5.6.19

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `forthehodor`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `ID` int(11) NOT NULL,
  `Username` varchar(32) NOT NULL,
  `Email` varchar(45) NOT NULL,
  `Password` varchar(32) NOT NULL,
  `RegisterCode` varchar(50) NOT NULL,
  `Date` varchar(50) NOT NULL,
  `ActiveStatus` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`ID`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
11
  • share your table(users) schema also ? Commented Oct 12, 2016 at 7:58
  • Edited .. Maybe add connect.php as well if it helps ? @Rishi Commented Oct 12, 2016 at 8:01
  • 1
    Check if you get an error from the mysqli query. This line var_dump(mysqli_error($GLOBALS["___mysqli_ston"])); should show the last error of mysqli. Put this after your insert. Commented Oct 12, 2016 at 8:08
  • 1
    I see 32 char in your table schema. Commented Oct 12, 2016 at 8:11
  • 1
    I recommend you echo the insert string and try to execute it manually via phpMyAdmin. Then you can check there where the problem could occur from. Please post the result too. Commented Oct 12, 2016 at 8:13

1 Answer 1

1

Your remove if query should be as follows:

if (
    $username_error_message_status == false
    and $password_error_message_status == false
    and $email_error_message_status == false
    and $bot_check_error_message_status == false
) {

You need to use double equals (==) not a single (=) in an if statement if you want to compare values.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer .. in my old codes I used to put single ( = ) but for some reason even though the code runs now the data are not inserted ... But I guess that is one part done.

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.