error connecting to mysql database through crazy domains - php

newbie here! I have made a simple form on my site
warning message
I can't seem to figure out what i've done wrong.
This is the php code:
<?php
$servername = "localhost";
$dbusername = "charityh_root";
$dbpassword = "";
$dbname = "charitydb";
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$state = $_POST['state'];
$email = $_POST['email'];
//$password = $_POST['password'];
//$sha1password = sha1($password);
//Create connection
$conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
//Check connection
If ($conn -> connect_error) {
die("Connection failed:" . $conn -> connect_error);
}
Line 17 is $conn = newmysqli($...
any idea guys?
Mysql database wizard
Connecting to database now although when i enter in the details (first name, last name, state and email) i get the message first name can not be left blank...
if (empty($fname)) {
Echo "First name can not be blank. Please press back and correct the
issue";
die();
}
if (empty($lname)) {
Echo "Last name can not be blank. Please press back and correct the issue!";
die();
}
if (empty($state)) {
Echo "State can not be blank. Please press back and correct the issue!";
die();
}
if (empty($email)) {
Echo "Email can not be blank. Please press back and correct the issue!";
die();
}
$sql = "INSERT INTO charityh_database (First_Name, Last_Name, State, Email)
VALUES ('$fname', '$lname', '$state', '$email')";
if ($conn->query($sql) === TRUE) {
echo "Thank You";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close ();
?>

$dbusername = "charityh_root";
$dbpassword = "";
Seems like you are using the same username and password as you were using in your local server. You need to replace them with the username and password crazy domain's.
Since as far as I know Crazy Domain deosn't allows a blank password.
EDIT
Besides that I can see the database name you have mentioned in the codes is "charitydb"; which should be something like charityh_database.(By looking at your recent edit this is a wild guess)

The error indicates your username and password do not have access to the database.
Have you checked the database will allow you to connect, using the username and password?

Related

PHP MySQL. Add table contents does not work

If I want to add content to the table using "INSERT INTO", I don't get an error message and the table is not filled. I'm new with PHP. explanations would be nice. The database runs on XAMPP.
I don't know what to try. I've already used another table, but it doesn't work. The user should have full access to the table. The names also match.
<?php
$username = $_POST["username"];
$passwort = $_POST["passwort"];
$mail = $_POST["mail"];
$passwort2 = $_POST["passwort2"];
$pass = sha1($passwort);
$db = mysqli_connect("localhost", "phptest1", "o84XM5wxo65QBjkF", "phptest1");
if($passwort == $passwort2) {
echo "Password is correct.";
$db = "INSERT INTO user (Username, Mail, Password) VALUES ('$username', '$mail', '$pass')";
} else if(!($passwort == $passwot2)) {
echo "Password is not correct";
} ?>
The variable $db actually contains information about the connection. You cannot insert a query into your database the way you are trying to
You can use $db (in your case) in order to check whether the connection has been correctly established or not and then if everything works correctly you can user mysqli_query() to inject the query into your database.
You can do it like so:
<?php
if(isset($_POST['submit'])){ //You have to check if your submit button is pressed
$username = $_POST["username"];
$passwort = $_POST["passwort"];
$mail = $_POST["mail"];
$passwort2 = $_POST["passwort2"];
$pass = sha1($passwort);
$db = mysqli_connect("localhost", "phptest1", "o84XM5wxo65QBjkF", "phptest1");
if(!$db){
die('Connection could not be established! Check provided information');
}
if($passwort == $passwort2) {
echo "Password is correct.Inserting query now";
$query = "INSERT INTO user (Username, Mail, Password) VALUES ('$username', '$mail', '$pass')";
$result = mysqli_query($db, $query); //keep $result for debugging purposes.
} else {
die("Password is not correct");
} //no need for else if as there are only 2 conditions.
if(!$result){ //check if query was successful.
die('Query Error');
}
echo "Query Updated successfully";
}
?>
This code is really simplistic and for testing purposes only.
I just wanted to show you the way you can send queries to your database. You better use other encryption techniques i.e. crypt() and of course functions like mysqli_real_escape_string() when retrieving data from users, in order to avoid potential injection attacks.
Check this post for more info about preventing injections.
Hope that helps.

PHP not inserting data into MySQLi database

My php has connected to my database however when I try to insert the data, I get the query is not okay response.
if(isset($_POST['Register'])) {
session_start();
$FName = $_POST['First_Name'];
$LName = $_POST['Last_Name'];
$Email = $_POST['Email'];
$PW = $_POST['Password'];
$query = "INSERT INTO user (Fname, Lname, Email, Password) VALUES('{$FName}', '{$LName}', '{$Email}', '{$PW}')";
$result=mysqli_query($con, $query);
if($result){
echo "Query is successfully executed";
}else{
echo "Query is not ok";
}
}
?>
This is my connection code but it tells me that I am successfully connected.
$con = mysqli_connect("localhost", "*******","*******");
mysqli_select_db('batman',$con);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
?>
The database matches all of the inputs and the table name is user so I cannot figure out why it will not insert into the database.
Use error_reporting(E_ALL); at startup
&
use simple $Fname instead {$Fname}
Also check input type="submit" and name=" is name Register"
Maybe you could try to make a class with the script, because I think the problem is the connection that you use , because you are not calling for the connection function when you execute the query.
Regards!

getting white screen after executing php code

Hi I am working on a simple php registration. But everytime I am submitting the registration page, i am getting a blank screen, no error, no display.
The code in my php file is :
<?php
if($_SERVER['REQUEST_METHOD']=="POST"){
$IP = //my hostname
$dbuser = "my user id";
$conn = new mysqli_connect($IP, $dbuser, "","my databse name");
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];
$address = $_POST['address'];
$query = "SELECT email FROM user where email='".$email."'";
$result = mysqli_query($conn,$query);
$numResults = mysqli_num_rows($result);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) // Validate email address
{
$message = "Invalid email address please type a valid email!!";
}
elseif($numResults>=1)
{
$message = $email." Email already exist!!";
}
else
{
mysqli_query("(insert into user(name,phone_number, email,pass1, pass2, address) values
('".$name."','".$phone."', '".$email."', '".$pass1."','".$pass2."','".$address."')");
echo $message = "Signup Sucessfully!!";
}
mysqli_close($conn);
}
print_r(error_get_last());
?>
there is no issue in establishing connection as i am using this connection method in other pages and they are working fine. Also i should specify that currently i am working on cloud 9 and my mysql database is on cloud9 itself.
please help me in undersstanding the trouble.
You say your line 42 has the insert query. I suppose you mean this query:
mysqli_query("(insert into user(name,phone_number, email,pass1, pass2, address) values
('".$name."','".$phone."', '".$email."', '".$pass1."','".$pass2."','".$address."')");
As your error says "2 parameters expected", you are missing here your connection parameter. You should have this:
mysqli_query($conn, "(insert into user(name,phone_number, email,pass1, pass2, address) values
('".$name."','".$phone."', '".$email."', '".$pass1."','".$pass2."','".$address."')");
Put this in on the first line of your script
ini_set('display_errors', '1');
This will then display the errors on the page and you will be able to see what is going wrong.
Also rather than the IP address try "localhost"
$conn = new mysqli_connect("localhost", $dbuser, "","my databse name");
If the database is on the same machine this may solve your problem.
Also your code is incredibly unsafe and will suffer from SQL injection. Please consider the following lines:
$email = $_POST['email'];
$query = "SELECT email FROM user where email='".$email."'";
Basically you put whatever the user is passing into your SQL statement. What if he enters "' OR 1; DROP TABLE user". You would lose all your data.
Please read about SQL injection and use PDO:
http://en.wikipedia.org/wiki/SQL_injection
http://php.net/manual/fr/book.pdo.php

Message on successful database entry

I'm trying to display a message upon a successful user registration however what I have doesn't seem to be working and just submits/refreshes the page. No message pops up even though data has successful been entered into the SQL database. Any tips or ideas?
<?php
require('php/connect.php');
if (isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "INSERT INTO `user` (username, password, email) VALUES ('$username', '$password', '$email')";
$result = mysql_query($query);
if($result){
echo $msg = "Successful Registration!";
}
}
?>
Learning PHP currently so sorry if there's a really obvious answer here!
Edit: Forgot to include echo. Just needed a second pair of eyes, sorry guys. Thanks for the tips!
First of all have this on consideration:
If you're getting started on PHP, please stop using mysql. It's deprecated, instead, you can use either PDO or MySQLi
As for your issue, your message is not being printed. Please make sure you echo the $msg variable:
$msg = "Successful Registration!";
echo $msg;
In mysqli a standard connection would be:
$DBConnect = new mysqli('serverName', 'userName', 'userPassword', 'dbName');
And that's it, that's all you need to start querying your database using mysqli.
For a mysql connection, try debugging it, see if there's a connection:
$connection = mysql_connect('localhost', 'userName', 'userPassword');
if (!$connection) {
die('Could not connect: ' . mysql_error());
}
Try to echo message
if($result)
{
echo $msg = "Successful Registration!";
}
<?php
echo "Successfully registered!";
?>

PHP register form not connecting to database

I have a simple user registration form and external connection script with some strange results.
The page register.php shows the form fine, however seems to display my entire connection string before the form?
It then throws up errors in relation to my connection variable '$dbcon' (I have commented the line at which this happens) Here is my register.php code:
<?php
session_start();
require "connect.php";
if (isset($_SESSION['username'])){
header("location: members.php");
}
if (isset($_POST['submit']))
{
$user = $_POST['user'];
$pass = $_POST['pass'];
$rpass = $_POST['rpass'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
if ($user == "" || $pass == "" || $rpass == "")
{
echo "Please fill all fields";
}
else
{
if ($pass != $rpass)
{
echo "Passwords do not match";
}
else
{
//This is where the errors are found
$query = mysqli_query($dbcon, "SELECT * FROM users WHERE username = '$user' ") or die ("Cannot query table");
$row = mysqli_num_rows($query);
if($row == 1)
{
echo "This username is already taken";
}
else
{
$add = mysqli_query($dbcon, "INSERT INTO users (id, firstname, lastname, username, password, admin) VALUES
(null, '$fname', '$lname', '$user', '$pass', '$admin') ") or die ("Cant insert data");
echo "Successfully added user!";
}
}
}
}
?>
And here is my connection file 'connect.php' (the $dbcon string is the one that prints out??)
$server = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'bodgett';
$dbcon = mysqli_connect($server, $user, $pass, $dbname)or die("Can not connect to Server.");
Specifically, the error is 'Notice: Undefined variable: dbcon in C:\webserver...\register2.php'
Can anyone suggest why is doesn't recognize this variable?
Probably a wrong filename (maybe file isn't called connect.php) OR wrong file extension? (html instead of .php)
I just copied all your code, and it works for me. Aswell I don't see php start and closing Tags.
I agree with #Xatenev. Also, you may want to consider using PDO for your database interactions, it's the most secure way. I found this very helpful: http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
Sorry if this seems irrelevant, just trying to help.
The connection file 'connect.php' is not enclosed within tags, hence not usable and explains why the text was simply printing out at the top of the page.
Check if mysqli extension is enabled
the code that generates $dbcon is inside a class or inside some function?
If yes, maybe you need to return or call it properly.

Categories