Multiple Issues (MySQL and text field searching)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Multiple Issues (MySQL and text field searching)

Post by ThatPerson »

I am having multiple issues with a sort of social network mixed with a online jotter. I am currently making a search bar to search for friends, and a thing to add friends who have sent you a friend request.

Search Bar

Code: Select all

<?php
ob_start();
include("database.php");
echo <<<END
<div>
<FORM NAME ="form1" METHOD ="POST" ACTION = "search.php">
<INPUT TYPE = "Text" VALUE ="" NAME = "search">

<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">
</div>
END;
if (isset($_POST['Submit1'])) {

$term = $_POST['search'];

$term2 = $term.'%';

$search = mysql_query('SELECT Username FROM notes_notes WHERE Username LIKE "$term2"') or die(mysql_error());
$user = $_COOKIE['loggeduser'];
$search2 = mysql_fetch_array($search) or die(mysql_error());
$friends = mysql_query("select friends from notes_notes where email like '$user'");
$friends2 = mysql_fetch_object($friends);
$friends3 = $friends2->friends;
$friendreq = mysql_query("select friendrequests from notes_notes where email like '$user'");
$friendreq2 = mysql_fetch_object($friendreq);
$friendreq4 = $friendreq2->friendrequests;


echo $term;

foreach ($search as &$value) {
$search4 = $value->Username;
$desc = mysql_query("select description from notes_notes where Username like '$value'");
echo $value;
$desc2 = mysql_fetch_object($desc);
$desc3 = $desc->description;

if (strpos($friends3,$value) == false) {
echo <<<END
    <div style="height:30; width:300;">
    $value
    <br>
    $desc3
    <input type="Submit" value="Send friend request" name = "friend">
    </div>
END;
}
else
{
echo <<<END
    <div style="height:30; width:300;">
    $value
    <br>
    $desc3
    <input type="Submit" value="View Profile" name = "only">
    </div>
END;
}
}
unset($value);

} else { 
echo "Failed";
}
?>
Above is the code used for the search bar which should search through the database for names which begin with the text in the query (Typing Pine would come up with Pinetrees, Pineapple, etc.) and then print these in dividers which also contain a description of them and, if they are not friends, an add to friends button, or a view profile button. When this is run with a username from the database, such as my two test users (Jeremy and Bethany (Nothing to do with me in real life), it returns nothing, wether it is 'Jer' (For Jeremy) or Bethany (For Bethany). Am I missing a vital part needed?

Friend viewer

This is probably much simpler, the update line in my add friends page is not running. The code is:

Code: Select all

$usr = $_COOKIE['loggeduser'];
$pwd = $_COOKIE['loggedpass'];

Code: Select all

$a = 0;
for ($i = 1; $i <= $p; $i++) {
 $a = $a + 1;
 $name = "submit".$a;
  if (isset($_POST[$name])) {
   $num = "$p".$a."pos";
   $len = "$p".$a."len";
   substr_replace($fr,"",${"p".$a."pos"},${"p".$a."len"}+1);
   $oldfriends = mysql_query("select friends from notes_notes where email = '$usr' and Password = '$pwd'");
   $old = $oldfriends.substr($fr,${"p".$a."pos"},${"p".$a."len"}).","
   mysql_query("UPDATE notes_notes SET friends = '$old' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
  }
}
And the error message is

[text]Parse error: syntax error, unexpected T_STRING on line 68[/text]

(Please note, the lines posted are 58-70, the error line is

Code: Select all

mysql_query("UPDATE notes_notes SET friends = '$old' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
)

Can anybody help on either of the issues?
Last edited by ThatPerson on Fri Aug 05, 2011 1:24 pm, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Multiple Issues (MySQL and text field searching)

Post by social_experiment »

Code: Select all

$oldfriends = mysql_query("select friends from notes_notes where email = '$usr' and Password = '$pwd'");
#try
$oldfriends = mysql_query("select friends from notes_notes where email = '" . $usr . "' and Password = '" . $pwd . "' ");
Note: The error you receive doesn't always indicate the problematic line, sometimes the error occurs prior to the given line. If the example i gave is successful, look at the second query as well because it could return a similar error. :idea: Escape any input with mysql_real_escape_string();
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Multiple Issues (MySQL and text field searching)

Post by califdon »

With the SQL predicate 'LIKE', you must use a wildcard character, '%', such as:

Code: Select all

SELECT Username FROM notes_notes WHERE Username LIKE "$term2%"'
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: Multiple Issues (MySQL and text field searching)

Post by ThatPerson »

I tried your method yet to no avail: the error message simply turned into:

Code: Select all

Parse error: syntax error, unexpected T_VARIABLE on line 68
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: Multiple Issues (MySQL and text field searching)

Post by ThatPerson »

Califdon, you possibly missed out this line: [text]$term2 = $term.'%';[/text]. Thanks for the help anyway.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Multiple Issues (MySQL and text field searching)

Post by social_experiment »

Code: Select all

<?php
$old = $oldfriends.substr($fr,${"p".$a."pos"},${"p".$a."len"}).","
# missing a semi-colon at the end of the line.
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: Multiple Issues (MySQL and text field searching)

Post by ThatPerson »

I fixed the Friend request one, the line above had no ';'. The search one however is still broken.

Edit: Late post
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: Multiple Issues (MySQL and text field searching)

Post by ThatPerson »

Ah, just found another error in the friend request one, it does not update either table.

Code: Select all

<?php
include("database.php");

$usr = $_COOKIE['loggeduser'];
$pwd = $_COOKIE['loggedpass'];
$fr = mysql_fetch_object(mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'")) -> friendrequests;

$user = mysql_query("select count(*) as count from notes_notes where email = '$usr' and Password = '$pwd'");
$user2 = mysql_fetch_object($user);
$user3 = $user2 -> count;
$oldOutPut = 0;

if ($user3 >= 1) {

if ($fr != "") {
 $p = 0;
 $offset = 0;
 $len = strlen($fr);
 $ppp = "p".$p;
 $ppl = "p".$p."len";
 $ppo = "p".$p."pos";
 for ($i = 1; $i <= $len; $i++) {
  if (strpos($fr,",",$offset) != false) {
   $offset = strpos($fr,",",$offset)+1;
   $p = $p + 1;
   $outPut = $offset - $oldOutPut;
    $friend = substr($fr,$oldOutPut,$outPut);
    ${$ppp} = $friend;
    ${$ppo} = 0;
    ${$ppl} = strlen($friend);
    $oldOutPut = $offset;

echo <<<END
<div style="width:100%;height:10%;">
$friend
<br>
<input type="Submit" name="submit$p" value="Add as friend">
<input type="Submit" name="cancel$p" value="Not now">
</div>
END;
   }
  }
 }

else {
echo <<<END
<div style = "width:50%;height:100%">
Sorry, you have no friend requests
</div>
END;

}
}
$a = 0;
for ($i = 1; $i <= $p; $i++) {
 $a = $a + 1;
 $name = "submit".$a;
  if (isset($_POST[$name])) {
   $num = "$p".$a."pos";
   $len = "$p".$a."len";
   substr_replace($fr,"",${$num},${$len}+1);
   $test = mysql_query("select friendrequests from notes_notes where email = '$usr' and Password = '$pwd'");
   $test2 = substr_replace($test,"",${$num},${$len}+1);
   echo $test2;
      mysql_query("UPDATE notes_notes SET friendrequests = '$test2' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
   $oldfriends = mysql_query("select friends from notes_notes where email = '$usr' and Password = '$pwd'");
   $old = $oldfriends.substr($fr,${"p".$a."pos"},${"p".$a."len"}).",";
   echo $old;
   mysql_query("UPDATE notes_notes SET friends = '$old' WHERE email = '$usr' and Password = '$pwd'") or die(mysql_error());
  }
}
?>
I do not think it is detecting the button being clicked, however I can think of no other way of going through all of the possible names. Any ideas?


Oh, the search engine one still needs help.
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: Multiple Issues (MySQL and text field searching)

Post by ThatPerson »

A new problem I came across, along with the others: When I do an include for each item so you can choose which are on your notepad, the friend one does not work, and all the other buttons redirect to the search one. Any ideas? :banghead:
Post Reply