Jump to content

Decrementing not working?


downah

Recommended Posts

Hi guys, I have written this code, it might look messy but I am learning so I don't really mind that but my main concern is that it is not working, could anyone help me out?

 

I am using an array to "unsubscribe" from multiple events at once, now I also need to decrement the amount of participants in that event, the code is working I am able to unsubscribe from events but the currentparticipants field is not updating.. any idea why?

 

This is the code that runs when you click unsubscribe :

 

if(isset($_POST['eventsID']) && count($_POST['eventsID']))
{
$eventsID = $_POST['eventsID'];

    //Ensure values are ints
    $deleteIDs = implode(', ', array_map('intval', $_POST['eventsID']));
    $query = "DELETE FROM Eventparticipants WHERE eventsID IN ({$deleteIDs})";
    //Debug line
echo '';
    mysql_query($query) or die(mysql_error());
    $result5=mysql_query("SELECT maxparticipants, currentparticipants FROM Events WHERE eventsID='$eventsID'");
$row5=mysql_fetch_assoc($result5);

$currentparticipants = $row5['currentparticipants'];
$maxparticipants = $row5['maxparticipants'];

$newcurrentparticipants = $currentparticipants--;



$sql="UPDATE Events SET currentparticipants='$newcurrentparticipants'
WHERE eventsID='$eventsID'";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }

}

 

And here is the code where you actually click unsubscribe

 

     $result2 = mysql_query("SELECT * FROM Eventparticipants WHERE participant = '$username'")
or die(mysql_error());

echo '</br><b>You are currently taking place in these events: </b></br>';
echo "<form action='' method='POST' onsubmit='return executeOnSubmit();'>";
echo '<table border="0">';
while($row2 = mysql_fetch_array($result2))
{


echo '<tr>';
echo '<td><a href="showevent.php?eventsID=';
echo $row2['eventsID'];
echo '">';
echo $row2['eventdescriptionheader'];
echo '</a></td>';


echo "<td><input type='checkbox' name='eventsID[]' value='{$row2['eventsID']}' /></td>";
echo '</tr>';


}
echo '</table>';
      echo "</br><input type='submit' value='Unsubscribe to event(s)' name='delete' />";
        echo '</form>';

 

 

And here is the full script in one

<?php
include 'connect.php';
include_once "markdown.php";
session_start();

if(!isset($_SESSION['username']))
{
     	echo 'You have to be a registered member to be able to view your space.<br><br> <a href="register.html">Click here to register</a>';
     	       	echo '<br><br><br><br>Or if you are already a member, please login to use this area.<br>';
       	echo '
       	<form method="POST" action="loginverification.php">
       	<table border="0">
       	<tr><td>
  Username: </td><td><input type="text" name="username" size="15" /></td></tr>
  <tr><td>Password:</td><td> <input type="password" name="password" size="15" /></td></tr>
  </table>
  <div align="center">
    <p><input type="submit" value="Login" /></p>
  </div>
</form>';
}
else{

if(isset($_POST['eventsID']) && count($_POST['eventsID']))
{
$eventsID = $_POST['eventsID'];

    //Ensure values are ints
    $deleteIDs = implode(', ', array_map('intval', $_POST['eventsID']));
    $query = "DELETE FROM Eventparticipants WHERE eventsID IN ({$deleteIDs})";
    //Debug line
echo '';
    mysql_query($query) or die(mysql_error());
    $result5=mysql_query("SELECT maxparticipants, currentparticipants FROM Events WHERE eventsID='$eventsID'");
$row5=mysql_fetch_assoc($result5);

$currentparticipants = $row5['currentparticipants'];
$maxparticipants = $row5['maxparticipants'];

$newcurrentparticipants = $currentparticipants--;



$sql="UPDATE Events SET currentparticipants='$newcurrentparticipants'
WHERE eventsID='$eventsID'";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }

}



       $username = $_SESSION['username'];


       $result = mysql_query("SELECT * FROM Members WHERE username = '$username'")
               or die(mysql_error());
               while($row = mysql_fetch_array($result))
               {
                       echo '<b>' . $username . '</b></br></br>';


     echo '<img src="data:image/jpg/png/jpeg;base64,' . base64_encode( $row['image'] ) . '" height="150" />';
     echo '</br>';


$selfdescription = Markdown($row['selfdescription']);
                       echo $selfdescription;
                       echo '</br></br>';
echo '<table border="0">';

echo '<tr>';
echo '<td>';
echo 'Email: ';
echo '</td>';
echo '<td><b>';
echo $row['email'] . "</b>";
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>';
echo 'Date joined: ';
echo '</td>';
echo '<td><b>';
print date('d M Y', strtotime($row['datejoined']));
echo "</b>";
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>';
echo 'Last visit: ';
echo '</td>';
echo '<td><b>';
print date('d M Y', strtotime($row['lastvisit']));
echo "</b>";
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>';
echo 'First name: ';
echo '</td>';
echo '<td><b>';
echo $row['firstname'] . "</b>";
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>';
echo 'Family name: ';
echo '</td>';
echo '<td><b>';
echo $row['familyname'] . "</b>";
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>';
echo 'Date of birth: ';
echo '</td>';
echo '<td><b>';
print date('d M Y', strtotime($row['dob']));
echo "</b>";
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>';
echo 'Gender: ';
echo '</td>';
echo '<td><b>';
echo $row['gender'] . "</b>";
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>';
echo 'County: ';
echo '</td>';
echo '<td><b>';
echo $row['county'] . "</b>";
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>';
echo 'Postcode: ';
echo '</td>';
echo '<td><b>';
echo $row['postcode'] . "</b>";
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td>';
echo '</br>';
echo '<form action="editprofile.php">';
echo '<input type="submit" name="" value="Edit Profile">';
echo '</form>';
echo '</td>';
echo '</tr>';

echo '</table>';

               }

            $result2 = mysql_query("SELECT * FROM Eventparticipants WHERE participant = '$username'")
or die(mysql_error());

echo '</br><b>You are currently taking place in these events: </b></br>';
echo "<form action='' method='POST' onsubmit='return executeOnSubmit();'>";
echo '<table border="0">';
while($row2 = mysql_fetch_array($result2))
{


echo '<tr>';
echo '<td><a href="showevent.php?eventsID=';
echo $row2['eventsID'];
echo '">';
echo $row2['eventdescriptionheader'];
echo '</a></td>';


echo "<td><input type='checkbox' name='eventsID[]' value='{$row2['eventsID']}' /></td>";
echo '</tr>';


}
echo '</table>';
      echo "</br><input type='submit' value='Unsubscribe to event(s)' name='delete' />";
        echo '</form>';

                            $result4 = mysql_query("SELECT * FROM Events WHERE hoster = '$username'")
or die(mysql_error());

echo '</br><b>You are currently hosting these events: </b></br>';
echo "<form action='' method='POST'>";
echo '<table border="0">';
while($row4 = mysql_fetch_array($result4))
{


echo '<tr>';
echo '<td><a href="showevent.php?eventsID=';
echo $row4['eventsID'];
echo '">';
echo $row4['eventdescriptionheader'];
echo '</a></td>';



echo '</tr>';


}
echo '</table>';

        echo '</form>';

}


?>

 

Much appreciated!

Link to comment
Share on other sites

A simple way of fixing this (and reducing the amount of code and data you have) is to simply COUNT() (inside a query statement) the number of participants for any event. By storing a separate number, you are creating redundant data that now must be maintained.

Link to comment
Share on other sites

Thanks for the suggestion, I am have not used the count function before, but I need this field and there must be something small wrong with why its not updating? Anyone else have any ideas why it is not decreasing the current participants by one?

Link to comment
Share on other sites

Now using this but still the same problem, the currentparticipants is not decreasing by one, any ideas?

 

	if(isset($_POST['eventsID']) && count($_POST['eventsID']))
{
$eventsID = $_POST['eventsID'];

    //Ensure values are ints
    $deleteIDs = implode(', ', array_map('intval', $_POST['eventsID']));
    $query = "DELETE FROM Eventparticipants WHERE eventsID IN ({$deleteIDs})";
    //Debug line
echo '';
    mysql_query($query) or die(mysql_error());
    $result5=mysql_query("SELECT maxparticipants, currentparticipants FROM Events WHERE eventsID='$eventsID'");
$row5=mysql_fetch_assoc($result5);

$currentparticipants = $row5['currentparticipants'];




$b[] = $eventsID;

mysql_query("UPDATE Events SET `eventsID` = (`currentparticipants` - 1) WHERE `eventsID` IN(" . implode(',', $b) . ")") or trigger_error('MySQL Update Failed: ' . mysql_error());
}

Link to comment
Share on other sites

FIXED! I am getting a hang of this slowly but surely.

 

here is the working script  if it is any help for anyone else

 

if(isset($_POST['eventsID']) && count($_POST['eventsID']))
{
$eventsID = $_POST['eventsID'];

    //Ensure values are ints
    $deleteIDs = implode(', ', array_map('intval', $_POST['eventsID']));
    $query = "DELETE FROM Eventparticipants WHERE eventsID IN ({$deleteIDs})";
    //Debug line
echo '';
    mysql_query($query) or die(mysql_error());
    $result5=mysql_query("SELECT maxparticipants, currentparticipants FROM Events WHERE eventsID='$eventsID'");
$row5=mysql_fetch_assoc($result5);

$currentparticipants = $row5['currentparticipants'];


mysql_query("UPDATE Events SET `currentparticipants` = (`currentparticipants` - 1) WHERE `eventsID` IN ({$deleteIDs})") or trigger_error('MySQL Update Failed: ' . mysql_error());
}

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.