Page 1 of 1

if statement

Posted: Thu Dec 19, 2013 7:52 pm
by SerbanSpire
I have this code snippet

Code: Select all

<?php

include 'connect.php';

?>

<?php
session_start();
$char_name = $_POST['char_name'];
$dynasty_name = $_POST['dynasty_name'];

if($char_name == "" || $dynasty_name == "")
{
	echo "You didn't enter all the character requirements!";
	echo "<a href='createchar.php>Go back</a>";
}

else{


$query = sprintf("SELECT id FROM users WHERE UPPER(username) = UPPER('%s')",
			mysql_real_escape_string($_SESSION['username']));
$result = mysql_query($query);
list($userID) = mysql_fetch_row($result);


$sql = sprintf("SELECT char_dynasty FROM characters WHERE id = '%b'", $userID);
$result = mysql_query($sql);


if(mysql_fetch_row($result))
{
	$query = sprintf("SELECT char_dynasty FROM characters WHERE id = '%b'", $userID);
	$result = mysql_query($query);
	echo "There already is a dynasty by that name!";
	echo "<a href='createchar.php>Go back</a>";
}

else
{
	$sql = "Insert into characters(id, char_name, char_dynasty, picture) VALUES ('$userID','$char_name','$dynasty_name','Male1.png')";
	mysql_query($sql);
}

}

?>
Why this code never gets to this if:

Code: Select all

if(mysql_fetch_row($result))
{
	$query = sprintf("SELECT char_dynasty FROM characters WHERE id = '%b'", $userID);
	$result = mysql_query($query);
	echo "There already is a dynasty by that name!";
	echo "<a href='createchar.php>Go back</a>";
}

Re: if statement

Posted: Fri Dec 20, 2013 6:34 am
by Winawer
Assuming id isn't binary, try using %d instead of %b.

Re: if statement

Posted: Tue Dec 24, 2013 3:52 am
by SerbanSpire
Oh wow thanks. It works now. It's not the first time i do this lawl :))