What is the mistake in this PHP code? - php

I have a user registration system but its empty. This is the script I use in forum.modxpertz.tk. It worked at first but it shows nothing now. Here is the code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
mysqli_select_db($conn,'login');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT userid FROM login";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$reguserid=$row["userid"];
}
$userid = mysqli_real_escape_string($conn, $_POST['userid']);
$pswrd = mysqli_real_escape_string($conn, $_POST['pswrd']);
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$gender = mysqli_real_escape_string($conn, $_POST['gender']);
$token = rand('122332344','922332344');
$url = array('forum.modzexpertz.tk/verify.php#',$token);
$post= join($url);
if($userid!=$reguserid){
$sql = "INSERT INTO login(fname, lname, userid, pswrd, gender)VALUES('$fname', '$lname', '$userid', '$pswrd', '$gender')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}else {
echo "Failed to Register.";
}} else {
echo "A user with the email youve provided has already been registered.";
}}
$conn->close();
?>
I know only little about PHP and jQuery.

Please try below code :
<?php
$servername = "localhost";
$username = "root";
$pswrd = "";
$db = "login";
$conn = mysqli_connect($servername,$username,$pswrd, $db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$table = 'login';
if(#mysqli_num_rows(mysqli_query($conn, "SELECT NULL FROM `$table` WHERE userid='".$_POST['userid']."'")) > 0){
$error = "1";
echo "user with same userid is already exist";
}
if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['gender']) && isset($_POST['userid']) && isset($_POST['pswrd']) && $_POST['fname']!="" && $_POST['lname']!="" && $_POST['gender']!="" && $_POST['userid']!="" && $_POST['pswrd']!="")
{
if($error==''){
$ins['fname'] = mysqli_real_escape_string($conn, $_POST['fname']);
$ins['lname'] = mysqli_real_escape_string($conn, $_POST['lname']);
$ins['gender'] = mysqli_real_escape_string($conn, $_POST['gender']);
$ins['userid'] = mysqli_real_escape_string($conn, $_POST['userid']);
$ins['pswrd'] = mysqli_real_escape_string($conn, $_POST['pswrd']);
$insertsql = "INSERT INTO `$table` (fname, lname, gender, userid, pswrd) VALUES ('".$ins['fname']."','".$ins['lname']."','".$ins['gender']."','".$ins['userid']."','".$ins['pswrd']."')";
#mysqli_query($conn, $insertsql);
//echo $insertsql; exit;
echo "Success";
}
}else{
echo "Please enter required parameters";
}
mysqli_close($conn);
?>

Related

Why I am not getting data in my database?

I am trying to POST my data in my database but unable to do... Which mistake I did here? I have simply try to print Query but it prints without any value....
If I did any mistake then let me know..
And suggest me what can I do now...
<?php
if(isset($_POST['firstname'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sms";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("Could not connect to the database due to the following error --> ".mysqli_connect_error());
}
//echo "success";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$mobno = $_POST['mobno'];
$dob = $_POST['dob'];
$sql1="INSERT INTO `register`(`firstname`, `lastname`, `email`, `mobno`, `dob`) VALUES ('$firstname','$lastname','$email','$mobno','$dob')";
echo $sql1;
if($conn->query($sql1) == true){
// echo "Successfully inserted";
// Flag for successful insertion
$insert = true;
}
else{
echo "ERROR: $sql1 <br> $conn->error";
}
// Close the database connection
$conn->close();
}
?>

Error while outputting sessions in other page

I created a login page in PHP with session. everything is going well. but when i try to output the session in other page. it giving me NOTICE
Notice: Undefined variable: email in C:\xampp\htdocs\COIN Website\test5.php on line 81
Notice: Undefined variable: email in C:\xampp\htdocs\COIN Website\test5.php on line 82
Notice: Undefined variable: email in C:\xampp\htdocs\COIN Website\test5.php on line 84
I don't know whats wrong. Actually, I am new to PHP.
here is the login page
<?php
session_start();
if (isset($_POST['submit'])) {
$servername = "localhost";
$username = "root";
$password = "";
$db_name = "coins";
$con = mysqli_connect("$servername", "$username", "$password","$db_name");
$email = mysqli_real_escape_string($con, $_POST['email']);
$eth = mysqli_real_escape_string($con, $_POST['eth']);
if (empty($email) || empty($eth)) {
header("Location: home.php?Login=Empty_fields");
exit();
} else{
$sql = "SELECT * FROM users WHERE email='$email' ";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck < 1) {
header("Location: home.php?Login=user_does_not_exist");
exit();
}
else {
$check = mysqli_query($con, "SELECT email FROM users WHERE eth='$eth'");
if (mysqli_num_rows($check) >= 1) {
$_SESSION['email'] = $row['email'];
$_SESSION['eth'] = $row['eth'];
header("Location: pow.php?Login=Success");
} else {
header("Location: home.php?Login=invaild_email_or_eth address");
}
}
}
}else {
header("Location: home.php?Login=Error");
exit();
}
?>
and the other page code
if (isset($_POST['submit'])) {
$servername = "localhost";
$user = "root";
$password = "";
$dbname = "coins";
// Create connection
$conn = new mysqli($servername, $user, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$_SESSION['email'] = $email;
echo $email;
$sql = "INSERT INTO profile (email, action_points)
VALUES ('$email' , '0.003')";
if ($conn->query($sql) === TRUE) {
echo "success";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
You can try like this. you are doing wrong with get session variable value.
<?php session_start();
// check session
if(isset($_SESSION['email']))
{
$email = $_SESSION['email'];
// Now you can do your stuff
}
else
{
echo "session not set for email";
die;
}

php user registration can't connect to database

I am having problems getting my PHP to connect to my database. This is my first time ever working with PHP and I am not sure what is wrong, I have searched all over the internet without finding a solution. Hopefully you guys can help!
Here's the error:
Fatal error: Class 'mysqli_connect' not found in D:\wamp64\www\einsteindesigns\reg.php on line 4
and the code:
<?php
session_start();
$host = "localhost";
$user = "root";
$pwd = "";
$db = "userdb";
$mysqli = mysqli_connect($host, $user, $pwd, $db);
if(! $mysqli )
{
die('Could not connect: ' . mysqli_error());
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST['password'] == $_POST['confirmpassword']) {
$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$password = md5($_POST['password']);
$confirmpassword = md5($_POST['confirmpassword']);
$_SESSION['email'] = $email;
$sql = "INSERT INTO $db (first, last, email, password) "
. "VALUES ('$first', '$last', '$email', '$password')";
$_SESSION['message'] = "Registration Successful";
}
else {
$_SESSION['message'] = "Error: User account could not be
created. Please try again.";
}
mysqli_close($mysqli);
}
else {
$_SESSION['message'] = "Passwords do not match";
}
?>

Data is not inserting on the database Game resgistration

So im making a web game app my problem is my register.php is not inserting users in the database please need help by the way im just a beginner in PHP Thanks
Here is the Code:
<?php
require_once('mysql_conn.php');
$Username = mysql_real_escape_string($_POST['Username']);
$Password = md5(mysql_real_escape_string($_POST['Password']));
$Email = mysql_real_escape_string($_POST['Email']);
$query_check ="SELECT user_name FROM account_info WHERE user_name = '$Username'";
$retval_check = mysql_query( $query_check, $conn );
if( $Username == "" || $Password == "" || $Email == "" ){
echo"Please fill the field";
} else{
if(mysql_num_rows($retval_check)){
echo"Username Already Taken";
} else {
$query = "INSERT INTO account_info(user_name,user_passemail) VALUES ('$Username','$Password','$Email');";
$retval = mysql_query( $query, $conn );
echo "<script>";
echo "alert('Thank you for registering Enjoy the game !')";
echo "</script>";
echo "<script>";
echo 'location.href = "menu.html";';
echo "</script>";
}
}
mysql_close($conn);
?>
Change:
$query = "INSERT INTO account_info(user_name,user_passemail) VALUES ('$Username','$Password','$Email');";
To:
$query = "INSERT INTO account_info(user_name,user_pass,email) VALUES ('$Username','$Password','$Email');";
You forgot to add a comma ","
Warning mysql_query, mysql_fetch_array,mysql_connect etc.. extensions were deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.
Instead, the MySQLi or PDO_MySQL extension should be used.
YOu miss the coma between user_pass and email in column name
$query = "INSERT INTO account_info(user_name,user_pass,email) VALUES ('$Username','$Password','$Email');";
try mysqli
//db connection
global $conn;
$servername = "localhost"; //host name
$username = "username"; //username
$password = "password"; //password
$mysql_database = "dbname"; //database name
//mysqli prepared statement
$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());
mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");
//require_once('mysql_conn.php');
$Username = mysqli_real_escape_string($_POST['Username']);
$Password = md5(mysqli_real_escape_string($_POST['Password']));
$Email = mysqli_real_escape_string($_POST['Email']);
//$query_check ="SELECT user_name FROM account_info WHERE user_name = '$Username'";
// $retval_check = mysqli_query( $query_check, $conn );
if(!empty($Username))
{
$stmt = $conn->prepare("SELECT user_name FROM account_info WHERE user_name =? ");
$stmt->bind_param('s',$Username);
The argument may be one of four types:
i - integer
d - double
s - string
b - BLOB
//change it by respectively
$stmt->execute();
$get_result =$stmt->get_result();
$row_count= $get_result->num_rows;
//$row_count= $stmt->affected_rows;
$stmt->close();
//$conn->close();
}
if( $Username == "" || $Password == "" || $Email == "" ){
echo"Please fill the field";
} else{
if($row_count>0){
echo"Username Already Taken";
} else {
//$query = "INSERT INTO account_info(user_name,user_pass,email) VALUES ('$Username','$Password','$Email');";
^^^^^
//$retval = mysql_query( $query, $conn );
$stmt1 = $conn->prepare("INSERT INTO account_info(user_name,user_pass,email) VALUES (?,?,?)");
$stmt->bind_param('sss',$Username,$Password,$Email);
The argument may be one of four types:
i - integer
d - double
s - string
b - BLOB
//change it by respectively
$stmt1->execute();
//$get_result1 =$stmt1->get_result();
//$row_count1= $get_result1->num_rows;
$row_count1= $stmt1->affected_rows;
$stmt1->close();
$conn->close();
if($row_count1>0)
{
echo "<script>";
echo "alert('Thank you for registering Enjoy the game !')";
echo "</script>";
}
else
{
echo "<script>";
echo "alert('registeration failed')";
echo "</script>";
}
echo "<script>";
echo 'location.href = "menu.html";';
echo "</script>";
}
}
wrong
$query = "INSERT INTO account_info(user_name,user_passemail) VALUES ('$Username','$Password','$Email');";
correct
$query = "INSERT INTO account_info(user_name,user_pass,email) VALUES ('$Username','$Password','$Email')";

Data entered 2 times after run the query in database

I have faced another problem please help.
when i query to my db 1 time, 2 rows entered into the db!
Here is the code:
<?php
$servername = "localhost";
$username = "***";
$password = "***";
$dbname = "***";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$name = $_POST['name'];
$famil = $_POST['famil'];
$email = $_POST['email'];
$amount = $_POST['amount'];
$name = mysqli_real_escape_string($conn, $name);
$famil = mysqli_real_escape_string($conn, $famil);
$email = mysqli_real_escape_string($conn, $email);
$amount = mysqli_real_escape_string($conn, $amount);
$sql = "INSERT INTO users".
"(name, famil, email, amount)".
"VALUES ('$name','$famil','$email','$amount')";
mysqli_query($conn,$sql);
if (mysqli_query($conn, $sql)) {
echo "Data entered successfully.";
} else {
echo "Error entering data: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
and the result after 2 times run this code:
3 mohsen gholi ***#yahoo.com 235354346
4 mohsen gholi ***#yahoo.com 235354346
5 mohsen gholi ***#yahoo.com 235354346
6 mohsen gholi ***#yahoo.com 235354346
Remove this line as said by Fred
mysqli_query($conn,$sql);
and just use:
if (mysqli_query($conn, $sql)) {
echo "Data entered successfully.";
} else {
echo "Error entering data: " . mysqli_error($conn);
}

Categories