Currently, my problem continues to persist as I want to display errors for existing accounts or a success message if the user creates a non-already existing account. For some odd reason, even if the sign-up is successful, it still display the errors? Is there something that I am doing wrong? The two if statements still run even if they are false??? I am not understanding...
if ($db_found) {
$uSQL = "SELECT * FROM login WHERE username = '$username'";
$uresult = mysql_query($uSQL);
$unum_rows = mysql_num_rows($uresult);
$eSQL = "SELECT * FROM login WHERE email = '$email'";
$eresult = mysql_query($eSQL);
$enum_rows = mysql_num_rows($eresult);
if ($unum_rows > 0) {
echo '<div class="error-message"><li>Username Already Exists</li></div>';
}
if ($enum_rows > 0) {
echo '<div class="error-message"><li>Email Already Exists</li></div>';
}
else {
echo '<div class="success-message">successfully</div>';
}
}
Personally I would combine the queries - and simplify the logic ( and escape your input )
if ($db_found) {
$sql = "SELECT * FROM login WHERE username = '{mysql_real_escape_string($username)}' OR email = '{mysql_real_escape_string($email)}'";
$result = mysql_query($sql);
if( mysql_num_rows($result) ){
while(false !== ($row = mysql_fetch_assoc($result))) {
if($row['username'] == $username ){
echo '<div class="error-message"><li>Username Already Exists</li></div>';
}
if($row['email'] == $email) {
echo '<div class="error-message"><li>Email Already Exists</li></div>';
}
}
}else{
echo '<div class="success-message">successfully</div>';
}
}
This might help you
$enum_rows = mysql_num_rows($eresult);
if($unum_rows < 1)
{
echo "Username is available";
}
else
{
echo '<div class="error-message"><li>Username Already Exists</li></div>';
}
if($enum_rows < 1)
{
echo '<div class="success-message">successfully</div>';
}
else
{
echo '<div class="error-message"><li>Email Already Exists</li></div>';
}
Related
I am trying to insert email into the table newsletter but it is not inserting into database
This is the code:
PHP
$table = 'news_letters';
if(isset($_REQUEST['mode'])) {
extract($_REQUEST);
$email=$_POST['email'];//print_r($email);die;
if (isset($_POST['email']) && trim($_POST['email'])=="submitNewsletter") {
$condition = " WHERE email='$email'";
$res = $obj_common->select_record('news_letters', $condition);
if (count($res) > 0) {
echo 'already register';
header("location:index.html?msg=already_register");
die;
}
}
if(trim($_POST['mode'])=="submitNewsletter")
{
$condition = " WHERE email='$email'";
$res = $obj_common->select_record('news_letters', $condition);
if (count($res) > 0) {
echo 'already register';
header("location:thankyou.html?msg=already_register");
die;
}
else
{
$data=array('email'=>"$email",'status'=>'$status');
$res=$obj_common->insert_record('news_letters', $data); //print_r($res);die;
header("location:thankyou.html?msg=newsletter_add_success");
die;
}
}
}
?>
Email is not inserting into the table newsletter.
This is my php file in which I am trying to check if the email already exists or not.
<?php
include_once("connection.php");
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$pass=$_REQUEST['pass'];
$mobno=$_REQUEST['mobno'];
$checkemail="SELECT * FROM dhruv_users WHERE email= '$_REQUEST[email]'";
$checkmob="SELECT * FROM dhruv_users WHERE mobno= '$_REQUEST[mobno]'";
$rsemail = mysqli_query($conn,$checkemail);
$rsmob = mysqli_query($conn,$checkno);
$dataemail = mysqli_num_rows($rsemail);
$datamob = mysqli_num_rows($rsmob);
if($dataemail >= 1) {
echo "exists";
}
else if($datamob >= 1)
{
echo "exists";
}
else{
$select=mysqli_query($conn,"select max(id) as id from dhruv_users");
if($data=mysqli_fetch_array($select))
{
$id=$data['id'];
$id++;
}
else
{
$id=1;
}
$query=mysqli_query($conn,"insert into dhruv_users VALUES ('$id','$name','$email','$mobno','$pass')");
if($query)
{
echo "success";
}
else{
echo "unsuces";
}
}
?>
There is no error but data gets entered successfuly without checking mob no if it exists or not.
Entering same mob no again and again shows success message instead of exist message.
Why dont you use mysqli_num_rows instead of mysqli_fetch_array with MYSQLI_NUM .
Try the following
$rs = mysqli_query($conn,$check);
$dataa = mysqli_num_rows($rs);
if($dataa > 1) {
echo "User Already in Exists<br/>";
}
You need to count the result which you getting from DB.
There is a logical error in code. Please have a look on code below:-
Your code
if($dataa[0] > 1) {
echo "User Already in Exists<br/>";
}
Replace above with:
if(count($dataa) > 1) {
echo "User Already in Exists<br/>";
}
You need to write your query with proper quotes. It's unable to recognize the email index of $_REQUEST. Also, use mysqli_num_rows function.
Refer to the code below for best possible practice:
$check = "SELECT * FROM dhruv_user WHERE email= '" . $_REQUEST['email'] . "'";
$rs = mysqli_query($conn,$check);
if ($rs) {
$rowcount = mysqli_num_rows($rs);
if ($rowcount) {
echo "User already exists<br/>";
}
}
The ajax function for the follow and unfollow features on the webapp I'm developing works fine, but the issue is that when I click on either 'Follow' or 'Unfollow' button on any of the displayed list of my followers, the follow or unfollow button ONLY toggle without page refresh for the first follower on the displayed list of followers. If I click follow or unfollow on any other displayed followers except the first one on the list, I would have to refresh the page to see the follow/unfollow button changed.
In summary, clicking follow/unfollow button on any follower except the first on my displayed list of followers doesn't toggle the follow/unfollow button without page refresh.
PHP Code
$sql = "SELECT users.firstname, users.lastname, users.avatar, users.username FROM users INNER JOIN follows ON users.username=follows.user1 WHERE user2='$u' ORDER BY RAND()";
$result_select = mysqli_query($db_connect, $sql);
// Check if user has followers
$numrows = mysqli_num_rows($result_select);
if($numrows < 1){
echo '<div id="nav-follow">';
echo '<ul>';
echo '<li>'.''.'Followers'.''.'</li>';
echo '<li>'.''.'Following'.''.'</li>';
echo '</ul>';
echo '</div>';
echo '<div id="followerList" style="height:60px; text-align:center; vertical-align: middle; font-size:20px; color:white;">';
echo "I don't have any follower yet. If I start following others, they will also follow me.";
echo '</div></div>';
include_once("template_pageRight.php");
exit();
}
?><?php
echo '<div id="nav-follow">';
echo '<ul>';
echo '<li>'.''.'Followers'.''.'</li>';
echo '<li>'.''.'Following'.''.'</li>';
echo '</ul>';
echo '</div>'.'<br />';
// Fetch the user row from the query above
$row = array();
while ($row = mysqli_fetch_array($result_select))
$rows[] = $row;
foreach($rows as $row){
$followUsername = $row['username'];
?><?php
$following = false;
if($u == $log_username && $user_ok == true){
$follow_check = "SELECT id FROM follows WHERE user1='$log_username' AND user2='$followUsername' LIMIT 1";
if(mysqli_num_rows(mysqli_query($db_connect, $follow_check)) > 0){
$following = true;
}
} elseif($u != $log_username && $user_ok == true){
$follow_check = "SELECT id FROM follows WHERE user1='$log_username' AND user2='$followUsername' LIMIT 1";
if(mysqli_num_rows(mysqli_query($db_connect, $follow_check)) > 0){
$following = true;
}
}
?><?php
//$followUsername_Btn = "";
if($following == true){
$followUsername_Btn = '<button onclick="followUsernameToggle(\'unfollow\',\''.$followUsername.'\',\'followUsernameBtn\')">Unfollow</button>';
} elseif($followUsername != $log_username && $following == false && $user_ok == true) {
$followUsername_Btn = '<button onclick="followUsernameToggle(\'follow\',\''.$followUsername.'\',\'followUsernameBtn\')">Follow</button>';
} else {
$followUsername_Btn = '<button disabled>Me</button>';
}
?><?php
echo '<div id="followerList">';
echo '<a class="image" href="http://localhost:8080/app/user_audio.php?u='.$followUsername.'"><img src="user/'.$followUsername.'/'.$row['avatar'].'" alt="'.$followUsername.'">'.'<br />';
echo '<div id="fuserName">'.$row["firstname"].' '.$row["lastname"].'</a>'.'</div>';
echo '<span id="followUsernameBtn">'.$followUsername_Btn.'</span>';
echo '</div><br />';
}
?>
jQuery
function followUsernameToggle(type, user, elem) {
var conf = confirm("Please confirm.");
if (conf != true) {
return false;
}
//_(elem).innerHTML = 'please wait...';
var xhttp;
if (window.XMLHttpRequest) {
// code for modern browsers
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.responseText == "follow_ok") {
_(elem).innerHTML = '<button onclick="followUsernameToggle(\'unfollow\',\'<?php echo $followUsername; ?>\',\'followUsernameBtn\')">Unfollow</button>';
} else if (this.responseText == "unfollow_ok") {
_(elem).innerHTML = '<button onclick="followUsernameToggle(\'follow\',\'<?php echo $followUsername; ?>\',\'followUsernameBtn\')">Follow</button>';
} else {
alert(this.responseText);
_(elem).innerHTML = 'Try again later';
}
}
};
xhttp.open("POST", "php_parsers/follow_system.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("type=" + type + "&user=" + user);
}
follow_system.php
<?php
$sql = "SELECT countrycode, mobile FROM users WHERE username='$log_username' LIMIT 1";
$query = mysqli_query($db_connect, $sql);
if(mysqli_num_rows($query) > 0){
$row = mysqli_fetch_row($query);
$countrycode = $row[0];
$mobile = $row[1];
}
?><?php
$user = mysqli_real_escape_string($db_connect, $_POST['user']);
//$type = mysqli_real_escape_string($db_connect, $_POST['type']);
if($_POST['type'] == "follow" && isset($_POST['user'])){
$sql = "INSERT INTO follows (user1, user2, countrycode, mobile, datefollowed) VALUES ('$log_username','$user','$countrycode','$mobile',now())";
$query = mysqli_query($db_connect, $sql);
mysqli_close($db_connect);
echo "follow_ok";
exit();
} else if($_POST['type'] == "unfollow" && isset($_POST['user'])){
$sql = "DELETE FROM follows WHERE user1='$log_username' AND user2='$user' AND countrycode='$countrycode' AND mobile='$mobile'";
$query = mysqli_query($db_connect, $sql);
mysqli_close($db_connect);
echo "unfollow_ok";
exit();
}
?>
As I said, looking at the below picture, when I click Follow or Unfollow on FollowerB or C, the button doesn't toggle and I have to refresh the page to see the change in button from follow to unfollow and vice versa. But when I click on FollowerA which is at the top of the list, the button toggle without refresh.
Please I need help on why it's only the first follower on the list that works properly.
Follow_Unfollow_Picture
I am creating a API for android developer in PHP in which he want to delete some values from database and want to show a message after that.
Now the problem is this data is deleting successfully but this API always shows else part message after complete the process. If I remove the else part its return the null which crash the android app. So I just want to give a proper json message to the android developer
Here is the code which I am trying
if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes" && $user_name_id == $user_name_id1)
{
$tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_count as $table_count)
{
$user_count = mysql_query("select * from $table_count where user_name = '$user_name'");
$total_user_count = mysql_num_rows($user_count);
if($total_user_count > 0)
{
$tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_data as $table_data)
{
$user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
if($user_sql)
{
$response['success'] = 1;
$response['user']['error_msg'] = 'Clear Successfully All History!';
}
}
}
else
{
$response['success'] = 0;
$response['user']['error_msg'] = 'Record Not Found!';
}
}
}
I know there is something wrong with this logic. But I need expert advise where my logic is wrong and what I have to do make it success
Problem with your original code, is that you are setting success/failure inside the loop. One of the four table may/may not contain the username. And if the last table don't have that, then as per your logic you are getting "record not found" even if previous iteration of the loop deleted data from the tables where username exists.
<?php
$conn = mysqli_connect(.....);
if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes" && $user_name_id == $user_name_id1) {
$tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");
$userHistoryDeleted = 0;
foreach($tables_count as $table_count) {
//if history is found, then it will be deleted otherwise not
mysql_query("delete from $table_count where user_name = '$user_name'");
if(mysqli_affected_rows($conn)) {
$userHistoryDeleted = 1;
}
}
$msg = 'Record Not Found!';
if($userHistoryDeleted) {
$msg = 'Clear Successfully All History!';
}
$response['success'] = $userHistoryDeleted;
$response['user']['error_msg'] = $msg;
}
Change your code :
if($total_user_count > 0)
{
$tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_data as $table_data)
{
$user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
if($user_sql)
{
$response['success'] = 1;
$response['user']['error_msg'] = 'Clear Successfully All History!';
}
}
}
else
{
$response['success'] = 0;
$response['user']['error_msg'] = 'Record Not Found!';
}
to this one
if($total_user_count > 0)
{
$tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_data as $table_data)
{
$user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
}
$response['success'] = 1;
$response['user']['error_msg'] = 'Clear Successfully All History!';
}
What I'm trying to figure out here is how to access the different array values that I need. I have the following query and it returns this for an array when the print_r() is applied. For some reason it only does the first row from the db table. It should return a whole another row.
<?php
session_start();
// Include the database page
require ('../inc/dbconfig.php');
require ('../inc/global_functions.php');
//Login submitted
if (isset($_POST['submit'])) {
// Errors defined as not being any
$errors = "no";
if((empty($_POST['answer1'])) || (trim($_POST['answer1'])=="") || ($_POST['answer1'] == NULL) || (!isset($_POST['answer1']))){$errors = "yes";}
if((empty($_POST['answer2'])) || (trim($_POST['answer2'])=="") || ($_POST['answer2'] == NULL) || (!isset($_POST['answer2']))){$errors = "yes";}
// Error checking, make sure all form fields have input
if ($errors == "yes") {
// Not all fields were entered error
$message = "You must enter values to all of the form fields!";
$output = array('errorsExist' => true, 'message' => $message);
} else {
$userID = mysqli_real_escape_string($dbc,$_POST['userID']);
$answer1Post = mysqli_real_escape_string($dbc,$_POST['answer1']);
$answer2Post = mysqli_real_escape_string($dbc,$_POST['answer2']);
$question1 = mysqli_real_escape_string($dbc,$_POST['question1']);
$question2 = mysqli_real_escape_string($dbc,$_POST['question2']);
$query = "SELECT * FROM manager_users_secretAnswers WHERE userID = '".$userID."'";
$result = mysqli_query($dbc,$query);
// Count number of returned results from query
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
$answer = $row['answer'];
// Comparing the database password with the posted password
if ($answer == $answerPost) {
} else {
$errors = "yes";
$message = "Your answers did not match the answers inside the database!";
$output = array('errorsExist' => true, 'message' => $message);
}
}
} else {
$errors = "yes";
$message = "We did not find any answers for your questions! Please consult the site administrator!";
$output = array('errorsExist' => true, 'message' => $message);
}
}
}
//Output the result
$output = json_encode($output);
echo $output;
?>
Because you just fetch the first one, where you should loop on the result set instead:
$query = "SELECT * FROM manager_users_secretAnswers WHERE userID = '$userID'";
$result = mysqli_query($dbc,$query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
print_r($row);
}
}
By the way, you should be using prepared statements to avoid SQL injection.
You need to wrap your fetch in a loop. e.g.
if (mysqli_num_rows($result) > 0)
{
while (($row = mysqli_fetch_array($result)) !== false)
{
if ($row['answer'] == $answerPost)
{
// $row matches what we're looking for
}
else
{
$errors = "yes";
$message = "Your answers did not match the answers inside the database!";
$output = array('errorsExist' => true, 'message' => $message);
}
print_r($row);
}
}