Jump to content

argh unworking code yet it runs through?


RaythMistwalker

Recommended Posts

if ($action == 'userstatus') {
   //Create Change
   if ($statusAction == "Enable") { $newStatus = 1; }
   if ($statusAction == "Disable") { $newStatus = 0; }
   $id = $_POST['id'];

   //Query
   $qry="UPDATE members SET sb_status='$newStatus' WHERE member_id='$id'";
   $update=mysql_query($qry);

   //Close mysql and redirect
   mysql_close();
   header("location: shoutbox-admin.php?action=users");
}

 

This is the enable/disable shoutbox for a user part of my shoutbox-admin.php

 

Basically: i click a button, and it tells this part of the script whether to enable or disable the user. HOWEVER: this script is just running through and going straight to the bottom line. any ideas?

Link to comment
https://forums.phpfreaks.com/topic/187510-argh-unworking-code-yet-it-runs-through/
Share on other sites

You could at least try some debugging. Place this at the top.

 

error_reporting(E_ALL); ini_set('display_errors',1);

 

Then replace....

 

$update=mysql_query($qry);

 

with....

 

if (!$update = mysql_query($qry)) {
  trigger_error(mysql_error() . '<br />' . $qry);
}

you cant just store the mysql query in a variable

try

if (!$update)
{
echo "Error in query";
}
else
{
mysql_close();
header('Location: shoutbox-admin.php?action=users
}

 

Dam thorpe, you answered the question as i was posting =[ xD

k first: it does have debugging but wasn't returning an error untill i added the update error check part.

Notice: Undefined variable: statusAction in /home/Rayth/shoutbox-admin.php on line 106

 

Notice: Undefined index: id in /home/Rayth/shoutbox-admin.php on line 108

 

Notice: Undefined variable: newStatus in /home/Rayth/shoutbox-admin.php on line 111

 

 

When i actually set $statusAction the code becomes:

if ($action == 'userstatus') {
   //Create Change
   $statusAction = $_POST['statusAction'];
   if ($statusAction == "Enable") { $newStatus = 1; }
   if ($statusAction == "Disable") { $newStatus = 0; }
   $id = $_POST['id'];

   //Query
   $qry="UPDATE members SET sb_status='$newStatus' WHERE member_id='$id'";
   $update=mysql_query($qry);
   if (!$update = mysql_query($qry)) {
     trigger_error(mysql_error() . '<br />' . $qry);
   }

   //Close mysql and redirect
   mysql_close();
   header("location: shoutbox-admin.php?action=users");
}

 

This then returns:

Notice: Undefined index: statusAction in /home/Rayth/shoutbox-admin.php on line 106

 

Notice: Undefined index: id in /home/Rayth/shoutbox-admin.php on line 109

 

Notice: Undefined variable: newStatus in /home/Rayth/shoutbox-admin.php on line 112

 

 

EDIT: Just fixed msql_close to mysql_close and it is no longer returning errors?

The problem all arrises from these two undefined indexes.

 

Notice: Undefined index: statusAction in /home/Rayth/shoutbox-admin.php on line 106

Notice: Undefined index: id in /home/Rayth/shoutbox-admin.php on line 109

 

You need to make sure indexes exists within an array before you use them. See isset.

they should be set. now it isn't actually displaying error messages. Full code:

 

<?php
    

        ini_set('display_errors', 'on');
        error_reporting(E_ALL);
    //Start session
    session_start();
    
    //Include database connection details
    require_once('../config.php');
    
    //Array to store validation errors
    $errmsg_arr = array();
    
    //Validation error flag
    $errflag = false;
    
    //Connect to mysql server
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
        die('Failed to connect to server: ' . mysql_error());
    }
    
    //Select database
    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
        die("Unable to select database");
    }
        
    //Set Action
    if (isset($_POST['action'])) { $action = $_POST['action']; }
    else { $action = $_GET['action']; }


    //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>

<?php
if ($action == 'users') {

   echo "<center><h2><b>Shoutbox Userlist</b></h2></center>";

   echo "<table cellspacing='2' cellpadding='2' class='tableborder' width='100%'>";
   echo "<tr>";
   echo "<td valign=left><b>ID</b></td>";
   echo "<td valign=left><b>Name</b></td>";
   echo "<td valign=left><b>Message Count</b></td>";
   echo "<td valign=left><b>Status</b></td>";
   echo "<td><b><center>Take Action</center></b></td>";
   echo "</tr>";
   //Get all Members
   $usersqry="SELECT * FROM members ORDER BY member_id";
   $users=mysql_query($usersqry);
   $usercount=mysql_numrows($users);

   $i=0;
   while ($i < $usercount) {
      $id=mysql_result($users,$i,"member_id");
      if ($id != 1) {
         $fname=mysql_result($users,$i,"firstname");
         $lname=mysql_result($users,$i,"lastname");
         $messages=mysql_result($users,$i,"sb_messages");
         $status=mysql_result($users,$i,"sb_status"); 

         //Get Action based on Status
         if ($status == 1) { $statusAction='Disable'; $trueStatus='Enabled'; }
         if ($status == 0) { $statusAction='Enable'; $trueStatus='Disabled'; }

         echo "<tr>";
         echo "<td>".$id."</td>";
         echo "<td>".$fname." ".$lname."</td>";
         echo "<td>".$messages."</td>";
         echo "<td>".$trueStatus."</td>";
         echo "<td>";
?>
         <center><form action="shoutbox-admin.php">
         <input type="hidden" name="action" value="userstatus">
         <input type="hidden" name="id" value="<? echo $id; ?>">
         <input type="hidden" name="statusAction" value="<? echo $statusAction; ?>">
         <input type="submit" value="<? echo $statusAction; ?>"></form></center>
<?php
         echo "</td></tr>";
      }
      ++$i;
   } 
   echo "</table>";
   mysql_close();
}
if ($action == 'userstatus') {
   //Create Change
   $statusAction = $_POST['statusAction'];
   if ($statusAction == "Enable") { $newStatus = 1; }
   if ($statusAction == "Disable") { $newStatus = 0; }
   $id = $_POST['id'];

   //Query
   $qry="UPDATE members SET sb_status='$newStatus' WHERE member_id='$id'";
   $update=mysql_query($qry);
   if (!$update = mysql_query($qry)) {
     trigger_error(mysql_error() . '<br />' . $qry);
   }

   //Close mysql and redirect
   mysql_close();
   header("location: shoutbox-admin.php?action=users");
}
?>

</body> </html>

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.