News:

Wondering if this will always be free?  See why free is better.

Main Menu

mysqli_query issues

Started by snoogans775, April 14, 2016, 04:27:24 AM

Previous topic - Next topic

snoogans775

Hi there! So far I really love SMF. They beat out my previous forum software handily :)

I am having an issue with SQL access. My server, user, pass, and database have been checked many times. I am still not getting my INSERT to go through. I am trying to send some values into smf_member when my users create a new account on a separate database. I have isolated this code and run it without the other $connection but it is still failing.


I am running the following PHP:


Quote$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
      // tests connection for errors
      if(mysqli_connect_errno()) {
         die("Database connection failed: ".
            mysqli_connect_error() . " (" . mysqli_connect_errno() . ")");
      }

      $username = mysqli_real_escape_string($connection, $_POST["username"]);
      $password = mysqli_real_escape_string($connection, $_POST["password"]);
      $hashed_password = sha1(strtolower($username) . $password);

      //performs database query
      $query  = "INSERT INTO smf_members (";
      $query .= "member_name, real_name, passwd";
      $query .= ") VALUES (";
      $query .= "'{$username}', '{$username}', '{$hashed_password}'";
      $query .= ")";
      $result = mysqli_query($connection, $query);

      if ($result) {
         //Success
         $_SESSION["message"] = " And forum account created!";
      } else {
         //Failure
         $_SESSION["message"] = " Forum registration failed.". DB_SERVER. DB_USER.DB_PASS. DB_NAME. " "
            . $query;
      }
   }
} else {
// END OF if (isset($_POST["submit"])) {
}
?>

This code reaches " Forum Registration failed". It never dies at the initial connection.

I originally checked the result with mysqli_affected_rows. I removed that for debugging.

If anyone sees a glaring problem here I'd really appreciate the help.

Night09

Is that the entire page of code or a part of it? If its the entire code it has brackets wrong near the bottom from what I see. Theres two too many closing. }




Shambles

The code works fine - make sure your DB credentials are correct.

I just created a simpleportal block and successfully created an account using your code


// connection details removed
if(mysqli_connect_errno()) {
         echo "Database connection failed: ".
            mysqli_connect_error() . " (" . mysqli_connect_errno() . ")";
      } else {
    echo 'connected ok'; }
$username = mysqli_real_escape_string($connection, 'dummy');
      $password = mysqli_real_escape_string($connection, 'dummypw');
      $hashed_password = sha1(strtolower($username) . $password);

      //performs database query
      $query  = "INSERT INTO smf_members (";
      $query .= "member_name, real_name, passwd";
      $query .= ") VALUES (";
      $query .= "'{$username}', '{$username}', '{$hashed_password}'";
      $query .= ")";
      $result = mysqli_query($connection, $query);

      if ($result) {
         //Success
         echo " And forum account created!";
      } else {
         //Failure
         echo " Forum registration failed.". DB_SERVER. DB_USER.DB_PASS. DB_NAME. " "
            . $query;
      }

Advertisement: