NameSilo

PHP help needed

SpaceshipSpaceship
Watch

cipcip

Top Member
Impact
1,585
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:
<?php
echo "<a href='{$_SERVER['PHP_SELF']}?vot=up&idpost=$idpost'";
?>
onclick="return confirm('Multumim pentru votul pozitiv!')"
<?php
echo'><img src="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="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";
$result12 = mysql_query($query12);
if ($row=mysql_fetch_assoc($result12))
{
//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!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains — AI StorefrontUnstoppable Domains — AI Storefront
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:
<?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://www.namepros.com/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://www.namepros.com/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;
	 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') {
	      			$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:)
 
0
•••
CatchedCatched

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back