Jump to content

MySQL SELECT MAX...AS statement


Shaun13

Recommended Posts

I have a small question about a MySQL Query:

 

Here is a small section of code from my script:

 

elseif($action == "reply")
	{
	$topicid=$_POST['topicid'];
	$sql8="SELECT MAX(answerid) AS Maxanswerid FROM $tablereplies WHERE questionid='$topicid'";
	$result8=mysql_query($sql8);
	$rows8=mysql_fetch_array($result8);

	if ($rows8) 
		{
		$Max_id = $rows8['Maxanswerid']+1;
		}
	else 
		{
		$Max_id = 1;
		}
	$myusername=$_SESSION['myusername'];
	$mydisplayname=$displayname;
	$content=$_REQUEST['content'];
	$author=$myusername;
	$authordisplay=$displayname;
	$datetime=date("d/m/y H:i:s"); // create date and time 

	$sql9="INSERT INTO $tablereplies(questionid, answerid, author, authordisplay, content, datetime)VALUES('$topicid', '$Max_id',  '$author', '$authordisplay', '$content', '$datetime')";
        $result9=mysql_query($sql9);

	if($result9)
		{
		echo "<br><br>Successful<BR>";
		echo "<a href='index.php?viewtopic=".$topicid."'>View your answer</a>";

		$sql10="UPDATE $tableposts SET replies='$Max_id' WHERE id='$topicid'";
		$result10=mysql_query($sql10);


		$memberid1="SELECT posts FROM $tablemembers WHERE username='$myusername'";
		$memberid1result=mysql_query($memberid1);
		$maxposts1="SELECT MAX(posts) AS Maxposts FROM $tablemembers WHERE id='$memberidresult'";
		$results10=mysql_query($maxposts1);
		$rows10=mysql_fetch_array($results10);
		$maxpostresults2=$rows10['posts']+1;
		$sql12="UPDATE $tablemembers SET posts='$maxpostresults2' WHERE username='$myusername'";
		mysql_query($sql12);
		}
	else 
		{
		echo "ERROR";
		}

	}
}

 

And I don't get any errors, but it doesn't enter into the database correctly. Any ideas?

 

~Shaun

Link to comment
https://forums.phpfreaks.com/topic/50741-mysql-select-maxas-statement/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.