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]; } } ?> Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/ 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] Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420051 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 Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420056 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. Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420060 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 Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420061 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"> Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420064 Share on other sites More sharing options...
trq Posted December 21, 2007 Share Posted December 21, 2007 Post your current code. Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420066 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; Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420068 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; } ?> Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420075 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 Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420078 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? Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420081 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!!!!! Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420084 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. Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420087 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 />"; } } Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420095 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 Link to comment https://forums.phpfreaks.com/topic/82607-solved-anyone-know-why/#findComment-420104 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.