barryflood22 Posted April 10, 2007 Author Share Posted April 10, 2007 still not working :-( does anyone have any pre-written scripts i can alter? Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-226301 Share on other sites More sharing options...
per1os Posted April 10, 2007 Share Posted April 10, 2007 Alright my last stab. Enter Last Name: <input name="Array[name]" type="text" id="Array[name]"><BR> That does not make any sense, why make that an array???? Enter Last Name: <input name="name" type="text" id="name"><BR> Use that instead. as for the script <HTML> <HEAD></HEAD> <BODY> <p><a href="../index.php">< back</a></p> <p> <?php $name = strtolower(trim($_POST["name"])); $Host="localhost"; $User="root"; $Password=""; $DBName="customer"; $TableName="customer"; $Link = mysql_connect($Host, $User, $Password); $Query="SELECT * from $TableName Where LOWER(name) = '$name'"; $Result= mysql_db_query ($DBName, $Query, $Link) OR DIE(mysql_error()); WHILE ($Row = mysql_fetch_assoc($Result)){ echo "Name: " . $Row['name'] . ", " . $Row['address'] . "Phone: " . $Row['postcode'] . "<BR />"; } mysql_close ($Link); ?> </p> </BODY> </HTML> Fix that, you are assuming register_globals was on. This is the correct way. Also as a side note, I think it was noted at least 3 or 4 times to "Make sure $Array['name'] has a value" If it was printing a value, which I doubt it was, than that amazes me. Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-226307 Share on other sites More sharing options...
barryflood22 Posted April 10, 2007 Author Share Posted April 10, 2007 still not working, thanks anyway frost Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-226311 Share on other sites More sharing options...
anthylon Posted April 10, 2007 Share Posted April 10, 2007 Dear friend, I don't know what could be problem with your code. You should use commands echo, print... to check your code results (step by step). But, other guys already told you all of that. I decided to make real code for you. I have tested it. I made database like yours from screenshot. Just copy code to some file - for example: search_customers.php. Make changes for variables ($host, $uname, $pass, $db). I appologize if this is detailed too much. Well, here is code: <? if($_POST['txt_search']) { $host = 'localhost'; $db='customer'; $uname='root'; $pass='root'; $cnn = mysql_connect($host, $uname, $pass); if (!$cnn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db($db)) { echo "Unable to select $db: " . mysql_error(); exit; } //your query (if it's okay; check it with echo command - echo $sql to see if it's good) $sql = "SELECT * FROM customer WHERE customer_name= '" . $_POST['txt_search'] ."'"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } echo 'Results of your search: <br> '; $i = 1; while ($row = mysql_fetch_assoc($result)) { echo "$i. ${row['customer_name']}<br>"; $i++; } mysql_free_result($result); } //if form activated ?> <form name="form1" method="post" action=""> <input name="txt_search" type="text" id="txt_search" /> <label> <input name="cmd_search" type="submit" id="cmd_search" value="Search"> </label> </form> With that code you should be able to track your errors (if there is any) easier. Please note this query will get result only if you put real customer_name like in db. Check PHP&MySQL manual for advanced queries (with LIKE and simillar conditions). Also I have changed field's name from name to customer_name. I don't know what version of MySQL database you are using. Maybe that could be problem because of reserved names. Think about that. I wish goodluck to you. Please, if this solve your problem use button Solved to confirm that. Anthylon Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-226389 Share on other sites More sharing options...
clown[NOR] Posted April 10, 2007 Share Posted April 10, 2007 dude... dont call us gays anthylon ... hehe... did u mean guys? Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-226394 Share on other sites More sharing options...
anthylon Posted April 11, 2007 Share Posted April 11, 2007 Thank you for correcting me. I really do appologize. Yes, I wanted to say GUYS. Sorry, :'( English is not my native language. Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-226403 Share on other sites More sharing options...
clown[NOR] Posted April 11, 2007 Share Posted April 11, 2007 hehe... no problem shit happens Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-226408 Share on other sites More sharing options...
per1os Posted April 11, 2007 Share Posted April 11, 2007 Just a shot in the dark, do you mean to use the LIKE operator? IE: $sql = "SELECT * FROM customer WHERE customer_name LIKE '%" . $_POST['txt_search'] ."%'"; ?? Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-226460 Share on other sites More sharing options...
barryflood22 Posted April 11, 2007 Author Share Posted April 11, 2007 tried your code, still not working :-( doin my head in search.php ------------- <HTML> <HEAD></HEAD> <BODY> <form name="form1" method="post" action="searchresults.php"> <input name="txt_search" type="text" id="txt_search" /> <label> <input name="cmd_search" type="submit" id="cmd_search" value="Search"> </label> </form> </BODY> </HTML> searchresults.php -------------------- <HTML> <HEAD></HEAD> <BODY> <p><a href="../index.php">< back</a></p> <p> <? if($_POST['txt_search']) { $host = 'localhost'; $db='customer'; $uname='root'; $pass=''; $cnn = mysql_connect($host, $uname, $pass); if (!$cnn) { echo "Unable to connect to DB: " . mysql_error(); exit; } if (!mysql_select_db($db)) { echo "Unable to select $db: " . mysql_error(); exit; } $sql = "SELECT * FROM customer WHERE name= '" . $_POST['txt_search'] ."'"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } echo 'Results of your search: <br> '; $i = 1; while ($row = mysql_fetch_assoc($result)) { echo "$i. ${row['name']}<br>"; $i++; } mysql_free_result($result); } ?> </p> </BODY> </HTML> when i try and search this is what i get: '; $i = 1; while ($row = mysql_fetch_assoc($result)) { echo "$i. ${row['name']} "; $i++; } mysql_free_result($result); } ?> Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-226767 Share on other sites More sharing options...
anthylon Posted April 11, 2007 Share Posted April 11, 2007 I really don't understand you ???. Sorry, but that's impossible. Please, export your db in file, rar/zip both files and send to me via email ([email protected]). I'll check that. It seams your PHP parser didn't finished it job. That's crazy. I'm confused now. Waiting for your email. Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-227159 Share on other sites More sharing options...
barryflood22 Posted April 11, 2007 Author Share Posted April 11, 2007 hey anthylon, i got it working :-) thanks my mistake so it was Link to comment https://forums.phpfreaks.com/topic/46498-solved-search-sql-database-records/page/2/#findComment-227251 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.