Jump to content


Photo

searching using php and mySQL (newbie)


  • Please log in to reply
3 replies to this topic

#1 gingercrock

gingercrock
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 22 March 2006 - 02:17 PM

Hey guys, i am having problems while searching for members on my website.. i have a form that users fill in before being able to click on a 'search' button.. which works and the code is:

<form method = "POST" action="searchName.php" onSubmit="returnverifyform(this)">

<br>First name: <input type ="text" name = "first">
<font color = "black"></font>

<br>Last name: <input type = "text" name = "second">
<font color="black"></font>

</select><input type="submit" value = "Search">


it then opens 'searchName.php' which is meant to search through a database that i have created online.. the code is below:

<?php
$connection = mysql_connect("sentinel.cs.cf.ac.uk","user_name",
"password");

$first=$_POST['first'];
$second=$_POST['second'];


mysql_select_db("sjcdb",$connection) or die("failed!");
echo $first;
echo $last;
$query = "SELECT * FROM info WHERE first='".$first."' AND last ='".$second."'";
echo $query;
$result = mysql_query($query, $connection);

while($row = mysql_fetch_row($result)) {

$title = $row[2];
$first = $row[3];
$last = $row[4];


echo "$title." ".$first." ".$last.";

}

mysql_close($connection);
?>


However, the screen simply is left blank.. any ideas where im going wrong? i am a newbie im afraid so i apologise for any schoolboy errors!!
thanks for your time
steven

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 22 March 2006 - 02:22 PM

remove your server, username and password from your example of the code you use... otherwise you leave yourself wide open to attack from someone not so nice as me.


Try this:
$query = "SELECT * FROM info WHERE first='$first' AND last ='$second'";

wisewood: proven fact, I am both wise, and wooden.

#3 gingercrock

gingercrock
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 22 March 2006 - 02:56 PM

thanks, the first post has been edited. the new code still leaves the screen blank unfortuantly. i have entered some more code so now we have :

<?php
$connection = mysql_connect("sentinel.cs.cf.ac.uk","User_Name",
"PASSWORD");

$first=$_POST['first'];
$second=$_POST['second'];


mysql_select_db("sjcdb",$connection) or die("failed!");
echo $first;
echo $last;
$query = "SELECT * FROM info WHERE first='$first' AND last ='$second'";
echo $query;
$result = mysql_query($query, $connection);

while($row = mysql_fetch_row($result)) {

$title = $row[2];
$first = $row[3];
$last = $row[4];

echo $title." ".$first." ".$last.;
}

mysql_close($connection);
?>

If it helps, MySQL database has field names 'title', 'first' and 'last' and there is a field before this which is an increment 'ID'. title,first and last are in rows 1,2 and 3 respectively.. i had it working a day or two ago but tryed to changed around abit of code, got it wrong and the back up has been wiped!! so frustration is at its highest!
thanks for your help, i hope this information is of some use!
Steven

#4 snackiesmores

snackiesmores
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 22 March 2006 - 03:44 PM

In your first page that calls the search page i see this:

[!--coloro:#CC0000--][span style=\"color:#CC0000\"][!--/coloro--]</select><input type="submit" value = "Search">[!--colorc--][/span][!--/colorc--]

I'm still learning PHP myself, but what is that </select> tag for?

Also, you changed

[!--coloro:#CC0000--][span style=\"color:#CC0000\"][!--/coloro--]echo "$title." ".$first." ".$last.";[!--colorc--][/span][!--/colorc--]

to

[!--coloro:#CC0000--][span style=\"color:#CC0000\"][!--/coloro--]echo $title." ".$first." ".$last.;[!--colorc--][/span][!--/colorc--]

What is the intended style of this statement?

Mr. John.Doe
Mr. John Doe
Mr.John.Doe
.
.
.

?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users