Question:
What is wrong with my php script?
?
2010-11-26 23:07:51 UTC
I am trying to created a search by categories where users can search other users by name, job, etc... my form in html is:


Job/Service







and my php script for it is:

// Start_session, check if user is logged in or not, and connect to the database all in one included file
include_once("scripts/checkuserlog.php");
?>
// Connect to database
include_once "scripts/connect_to_mysql.php";
// DEAFAULT QUERY STRING
$queryString = "WHERE email_activated='1' ORDER BY id ASC";
// DEFAULT MESSAGE ON TOP OF RESULT DISPLAY
$queryMsg = "Senior to Newest users";
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////// SET UP FOR SEARCH CRITERIA QUERY SWITCH MECHANISMS
if ((@$_POST['listByq'] == "by_job")) {


$xjob = @$_POST['xjob'];
$xjob = stripslashes($xjob);
$xjob = strip_tags($xjob);
$xjob = eregi_replace("`", "", $xjob);
$xjob = mysql_real_escape_string($xjob);
$queryString = "WHERE job LIKE '%$xjob%' AND email_activated='1'";
$queryMsg = "Showing Members with the Job/Service you searched for";
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// QUERY THE MEMBER DATA USING THE $queryString variable's value
$sql = mysql_query("SELECT id, username, firstname, lastname, job, country, state, city, zip FROM myMembers WHERE email_activated='1' ORDER BY id ASC");

$outputList = '';
while($row = mysql_fetch_array($sql2)) {

$id = $row["id"];
$username = $row["username"];
$firstname = $row["firstname"];
if (!$firstname) {
$firstname = $username;
}
$lastname = $row["lastname"];
$job = $row["job"];
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$zip = $row["zip"];
$country = $row["country"];
$outputList .= '







' . $firstname . ' ' . $lastname . ' ' . $state . ' ' . $city . ' ' . $zip . ' ' . $job . '



';


}
?>
Three answers:
anonymous
2010-11-27 01:13:56 UTC
Apart from above updates i think you need to change some thing like this as well.



For the line containing below text :

$sql = mysql_query("SELECT id, username, firstname, lastname, job, country, state, city, zip FROM myMembers WHERE email_activated='1' ORDER BY id ASC");



Replace it with below one



$FinalQueryString = "SELECT id, username, firstname, lastname, job, country, state, city, zip FROM myMembers ";

$FinalQueryString .= $queryString;

$sql = mysql_query($FinalQueryString);
dgood1
2010-11-27 07:43:59 UTC
1. Inside second php tag, there is an instance of include_once that does NOT use the string as an argument. Written:

include_once "scripts/connect_to_mysql.php";



2. Numeric values must not be inside quotes. Written:

$queryString = "WHERE email_activated='1' ORDER BY id ASC";



3. Address of? I think that '@' symbol is a typo. Written:

$xjob = @$_POST['xjob'];



4. Undefined variable found: $sql2. $sql was declared though... Written:

while($row = mysql_fetch_array($sql2)) {



5. Undefined variable found: $userpic. Written:

target="_self">' . $user_pic . '




Good luck...
Jim
2010-11-27 07:32:11 UTC
that overflow:hidden wrecks havoc on firefox during printing (truncates printing). find another w3ay to do that if possible.

you might want to use ILIKE instead of LIKE. ILIKE is case-insensitive.

you did not do mysql_free_result($sql); at the end.

$xjob = @$_POST['xjob'];

should be

$xjob = isset($_POST['xjob'])?$_POST['xjob']:"";

(fancy if statement)

you don't do anything with outputList after the loop. try echoing it at the end.

you don't do anything with $xjob.

you don't do anything with $queryString (none of them).

you don't do anything with $queryMsg (none of those either).

and I am not sure, but I think only double-quotes can span multiple lines (maybe that's only a C++ rule and it doesn't apply to PHP - I use many languages).

I can't do any more debugging because I don't have your database.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...