$servername = "host";
$dBUsername = "";
$dBPassword = "";
$dBName = "";
$conn = mysqli_connect($servername,$dBUsername,$dBPassword,$dBName);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
}
line 8 is the "$conn = mysqli_connect($servername,$dBUsername,$dBPassword,$dBName);" error code = PHP Fatal error: Call to undefined function mysqli_connect()
Please post you code either inside the code tags or using three backward tick marks at the beginning and end. Like this:
<?php
$servername = "host";
$dBUsername = "wolves_wolves";
$dBPassword = "Bugsbunny15!";
$dBName = "wolves_rocking";
$conn = mysqli_connect($servername,$dBUsername,$dBPassword,$dBName);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
} line 8 is the "$conn = mysqli_connect($servername,$dBUsername,$dBPassword,$dBName);" error code = PHP Fatal error: Call to undefined function mysqli_connect()
My guess is that you are using Wampp or Xampp or other localhost system. Therefore you need to point it at localhost not host. Also, never ever post your passwords or user ID’s on ANY website. Since you are using a local server, you can change now. Hope that helps!
i do apaulogise, this is actually a site im trying to build and it has a login system and the system is built on cpanel, i know css and html but i am new to php and i am trying to learn, this code was working yesterday and i accidently deleted the database but i remade it on the c-panel again. when the user signs up the sign up form rthen delivers to the database. I am unsure of what happened.
i do not know if it will help or not but here is the link to the site I am trying to make.
http://rockingwolvesradio.com/wolves_rocking/signup.php
when i click the sign up form submit it goes to a page and gives the error code 500 also.
and here is the sign up form code
<h2>Sign Up</h2>
<div class="signup-form-form">
<form action="includes/signup.inc.php" method="post">
<input type="text" name="name" placeholder="Full name...">
<input type="text" name="email" placeholder="Email...">
<input type="text" name="uid" placeholder="Username...">
<input type="password" name="pwd" placeholder="Password...">
<input type="password" name="pwdrepeat" placeholder="Repeat password...">
<button type="submit" name="submit">Sign up</button>
</form>
</div>
<?php
// Error messages
if (isset($_GET["error"])) {
if ($_GET["error"] == "emptyinput") {
echo "<p>Fill in all fields!</p>";
}
else if ($_GET["error"] == "invaliduid") {
echo "<p>Choose a proper username!</p>";
}
else if ($_GET["error"] == "invalidemail") {
echo "<p>Choose a proper email!</p>";
}
else if ($_GET["error"] == "passwordsdontmatch") {
echo "<p>Passwords doesn't match!</p>";
}
else if ($_GET["error"] == "stmtfailed") {
echo "<p>Something went wrong!</p>";
}
else if ($_GET["error"] == "usernametaken") {
echo "<p>Username already taken!</p>";
}
else if ($_GET["error"] == "none") {
echo "<p>You have signed up!</p>";
}
}
?>
</section>
```if (isset($_POST["submit"])) {
// First we get the form data from the URL
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["uid"];
$pwd = $_POST["pwd"];
$pwdRepeat = $_POST["pwdrepeat"];
require_once "dbh.inc.php";
require_once 'functions.inc.php';
if (emptyInputSignup($name, $email, $username, $pwd, $pwdRepeat) !== false) {
header("location: ../signup.php?error=emptyinput");
exit();
}
if (invalidUid($uid) !== false) {
header("location: ../signup.php?error=invaliduid");
exit();
}
if (invalidEmail($email) !== false) {
header("location: ../signup.php?error=invalidemail");
exit();
}
if (pwdMatch($pwd, $pwdRepeat) !== false) {
header("location: ../signup.php?error=passwordsdontmatch");
exit();
}
// Is the username taken already
if (uidExists($conn, $username) !== false) {
header("location: ../signup.php?error=usernametaken");
exit();
}
createUser($conn, $name, $email, $username, $pwd);
} else {
header("location: ../signup.php");
exit();
}
The Functions
```<?php
function emptyInputSignup($name, $email, $username, $pwd, $pwdRepeat) {
$result;
if (empty($name) || empty($email) || empty($username) || empty($pwd) || empty($pwdRepeat)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function invalidUid($username) {
$result;
if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function invalidEmail($email) {
$result;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function pwdMatch($pwd, $pwdrepeat) {
$result;
if ($pwd !== $pwdrepeat) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function uidExists($conn, $username) {
$sql = "SELECT * FROM users WHERE usersUid = ? OR usersEmail = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../signup.php?error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "ss", $username, $username);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($resultData)) {
return $row;
}
else {
$result = false;
return $result;
}
mysqli_stmt_close($stmt);
}
function createUser($conn, $name, $email, $username, $pwd) {
$sql = "INSERT INTO users (usersName, usersEmail, usersUid, usersPwd) VALUES (?, ?, ?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../signup.php?error=stmtfailed");
exit();
}
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssss", $name, $email, $username, $hashedPwd);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($conn);
header("location: ../signup.php?error=none");
exit();
}
function emptyInputLogin($username, $pwd) {
$result;
if (empty($username) || empty($pwd)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
function loginUser($conn, $username, $pwd) {
$uidExists = uidExists($conn, $username);
if ($uidExists === false) {
header("location: ../login.php?error=wronglogin");
exit();
}
$pwdHashed = $uidExists["usersPwd"];
$checkPwd = password_verify($pwd, $pwdHashed);
if ($checkPwd === false) {
header("location: ../login.php?error=wronglogin");
exit();
}
elseif ($checkPwd === true) {
session_start();
$_SESSION["userid"] = $uidExists["usersId"];
$_SESSION["useruid"] = $uidExists["usersUid"];
header("location: ../index.php?error=none");
exit();
}
}
Other than the about button being dead, the site seems to work. Looking at the HTML code it looks basic. It does have a script error on the JS code.
Well, almost all remote sites have a place in the control panel called configuration. In there it shows you how you need to connect to your database. Normally it would not be just 'host". At least in my experience. I suggest you check your control panel to start or look in the help page on the control panel.
i updated my previous post with the other coding going with it, here is the javascript code
let getNav = document.querySelectorAll("nav div ul li a");
for (let i = 0; i < getNav.length; i++) {
// Get URL info
let pageUrlName = pageUrl.split("/");
let pageUrlLength = pageUrlName.length - 1;
// Get links info
let pageNav = getNav[i].pathname;
let pageNavName = pageNav.split("/");
let pageNavLength = pageNavName.length - 1;
let pageFinalName = pageNavName[pageNavLength];
let pageNavExists = pageUrl.includes(pageFinalName);
// Change links color
if (pageNavExists == true) {
getNav[i].style.cssText = "color: #31a6ff;";
}
else if (pageUrlName[pageUrlLength] == "") {
getNav[0].style.cssText = "color: #31a6ff;";
}
}
Well, this line makes no sense. I guess I am confused on what all this is. You set up a uidExists() function which doesn’t work as written. Then, right after that, you do the same thing again not in a function. Also, if you are posting to this code, you check your code to see if the submit button is pressed, you should do it this way:
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST["submit"]) {
// handle the submit button here...
} elseif (isset($_POST["some-other-button"]} {
// Handle another button
}
}
The first line checks if the server has posted to itself. If you just check for the posted button, it does not protect you from some programmer using a cURL or other type of post to your site. They could post data to your site without being on your site. The first line helps protect that loophole.
Lastly, three slashes are NOT three back-tick’s. A backward tick mark it a backwards single quote which on most keyboards are in the upper left of the keyboard. Type three of these, enter, your code, however many lines, then three more of them. Or you can use the Quote tags…
I would have to strongly disagree with this. A simple view source will show you all the fields that will be submitted.
True. This is where CSRF Protection with a NONCE comes in.
Well, the server array is not available outside the server as far as I know. So, it protects the post somewhat.
Perhaps you need to explain further. But, it will be over his head as he is a beginner and has stated so.
Ya I honestly did not understand that, but I have remade the data base and re entered the code and I am still getting the error code. It is just kinda weird how overnight it just stops working. I had made a mistake in the database before and I deleted the database and remade it within the copanel. All of the tables within the database are the same as when it was working tho. I just don’t know what happened. Or if I am missing something small and it is going over my head.
Well, to force all errors to show, you can add this to the top of your page. ( On the page that is failing at the top of the page before anything else. )
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
This might show further errors that are not showing up. But, you can also check your server logs for real errors. The way you do that is log into your control panel, there will be a server-logs area and you can look at them. Clear them all and then run your bad page and review them again. If the error is still in line 8 I am sure it is the HOSTNAME. There is no servers that use “host” as the name. It is normally either “localhost” or the real name of the server and you find that in the control panel under the configuration section. You might want to just create a simple page that connects to the server and runs one simple query to make it easier for testing…
How does it protect anything? Protect from what?
$_SERVER['REQUEST_METHOD']
contains the request method (no surprise).
( Let’s you check if it was the correct method that the page requires and can verify it is from a valid caller.)
$_POST
contains any post data.
(Data that is sent from a form or remote call.)
It’s possible for a POST request to contain no POST data.
So, you check if a post request came in and then check the posted data for the button and fields that posted it. What is wrong with this?
Ok, we are not far off. Everything you stated is correct except the last part.
The thing is, in a properly coded form, every field with a name, save for unchecked checkboxes, will be isset so checking that any are isset is pointless and redundant since they ALL will be isset whether anything was entered in the field of not.
Yes, and this is why, after trimming the entire POST array, you then check for empty, not isset, on required fields.
I disagree. I use the posting method to indicate a post was made. Then, in some cases, I check IP addresses and then I check ALL of the posted buttons and handle just one of them at a time. If the user presses an insert type of button, they do not need update data handled. Therefore, you handle it like this…
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST["submit"]) {
// handle the submit button here...
} elseif (isset($_POST["some-other-button"]} {
// Handle another button
} elseif (isset($_POST["third-button"]} {
// Handle third button...
}
}
In this manner ONLY one button is handled ever. It does drop down thru all the buttons but handles only one. Some new users check each button separately and there is a lot of extra server processing for every button that exists. This version process less IF’s in the long run. And, if you order them to the most used first, then overall the server is faster, especially when it is in high-use mode with lots of users.
Well, that is my humble opinion…
@ErnieAlex , You posted code for multiple forms (buttons) on one page. The link OP posted doesn’t work so I don’t know if he had multiple forms on a page which in THAT case, then you would do like you posted save for the submit button check which can completely fail in certain cases. We may not have been on the same page about this. I didnt really notice this was about multiple “buttons”.
Based on the OP’s code for a “signup” page it doesn’t look like there are multiple “buttons” involved.