Jump to content

Mysql updating row to blank value


Russia

Recommended Posts

Hey guys, I have a private message script but for some reason when its updating, it turns the value into a blank one from 1 to 3.

 

I have no idea why, but it works with the old statement of deleting, but I changed it to instead update it to 3 because I want to keep the pm in archives.

 

here is the delete PM part.

 

<?php
session_start();
header("Location:inbox.php");

$user = $_SESSION['username'];

include 'db.php';

//We do not have a user check on this page, because it seems silly to, you just send data to this page then it directs you right back to inbox

//We need to get the total number of private messages the user has
$sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'");
$row = mysql_fetch_array ($sql);
$pm_count = $row['pm_count'];
            
//A foreach loop for each pm in the array, get the values and set it as $pm_id because they were the ones selected for deletion
foreach($_POST['pms'] as $num => $pm_id)
    {
    //Delete the PM from the database
     mysql_query("UPDATE messages SET recieved='3' WHERE id='$pm_id' AND reciever='$user'");

    // mysql_query("DELETE FROM messages WHERE id='$pm_id' AND reciever='$user'");
    
    //Subtract a private message from the counter! YAY!
    //$pm_count = $pm_count - '1';
    
    //Now update the users message count with the new value
    //mysql_query("UPDATE users SET pm_count='$pm_count' WHERE username='$user'");
    }
?> 

 

The commented out part at the end, is the old deleting parts but instead changed it to update value.

 

Here is my form that shows checkboxes.

I showed the whole code, where it has comments.

 

<?php
    //This stuff and the while loop will query the database, see if you have messages or not, and display them if you do
    $query = "SELECT id, sender, subject, message FROM messages WHERE reciever='$user' AND recieved='1'";
    $sqlinbox = mysql_query($query);

    
    //We have a mysql error, we should probably let somone know about the error, so we should print the error
    if(!$sqlinbox)
        {
        ?>
        <p><?php print '$query: '.$query.mysql_error();?></p>
        <?php
        }
    
    //There are no rows found for the user that is logged in, so that either means they have no messages or something broke, lets assume them they have no messages
    elseif (!mysql_num_rows($sqlinbox) )
        {
        ?>
        <center><p><b>You havent read any messages</b></p></center>
        <?php
        }
    
    //There are no errors, and they do have messages, lets query the database and get the information after we make a table to put the information into
    else
        {
        //Ok, Lets center this whole table Im going to make just because I like it like that
        //Then we create a table 80% the total width, with 3 columns, The subject is 75% of the whole table, the sender is 120 pixels (should be plenty) and the select checkboxes only get 25 pixels
        ?>
        <center>
        <form name="send" method="post" action="delete.php">
        <table width="80%">
        <tr>
          <td width="75%" valign="top"><p><b><u>Subject</u></b></p></td>
          <td width="120px" valign="top"><p><b><u>Sender</u></b></p></td>  
          <td width="25px" valign="top"><p><b><u>Select</u></b></p></td>
        </tr>
        <?php
        //Since everything is good so far and we earlier did a query to get all the message information we need to display the information. 
        //This while loop goes through the array outputting all of the message information
        while($inbox = mysql_fetch_array($sqlinbox))
            {
            //These are the variables we get from the array as it is going through the messages, we have the id of the private message, we have the person who sent the message, we have the subject of the message, and yeah thats it
            $pm_id = $inbox['id'];
            $sender = $inbox['sender'];
            $subject = $inbox['subject'];
            
            //So lets show the subject and make that a link to the view message page, we will send the message id through the URL to the view message page so the message can be displayed
            //And also let the person see who sent it to them, if you want you can make that some sort of a link to view more stuff about the user, but Im not doing that here, I did it for my game though, similar to the viewmsg.php page but a different page, and with the senders id
            //And finally the checkboxes that are all stuck into an array and if they are selected we stick the private message id into the array
            //I will only let my users have a maximum of 50 messages, remeber that ok? Because that's the value I will later in another page
            //Here is finally the html output for the message data, the while loop keeps going untill it runs out of messages
            ?>
            <tr>
              <td width="75%" valign="top"><p><a href="viewmsg.php?msg_id=<?php echo $pm_id; ?>"><?php echo $subject; ?></a></p></td>
              <td width="120px" valign="top"><p><?php echo $sender; ?></p></td>
              <td width="25px" valign="top"><input name="pms[]" type="checkbox" value="<?php echo $pm_id; ?>"></td>
            </tr>
            <?php
            //This ends the while loop
            }
        //Here is a submit button for the form that sends the delete page the message ids in an array
        ?>
        
        
        <tr>  
    
        <td colspan="3"><input type="submit" name="Submit" value="Delete Selected"></td>
        <td></td>
        <td></td>
        
        
        
        
        </tr>
        
        
        
        
        </table>
        
        </center>
        <?php
        //So this ends the else to see if it is all ok and having messages or not
        }
?>

 

 

So yeah, does anyone know why it updates it to a blank value, from the value 1 in recieved?

Link to comment
Share on other sites

mysql_query("UPDATE messages SET recieved='3' WHERE id='$pm_id' AND reciever='$user'");

 

try:

 

mysql_query("UPDATE messages SET recieved='3' WHERE id='$pm_id' AND reciever='" . $_SESSION['username'] . "'") or die('Could not connect: ' . mysql_error());

 

Let's see what errors you get with the query!

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.