macattack Posted May 16, 2009 Share Posted May 16, 2009 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. Quote Link to comment Share on other sites More sharing options...
jlhaslip Posted May 16, 2009 Share Posted May 16, 2009 Echo out the value of the num_rows to see what it returns. Quote Link to comment Share on other sites More sharing options...
papaface Posted May 16, 2009 Share Posted May 16, 2009 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'); } Quote Link to comment Share on other sites More sharing options...
macattack Posted May 17, 2009 Author Share Posted May 17, 2009 It is returning 1 value (I used a name that I know is in the database once) Quote Link to comment Share on other sites More sharing options...
macattack Posted May 17, 2009 Author Share Posted May 17, 2009 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"); } } Quote Link to comment Share on other sites More sharing options...
trq Posted May 17, 2009 Share Posted May 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
macattack Posted May 17, 2009 Author Share Posted May 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
BobcatM Posted May 17, 2009 Share Posted May 17, 2009 $row = mysql_fetch_assoc($query)); You have an extra ) on the right I believe. Quote Link to comment Share on other sites More sharing options...
macattack Posted May 17, 2009 Author Share Posted May 17, 2009 Thank you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.