NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP help needed

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 03-24-2011, 07:31 AM THREAD STARTER               #1 (permalink)
NamePros Regular
 
cipcip's Avatar
Join Date: Mar 2007
Location: Constanta, Romania
Posts: 504
cipcip is just really nicecipcip is just really nicecipcip is just really nicecipcip is just really nice
 



PHP help needed


Ok, so I am creating a like system like the one on youtube or facebook and so on. I have a problem, the code is not working, I do not know what I am doing wrong since I don't get any errors and I am stuck like this for 2 days now. The values aren't inserted in the database.

PHP Code:
<?php
echo "<a href='{$_SERVER['PHP_SELF']}?vot=up&idpost=$idpost'";
?>
onclick="return confirm('Multumim pentru votul pozitiv!')"
<?php
echo'><img src="http://wac.658e.edgecastcdn.net/80658E/namepros/images/up.png" alt="Vote UP" /></a>';
?> <?php
echo "<a href='{$_SERVER['PHP_SELF']}?vot=down&idpost=$idpost'";
?>
onclick="return confirm('Ne pare rau ca nu ti-a placut acest post!')"
<?php
echo'><img src="http://wac.658e.edgecastcdn.net/80658E/namepros/images/down.png" alt="Vote DOWN" /></a>';

if(!isset(
$_GET['vot'])) $_GET['vot'] = '';

switch(
$_GET['vot'])
{
case 
'':
echo 
'';
break;

case 
'up':

$query12="SELECT idlike, up, down, idpost FROM like INNER JOIN posts ON like.idpost=posts.idpost WHERE idpost=$idpost";
????: NamePros.com http://www.namepros.com/programming/708130-php-help-needed.html
$result12 mysql_query($query12);
if (
$row=mysql_fetch_assoc($result12))
????: NamePros.com http://www.namepros.com/showthread.php?t=708130
{
//UPDATE
$increment $row['up']+1;
$query13 "UPDATE like SET up=$increment WHERE idpost=$idpost";
$result13 mysql_query($query13);
}
else
{
//INSERT
$implicit=1;
$query21 "INSERT INTO like (idlike, idpost, up) VALUES ('', '$idpost', '$implicit')";
$result21 mysql_query($query21);
}

break;

case 
'down':

$query14="SELECT idlike, up, down, idpost FROM like INNER JOIN posts ON like.idpost=posts.idpost WHERE idpost=$idpost";
$result14 mysql_query($query14);
if (
$row=mysql_fetch_assoc($result14))
{
//UPDATE
$increment2 $row['down']+1;
$query15 "UPDATE like SET down=$increment2 WHERE idpost=$idpost";
$result15 mysql_query($query15);
}
else
{
//INSERT
$implicit2=1;
$query22 "INSERT INTO like (idlike, idpost, down) VALUES ('', '$idpost', '$implicit2')";
$result22 mysql_query($query22);
}

break;
}
?>
Thanks in advance!
cipcip is online now   Reply With Quote
Old 03-24-2011, 08:39 AM   #2 (permalink)
Account Suspended
Join Date: Dec 2008
Location: Boston, Ma
Posts: 650
CrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to allCrackFeed.Com is a name known to all
 



Marrow Donor Program Animal Rescue Autism Autism
First, this is sloppy code. Really ugly. Here is the right way to do it.

You need to have a column for up and a column for down. Your code makes no sense. This is just a quick throw together but should get you closer.

PHP Code:

<?php

ini_set
("display_errors"1);
error_reporting(2047);

echo 
'<a href="'.$_SERVER['PHP_SELF'].'?vot=up&idpost='.$idpost.'" onclick="return confirm(\'Multumim pentru votul pozitiv!\')"><img src="http://wac.658e.edgecastcdn.net/80658E/namepros/images/up.png" alt="Vote UP" /></a><a href="'.$_SERVER['PHP_SELF'].'?vot=down&idpost='.$idpost.'" onclick="return confirm(\'Ne pare rau ca nu ti-a placut acest post!\')"><img src="http://wac.658e.edgecastcdn.net/80658E/namepros/images/down.png" alt="Vote DOWN" /></a>'

if (!
function_exists('isNaturalNumber')) {
    function 
isNaturalNumber($val$acceptzero false) {
      if (empty(
$val) || $val==null || $val=='' || $val==0) {
          return 
FALSE;
      }
     
$return = ((string)$val === (string)(int)$val);
     if (
$acceptzero)
      
$base 0;
     else
      
$base 1;
????: NamePros.com http://www.namepros.com/showthread.php?t=708130
     if (
$return && intval($val) < $base)
      
$return FALSE;
     return 
$return;
    }
}

// isset does not check for null values, so use empty()
// also parse in the id and make sure it is a natural number and not a bad string

if(!empty($_GET['vot']) && !empty($_GET['idpost']) && isNaturalNumber($_GET['idpost'])) {
    
$idpost $_GET['idpost'];
    if (
$_GET['vot'] == 'up' || $_GET['vot'] == 'down') {
              
$result14 mysql_query('SELECT like.idlike,like.up,like.down,like.idpost FROM `like` INNER JOIN `posts` ON like.idpost=posts.idpost WHERE `idpost`=' $idpost) or die(mysql_error()); 
              if (
mysql_num_rows($result14) != 0) {
                  
//UPDATE
                  
if ($_GET['vot'] == 'up') {
????: NamePros.com http://www.namepros.com/showthread.php?t=708130
                      
$result15 mysql_query('UPDATE `like` SET `up`=(up+1) WHERE `idpost`=' $idpost) or die(mysql_error()); 
                  } else {
                      
$result15 mysql_query('UPDATE `like` SET `down`=(down+1) WHERE `idpost`=' $idpost) or die(mysql_error()); 
                  }
              } else {
                  
//INSERT 
                  
if ($_GET['vot'] == 'up') {
                      
$result15 mysql_query('INSERT INTO `like` SET `up`=(up+1), `idpost`=' $idpost) or die(mysql_error()); 
                  } else {
                      
$result15 mysql_query('INSERT INTO `like` SET `down`=(down+1), `idpost`=' $idpost) or die(mysql_error()); 
                  }
              }
    }
}
?>


---------- Post added at 11:39 AM ---------- Previous post was at 11:37 AM ----------

Always validate every variable. You never know what the data they submit REALLY is. Best of luck bud
CrackFeed.Com is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Residence Coder Needed xrvel Web Development Wanted 1 02-04-2009 07:48 AM
Dan's snippets Daniel Programming 4 12-20-2008 03:12 AM
PHP programmer/partner needed nailek Web Development Wanted 0 06-21-2006 02:18 AM
PHP and MYSQL help needed unknowngiver Programming 13 06-15-2006 09:41 PM

 
All times are GMT -7. The time now is 02:45 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger