Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am having some trouble with some 1

Status
Not open for further replies.

webmastadj84

IS-IT--Management
Aug 23, 2006
86
US
I am having some trouble with some PHP code. It will not do what I need it to do which is get information from a database, add a count to it, then update the record in the database. The select works and the addition works, but it will not update the record...any suggestions?

Here is the code:
$sql = "SELECT `Count` FROM `counters` WHERE `Site`='1'";
$result = mysql_query($sql);
$newArray = mysql_fetch_array($result);
$CountNumber = $newArray['Count'];

echo "$CountNumber";

$CurrentCount = $CountNumber + 1;

echo "$CurrentCount";

$sql2 = "UPDATE `counters` SET `Count`=\'$CurrentCount\' WHERE `Site`=\'1\'";
mysql_query($sql2);
 
Why do you have backslashes infront of the single quotes in
Code:
$sql2 = "UPDATE `counters` SET `Count`=\'$CurrentCount\' WHERE `Site`=\'1\'";

You should always check for mysql errors. They will usually tell you what is wrong:
Code:
   $sql = "SELECT `Count` FROM `counters` WHERE `Site`='1'";
   $result = mysql_query($sql) or die("Problem with the query: $sql<br>" . mysql_error());
   $newArray = mysql_fetch_array($result);
   $CountNumber = $newArray['Count'];

   echo "$CountNumber<br>";
   
   $CurrentCount++;

   echo "$CurrentCount<br>";

   $sql2 = "UPDATE `counters` SET `Count`='$CurrentCount' WHERE `Site`='1'";
   mysql_query($sql2) or die("Problem with the update query: $sql2<br>" . mysql_error());

Ken
 
Hi,

I can post some code I made in y2k5, a link-script with an out-file.

Code:
mysql_connect($server, $db_user, $db_pass) or die ("CONNECT Error");


	// gather link
	$result=mysql_db_query($database, "SELECT * FROM `discount` WHERE `discount_id` = '{$id}'") or die("Database SELECT Error");
	$c_totals  = mysql_num_rows($result);

	if ($c_totals > 0)
	{
	// increment click_out
	[b]$result2=mysql_db_query($database, "UPDATE `discount` SET `discount_clicks_out` = '1' + `discount_clicks_out` WHERE `discount_id` = '{$id}' LIMIT 1") or die("Database SELECT Error");[/b]
	
	
	while ($row = mysql_fetch_array($result)) 
	  {
		$goto = $row['discount_link'];
		if (substr($goto, 0, 7) != "[URL unfurl="true"]http://")[/URL]
		  {
			$goto = "[URL unfurl="true"]http://"[/URL] . $goto; 
		  }
		header("Location: {$goto}/"); 
                                echo "Wait 5 seconds or <a href=\"$goto\">Click here to load new page...</a>";
	  } // end while
	} // end if $c_totals > 0
else
  {
	echo "Sorry, this page cannot be viewed";
  }

ps. this code is OLD, so you might want to secure it..
Also it is just a part of a script I no longer use, so.. yeah.. My point was the update query, as you see it does not need you to first select and output the values in a variable, when you can simply update the field in the SQL!

Faster and more secure!

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top