Jump to content

Add to 'likes' field in Database


nortksi

Recommended Posts

Hi guys, I am hoping someone can help me with this.  I am pretty new to PHP and MySQL but I am learning all the time; however I have become stuck.

 

I am developing a business directory and need to be able to let the user click a 'like' button which will then add to the total number of 'likes' for that business.  At the moment I am able to retrieve all businesses and output them to the screen, the current number of likes is also displayed.  The brief code for displaying th number of likes is:

 

<div class="pr_likes"><?php echo $sr[$ii]['number_likes'];?> likes</div>
<a href="#"><div class="pr_thumb"></div></a>

 

Underneath the pr_likes class is a div that displays a roll over 'thumbs up' image.  I realise that instead of this being a hyperlink it will most probably be a form?

 

If possible I want it to add to the total number of 'likes' without leaving the current page as the retrieved businesses are returned randomly and I do not want to scramble the results.

 

All help will be greatly appreciated.

Kind regards,

 

Mark.

Link to comment
Share on other sites

Ok, having reading up on the suggested material I have gave it a go.  However; I am unable to get this working.  This is what I have so far:

 

HTML FILE:

<script type="text/javascript" src="prototype.js"></script>
<script>

		function sendRequest() {
			new Ajax.Request("test.php", 
				{ 
				method: 'post', 
				postBody: '<?php $Company;?>'+$F('<?php $Company;?>')+'&<?php $Code;?>='+$F('<?php $Code;?>')
				onComplete: showResponse 
				});
			}

		function showResponse(req){
			$('pr_likes').innerHTML= req.responseText;
		}
</script>
<body>
<div class="pr_thumb">
        <?php $Code =  $sr[$ii]['postcode'];
         $Company = $sr[$ii]['company_name']; ?>
                                    
         <form id="test" onsubmit="return false;">
	<input type="hidden" name="<?php $Company;?>" id="<?php $Company;?>" >
                 <input type="hidden" name="<?php $Code;?>" id="<?php $Code;?>" >
                 <input type="submit" value="submit" onClick="sendRequest()">
</form>
</div>
</body>

 

TEST.PHP

<?php

$companyName=($_REQUEST['$Company']);
$postCode=($_REQUEST['$Code']);

// connect to the database
	mysql_connect("*****", "*****") or die(mysql_error()); 
	mysql_select_db("******") or die(mysql_error());

	mysql_query("UPDATE businesses SET number_likes = number_likes+1 WHERE company_name = '$companyName' AND postcode = '$postCode'");

?>

 

It is no doubt incorrect on so many levels.  Any helpful pointers will be greatly appreciated

 

Regards,

Mark.

Link to comment
Share on other sites

you will need the echo out your variables. and there is php in both scripts thus it must used .php extensions unless you parsed it in your server config.

 

index.php

        <?php $Code =  $sr[$ii]['postcode'];
         $Company = $sr[$ii]['company_name']; ?>
<script type="text/javascript" src="prototype.js"></script>
<script>

		function sendRequest() {
			new Ajax.Request("test.php", 
				{ 
				method: 'post', 
				postBody: '<?php echo $Company;?>'+$F('<?php echo $Company;?>')+'&<?php echo $Code;?>='+$F('<?php echo $Code;?>')
				onComplete: showResponse 
				});
			}

		function showResponse(req){
			$('pr_likes').innerHTML= req.responseText;
		}
</script>
<body>
<div class="pr_thumb">

                                    
         <form id="test" onsubmit="return false;">
	<input type="hidden" name="<?php echo $Company;?>" id="<?php echo $Company;?>" >
                 <input type="hidden" name="<?php echo $Code;?>" id="<?php echo $Code;?>" >
                 <input type="submit" value="submit" onClick="sendRequest()">
</form>
</div>
</body>

 

test.php

<?php

$companyName=($_REQUEST['$Company']);
$postCode=($_REQUEST['$Code']);

// connect to the database
	mysql_connect("*****", "*****") or die(mysql_error()); 
	mysql_select_db("******") or die(mysql_error());

	mysql_query("UPDATE businesses SET number_likes = number_likes+1 WHERE company_name = '$companyName' AND postcode = '$postCode'");
?>

Link to comment
Share on other sites

Hi ZulfadlyAshBurn, thanks so much for taking the time help me!

 

I have implemented the changes you suggested.  Unfortunately; it still does not work :(

 

I have checked the variable and field names which all appear correct.  Any other thoughts?

 

Kind regards,

Mark.

Link to comment
Share on other sites

is there any error? enable error reporting.

 

Hi.  Yes an error is reported:

 

Notice: Undefined variable: ii in /content/Hosting/p/l/web/eventresults.php on line 25

Notice: Undefined variable: sr in /content/Hosting/p/lweb/eventresults.php on line 25

Notice: Undefined variable: ii in /content/Hosting/p/l/web/eventresults.php on line 26

Notice: Undefined variable: sr in /content/Hosting/p/l/web/eventresults.php on line 26

 

These lines are:

 

$Code =  $sr[$ii]['postcode'];
$Company = $sr[$ii]['company_name']; 

 

I hope this can shed some light :)

 

Regards,

Mark.

Link to comment
Share on other sites

Hi ZulfadlyAshBurn, sorry for the late reply, I've been out of the office over the weekend.

 

I am assuming that the variables have been set as I can display the data being retrieved from my 'businesses' table in the MySQL database.  For example; I can display the company name and postcode that these variables hold.  Could it be something to do with where abouts in my code these variables are placed?

 

I am in a difficult position because I am signed into a non-disclosure agreement with my employers so I can not reveal to much of my code.

 

Kind regards,

Mark.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.