Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

i have a web host, which has MySQL, and phpMyAdmin. The code below shows the PHP in my .php file. When i submit the form, the data does not store in the SQL database.

My database is a table called 'Logins'. It has three fields: 'userID' which is an autoincrement type,'email' which is a VARCHAR, and 'password' which is also a VARCHAR.

I have tested my connection, and it does work, meaning something in the code is wrong, but i can't find it. I would be grateful if you guys could help me.

This is my code:

<form action="index.php" method="post">
              <p>Email Address:</p>
              <input type="text" name="email" required autofocus pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" placeholder="Please enter your email">
              <p>Password:</p>
              <input type="text" name="password" required placeholder="Enter your password">
              <p>Confirm Password:</p>
              <input type="text" name="confirmpassword" required placeholder="Confirm your password">
              <br>
              <input class="button" type="submit" value="Register">
            </form> 


            <?php
                if($_POST['submit']=="Submit")
                {
                    $email = cleanData($_POST['email']);
                    $password = cleanData($_POST['password']);
                    $message = "wrong answer";
                    addData($email, $pasword);
                }

                function cleanData($data){
                    $data = trim($data);
                    $data = stripslashes($data);
                    $data = htmlspecialchars($data);
                    $data = strip_tags($data);
                    return $data;
                }

                function addData ($email, $password)
                {
                    //include("dbinfo.php");
                    $dbhost = 'mysql11.000webhost.com';
                       $dbuser = '************'; //censored for security
                       $dbpass = '******'; //censored for security
                       $conn = mysql_connect("$dbhost", "$dbuser", "$dbpass");

                       if(! $conn )
                       {
                          die('Could not connect: ' . mysql_error());
                       }
                    $sql="INSERT INTO Logins ('userID','email','password') VALUES (null, '$email', '$password')";
                    $result=mysql_query($sql) or die(mysql_error());
                }
            ?>
share|improve this question
    
Does the Logins table allow the userID field have a null value in it? – Russ yesterday
    
userID is the primary key, and i have set it to autoincrement, so each time a new record is added, it increase it by 1. Therefore, there is no need to specify a userID – donfromeway yesterday
    
Then don't specify it. Your code tries to insert a null value, instead of letting it default to the next autoincrement value. – Russ yesterday
    
Still doesn't work – donfromeway yesterday

3 Answers 3

change if($_POST['submit']=="Submit")

to if(!empty($_POST['email']) && !empty($_POST['password']))

share|improve this answer
    
Nope, that didn't work, thanks for trying though. – donfromeway yesterday
    
You have to remove quotes from INSERT INTO query, like @Tristan said, also make sure that userID attribute allows null or is AUTO_INCREMENT – user1365914 yesterday
    
i tried removing the quotes but i got a 'Parse error: syntax error, unexpected T_STRING' error – donfromeway yesterday
1  
While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run. – Bono 23 hours ago

you must replace the line :if($_POST['submit']=="Submit") By if(!empty($_POST['submit'])) Or by if($_POST['submit']=="Register")

Try to connect with wrong password and wrong login want to know if it display the message

share|improve this answer
    
Okay, i tried changing it on my test php file, and it echoed 'login successful' even though i entered wrong details, so now im thinking something could be wrong with my connection? – donfromeway 23 hours ago

The sql insert field names should not be in quotes and you mentioned that userID is autoincrementing so no need to include it unless you want to set it to a specific value.

$sql="INSERT INTO Logins (email, password) VALUES ('$email', '$password')";

You should also consider changing to mysqli since mysql is depreciated.

Edit: Your mysql_connect is missing the database select variable in the code you provided.

You will find debugging mysql queries much easier if you make use of the return values of each mysqli function. The below code checks for, and returns, any errors relating to database connection, query prepare, parameter binding, and execution.

This procedure makes it much easier to identify problems if they occur and with modification can be used to log errors, and report errors to the user even in production.

function addData ($email, $password)
{
    //include("dbinfo.php");
    $dbhost = 'mysql11.000webhost.com';
    $dbuser = '************'; //censored for security
    $dbpass = '******'; //censored for security
    $dbname = ''; // this is currently missing
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    /* check connection */
    if ($mysqli->connect_errno) {
        die("Connect failed: ".$mysqli->connect_error);
    }
    // define the query
    $sql="INSERT INTO Logins (email, password) VALUES (?, ?)";
    // prepare the query
    if (!$stmt = $mysqli->prepare($sql)) {
        // failed to prepare query;
        die("Prepare failed: ".$stmt->error);
    }
    // bind the parameters
    if (!$res = $stmt->bind_param('ss', $email, $password)) {
        // failed to bind
        die("Bind failed: ".$stmt->error);
    }
    // execute
    if (!$res = $stmt->execute()) {
        // failed to execute
        die("Execute failed: ".$stmt->error);
    }
    echo "Rows inserted: ".$stmt->affected_rows;
}
share|improve this answer
    
i tried removing the quotes but i got a 'Parse error: syntax error, unexpected T_STRING' error – donfromeway yesterday
    
Check your code. There is no T_STRING error in the $sql line I posted. – Tristan yesterday
    
Okay, i changed it, now theres no error, but the data is still not being stored in the SQL database – donfromeway yesterday
    
If you echo the $sql and copy paste into phpmyadmin does it work/error? – Tristan yesterday
    
I don't know how to do what you instructed, so i just added: 'echo $sql;' without the quotes to the next line, and that didn't work, and gave no error – donfromeway yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.