Pomoc oko PHP forme

Poz,

Ovako imam problem sa skriptom, u prvom polju di je Nickname, to polje nebi trebalo provjeravat kad se klikne na submit nego samo kad ga izmjenim da mi provjeri dali postoji vec taj nick i da mu onda izbaci error, ako ga ubacim dole za provjeru svaki put kad napravim update kaze mi da Nick vec postoji posto je u tom polju di mi je ovaj user

if ($users->nick_exists($nickname) === true)
{
$error = '<p class="error-msg">Nickname in use, please choose another</p>';
} 

cijeli kod

<?php
// user profile

/*
if ($users->nick_exists($nickname) === true)
{
$error = '<p class="error-msg">Nickname in use, please choose another</p>';
} 
*/

$country = $teams->get_country();
$last_update = time();

if (isset($_POST['update']))
{
if (isset($_POST['nickname']) && isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['dob']) && isset($_POST['gender']) && isset($_POST['website']) && isset($_POST['country']) && isset($_POST['town']) && isset($_POST['bio']))
{
	$nickname   = $general->safe_input($_POST['nickname']);
	$first_name = $general->safe_input($_POST['first_name']);
	$last_name 	= $general->safe_input($_POST['last_name']);
	$dob 		= $general->safe_input($_POST['dob']);
	$gender 	= $general->safe_input($_POST['gender']);
	$website 	= $general->safe_input($_POST['website']);
	$country 	= $general->safe_input($_POST['country']);
	$town 		= $general->safe_input($_POST['town']);
	$bio 		= $general->safe_input($_POST['bio']); 

	if (empty($first_name) || empty($last_name) || empty($dob) || empty($gender) || empty($website) || empty($country) || empty($town) || empty($bio))
	{
		$error = '<p class="error-msg">All fields are required.</p>';
	}
	else
	{
		// update user data
		$update = $users->update_user($first_name, $last_name, $dob, $gender, $website, $country, $town, $bio, $last_update, $nickname, $id);

		if ($update === true)
		{
			$error = '<p class="success-msg">Data successfully saved.</p>';
			header('Refresh: 2; url=profile.php');
		}
		else
		{
			$error = '<p class="error-msg">There was a problem updating your data, please try again.</p>';
		}
	}
}
}

include 'inc/main/header.php';

?>

<h2 class="main-title">My profile</h2>
<hr />

<div style="background: #f3f3f3; padding: 10px; width:640px; overflow:hidden;">

<?php if (!empty($error)) { echo $error; } ?>

<form action="profile.php" method="POST" class="profile">
<table width="100%" cellspacing="0" cellpadding="5">
	<tr>
		<td width="110">Nickname</td>
		<td><input type="text" name="nickname" value="<?php echo $data['nickname']; ?>"></td>
	</tr>
	<tr>
		<td>First name</td>
		<td><input type="text" name="first_name" value="<?php echo $data['first_name']; ?>"></td>
	</tr>
	<tr>
		<td>Last name</td>
		<td><input type="text" name="last_name" value="<?php echo $data['last_name']; ?>"></td>
	</tr>
	<tr>
		<td>Date of birth</td>
		<td><input type="text" name="dob" value="<?php echo $data['dob']; ?>"></td>
	</tr>
	<tr>
		<td>Gender</td>
		<td>
			<select name="gender" style="width: 215px;">
				<option <?php if ($data['gender'] == 'Male') { echo 'selected="selected"'; } ?> value="Male">Male</option>
				<option <?php if ($data['gender'] == 'Female') { echo 'selected="selected"'; } ?> value="Female">Female</option>
			</select>
		</td>
	</tr>
	<tr>
		<td>Email</td>
		<td><input type="text" name="email" value="<?php echo $data['email']; ?>" disabled="disabled"></td>
	</tr>
	<tr>
		<td>Website</td>
		<td><input type="text" name="website" value="<?php echo $data['website']; ?>"></td>
	</tr>
	<tr>
		<td>Country</td>
		<td>
			<select name="country" id="country" style="width: 215px;">
				<option>
					<?php
						$cntry = explode(",", $data['country']);
						echo $cntry['0']; 
					?>
				</option>
				<?php foreach ($country as $key) 
				{
					echo '
					<option value="'.$key['name'].','.$key['alpha_2'].'">'.$key['name'].'</option>
					';
				}
				?>
			</select>
		</td>
	</tr>
	<tr>
		<td>Town</td>
		<td><input type="text" name="town" value="<?php echo $data['town']; ?>"></td>
	</tr>
	<tr>
		<td valign="top">Bio</td>
		<td><textarea cols="30" rows="5" name="bio"><?php echo $data['bio']; ?></textarea></td>
	</tr>
	<tr>
		<td>Member since</td>
		<td><input type="text" value="<?php echo date('d.m.Y', $data['registered_date']); ?>" disabled="disabled"></td>
	</tr>
	<tr>
		<td>Latest edit</td>
		<td><input type="text" value="<?php if ($data['last_updated'] != '') { echo date('d.m.Y - H:m:s', $data['last_updated']); } ?>" disabled="disabled"></td>
	</tr>
	<tr>
		<td>Facebook</td>
		<td><input type="text" name="facebook" value="<?php echo $data['facebook']; ?>"></td>
	</tr>
	<tr>
		<td>Youtube</td>
		<td><input type="text" name="youtube" value="<?php echo $data['youtube']; ?>"></td>
	</tr>
	<tr>
		<td>Xfire</td>
		<td><input type="text" name="xfire" value="<?php echo $data['xfire']; ?>"></td>
	</tr>
	<tr>
		<td>Twitter</td>
		<td><input type="text" name="twitter" value="<?php echo $data['twitter']; ?>"></td>
	</tr>
	<tr>
		<td>Twitch</td>
		<td><input type="text" name="twitch" value="<?php echo $data['twitch']; ?>"></td>
	</tr>
	<tr>
		<td>Skype</td>
		<td><input type="text" name="skype" value="<?php echo $data['skype']; ?>"></td>
	</tr>
	</table>

	<input type="submit" name="update" value="Update">
</form>
</div>

<?php include 'inc/main/footer.php'; ?>

greska je verovatno u nick_exists() metodi select mysql ti nije dobar ili se izvrsava posle inserta. Sto implicira da je nik vec unesen. Daj da vidim metodu nick_exists??

// if nickname exists
public function nick_exists($nickname)
{
	$query = $this->db->prepare("SELECT nickname FROM users WHERE nickname = ?");
	$query->bindValue(1, $nickname);

	$query->execute();
	$row = $query->rowCount();

	if ($row == 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

Probaj koristi ovaj sql mysql_query(“SELECT * FROM nickname WHERE users LIKE (’%$nickname%’)”); iz razloga jer trazis string. Ja imam slicnu namenu i radi mi super.

Daj update_user metod, da vidim dal ce ovo da radi, s pretpostavkom da imas $_SESSION['nickname']

  if ($_SESSION['nickname'] !== $nickname) {
    if ($users->nick_exists($nickname) === true)
            {
            $error = '<p class="error-msg">Nickname in use, please choose another</p>';
            }

            else {
              $update = $users->update_user($first_name, $last_name, $dob, $gender, $website, $country, $town, $bio, $last_update, $nickname, $id);

            }

  }
  else
  {
    // update user data
    $update = $users->update_user($first_name, $last_name, $dob, $gender, $website, $country, $town, $bio, $last_update, $id);

    if ($update === true)
    {
      $error = '<p class="success-msg">Data successfully saved.</p>';
      header('Refresh: 2; url=profile.php');
    }
    else
    {
      $error = '<p class="error-msg">There was a problem updating your data, please try again.</p>';
    }
  }

Mislim da ste me krivo skuzili ili sam ja krivo objasnio, meni sve radi ok, update radi, funkcija radi, medjutim sto meni treba je znas ono na nekim stranicama ima di ides update profil i imas dole ispod change password i ako ne diras to ne dira to uopce forma, e tako meni isto treba vamo, znaci ako ne diram Nick da ga ne mjenja a ako ga izmjenim da mi kaze da je zauzet. Samo neznam di da ubacim tu provjeru probo sam svakako pa nece.

E pa onda prvo moras da proveris pomocu jqueryja da li je izmenjen input value, pogledaj ovo link

1 Like

Copyright © 2020 WM Forum - AboutContact - Sponsored by: Mydataknox & Profit Monkey