pure_skill_2000 Posted December 21, 2007 Share Posted December 21, 2007 Hi Simple piece of php code to search for a record using one veriable input from a form, anyone know why its not working? I have entered all the database connection stuff but not posting it online for security reasons ta muchio <form method="POST" action="<?echo $_SERVER[ "PHP_SELF" ]?>"> Forname: <input type="text" name="query"> <input type="Submit" name="Submit" value="Search"> </form> <? $query = $_POST['query']; $hostname = ; $username = ; $password = ; $usertable = "Contacts"; $dbName = ; $conn = mysql_connect($hostname,$username,$password) or die ('Error connecting to mysql'); //Connect to mysql and verify user mysql_select_db($dbname); //Select appropriate database if (isset($_POST['Submit'])) { $result = mysql_query( "SELECT * FROM Contacts WHERE Forname='$Array[query]'" ) or die("SELECT Error: ".mysql_error()); while($row = mysql_fetch_array($result)){ print $Array[Forname]; } } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 You turned your $_POST variable into $query, so you should use that in your WHERE clause, not $Array[query] Quote Link to comment Share on other sites More sharing options...
pure_skill_2000 Posted December 21, 2007 Author Share Posted December 21, 2007 Still not getting any outputs but I dont get any errors either the form just reloads ive changed the string to $result = mysql_query( "SELECT * FROM Contacts WHERE Forname=$query" ) Thanks Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 '$query' use single quotes. You need to change the last line as well. print $Array[Forname]; to print_r ($row); Also, the way your logic is, the form will always show up because you are not checking submit until after its displayed. Quote Link to comment Share on other sites More sharing options...
pure_skill_2000 Posted December 21, 2007 Author Share Posted December 21, 2007 Hi Have made the other changes, still seem to be having no luck no outputs from my database any other ideas? Ta Quote Link to comment Share on other sites More sharing options...
trq Posted December 21, 2007 Share Posted December 21, 2007 Your form action is foobar'd. Try... <form method="POST"> Quote Link to comment Share on other sites More sharing options...
trq Posted December 21, 2007 Share Posted December 21, 2007 Post your current code. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 Is the array empty? Change this $result = mysql_query( "SELECT * FROM Contacts WHERE Forname='$Array[query]'" ) or die("SELECT Error: ".mysql_error()); to $result = mysql_query( "SELECT * FROM Contacts WHERE Forname='$query'" ) or die("SELECT Error: ".mysql_error()); $count = mysql_num_rows($result); echo $count; Quote Link to comment Share on other sites More sharing options...
pure_skill_2000 Posted December 21, 2007 Author Share Posted December 21, 2007 This is the code at the mo, still not working for me though <form method="POST"> Forname: <input type="text" name="query"> <input type="Submit" name="Submit" value="Search"> </form> <? $query = $_POST['query']; $hostname = ; $username = ; $password = ; $usertable = ; $dbName = ; $conn = mysql_connect($hostname,$username,$password) or die ('Error connecting to mysql'); mysql_select_db($dbName); if (isset($_POST['Submit'])) { $result = mysql_query( "SELECT * FROM Contacts WHERE Forname='$query'" ) or die("SELECT Error: ".mysql_error()); $count = mysql_num_rows($result); echo $count; } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 Does anything echo? Like 0? You should probably also change your <? tags to <?php Quote Link to comment Share on other sites More sharing options...
trq Posted December 21, 2007 Share Posted December 21, 2007 One concern may simply be the fact your using short tags. Also, you need to check the form was actually submitted to avaoid undefined variable warnings. <form method="post"> Forname: <input type="text" name="query"> <input type="Submit" name="Submit" value="Search"> </form> <?php if (isset($_POST['Submit'])) { $query = mysql_real_escape_string($_POST['query']); $hostname = ; $username = ; $password = ; $usertable = ; $dbName = ; mysql_connect($hostname,$username,$password) || die('Error connecting to mysql'); mysql_select_db($dbName); $sql = "SELECT * FROM Contacts WHERE Forname='$query'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_array($result,MYSQL_NUM)) { foreach($row as $k => $v) { echo "$k = $v<br />"; } } } else { echo "No results found"; } } else { echo "Query failed<br />" . mysql_error() . "<br />$sql"; } } ?> What does that produce? Quote Link to comment Share on other sites More sharing options...
pure_skill_2000 Posted December 21, 2007 Author Share Posted December 21, 2007 wow that seemed to work thorpe! Anyway I can change the numbers to the headings of the colunms? Thanks again for the help!!!!! Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 21, 2007 Share Posted December 21, 2007 Mark the topic solved please, I can't help it but I keep coming here to help you, but your problem is solved. Quote Link to comment Share on other sites More sharing options...
trq Posted December 21, 2007 Share Posted December 21, 2007 Anyway I can change the numbers to the headings of the colunms? while ($row = mysql_fetch_assoc($result)) { foreach($row as $k => $v) { echo "$k = $v<br />"; } } Quote Link to comment Share on other sites More sharing options...
pure_skill_2000 Posted December 21, 2007 Author Share Posted December 21, 2007 Might sound abit daft but I dont know how to change that, could you poss show me an example if my headings where a, b and c? Many thanks 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.