Jump to content

[SOLVED] Code not outputting properly


macattack

Recommended Posts

I have the following code

 

require("mysql.php");
$con = mysql_connect(PATH,USERNAME,PASSWORD);
$database = mysql_select_db(DATABASE);
if(!$database)
{
header('Location: login.php?database=no');
}
else
{
$surname1 = strtoupper($_POST['surname']);
$name1 = strtoupper($_POST['name']);
$surname = mysql_real_escape_string($surname1);
$name = mysql_real_escape_string($name1);
$query = mysql_query("SELECT * FROM Electors WHERE Surname='".$surname."' AND Name='".$name."';");
if(mysql_num_rows($query) >= 2)
{
	echo "Who do you mean?<br/>
			<table border='1'><tr><th>Name</th><th>Surname</th><th>Address</th><th>Submit</th></tr>";
	while($row=mysql_fetch_assoc($query))
	{
		echo "<tr><form action='index.php?task=name&vid=".$row['V_ID']."' method='post'>
			<td>".$row['Name']."</td><td>".$row['Surname']."</td><td>";
		if($row['unit'] != ' ')
		{
			echo $row['unit']."-";
		}
		echo $row['number']." ".$row['street']." ".$row['streettype']."</td>";
		echo "<td><input type='submit'></td></tr>";
	}
}
if(mysql_num_rows($query) == '1')
{
	$row = mysql_fetch_assoc($query));
	header("Location: index.php?task=name&vid=".$row['V_ID']);
}
else
{
	header("Location: index.php?task=name&exist=no");
}
}

 

and for some reason, it won't execute past the first if(mysql_num_rows($query)) clause.

 

If someone may know why (I bet it's a minor oversight that an hour or so of trying things hasn't caught) your help is greatly appreciated!

 

Basically, I want to see if the same name is in the list more than once. If it is, the person should have the option to confirm who they are searching for, if it's there once, it should redirect automatically, and if it's not there at all, redirect and give an error message.

Link to comment
Share on other sites

change:

$con = mysql_connect(PATH,USERNAME,PASSWORD);
$database = mysql_select_db(DATABASE);
if(!$database)
{
   header('Location: login.php?database=no');
}

to

$con = mysql_connect(PATH,USERNAME,PASSWORD);
$database = mysql_select_db(DATABASE,$con);
if(!$database)
{
   header('Location: login.php?database=no');
}

Link to comment
Share on other sites

I have made a few changes to the code, but still no luck.

 


require("mysql.php");
$con = mysql_connect(PATH,USERNAME,PASSWORD);
$database = mysql_select_db(DATABASE);
if(!$database)
{
header('Location: login.php?database=no');
}
else
{
$surname1 = strtoupper($_POST['surname']);
$name1 = strtoupper($_POST['name']);
$surname = mysql_real_escape_string($surname1);
$name = mysql_real_escape_string($name1);
$query = mysql_query("SELECT * FROM Electors WHERE Surname='".$surname."' AND Name='".$name."';");
//echo mysql_num_rows($query);

if(mysql_num_rows($query) >= 2)
{
	echo "Who do you mean?<br/>
			<table border='1'><tr><th>Name</th><th>Surname</th><th>Address</th><th>Submit</th></tr>";
	while($row=mysql_fetch_assoc($query))
	{
		echo "<tr><form action='index.php?task=name&vid=".$row['V_ID']."' method='post'>
			<td>".$row['Name']."</td><td>".$row['Surname']."</td><td>";
		if($row['unit'] != ' ')
		{
			echo $row['unit']."-";
		}
		echo $row['number']." ".$row['street']." ".$row['streettype']."</td>";
		echo "<td><input type='submit'></td></form></tr>";
	}
		echo "</table>";
}
if(mysql_num_rows($query) == 1)
{
	echo "here";
	$row = mysql_fetch_assoc($query));
	header("Location: index.php?task=name&vid=".$row['V_ID']);
}
else
{
	header("Location: index.php?task=name&exist=no");
}

}

Link to comment
Share on other sites

Are you sure its returning 1 or are you just assuming it is because it should?

 

You never actually check your query was successful before proceeding to use its result, this is nebver a good idea.

Link to comment
Share on other sites

The line that is commented out was uncommented and it returned a 1. So yes, I'm sure the query itself works. It's all the stuff after that that won't work. It won't even return the 1 unless the entire part after (with the exception of the final curly brace) is commented out. If that part is left on, the page is blank.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.