Hi

about 7 days am experiencing this problem if you can help me out this is the code; it's not updating at all it's just redirecting me to the page this is my code

function editcat($cid,$catname,$catdesc,$ok=false) {
	global $db;
	
	$id = intval($cid);
	
	$result = $db->query("SELECT * FROM category WHERE cid='$cid'");
	$row = $result->fetch_array();
	$cid = $row['cid'];
	$catname = $row['catname'];
	$catdesc = $row['catdesc'];

	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
		echo "<form name='form_arg' method='post' action='index.php?page=articles&op=editCategory&cid=$id&ok=true'>";
		
			echo "<tr><td width='25%'><b>Name</b><td><input type='text' name='catname' size='40' maxlength='255' value=\"$catname\">\n";
			echo "<tr><td><b>catdescription</b><td><input type='text' name='catdesc' size='40' maxlength='255' value=\"$catdesc\">\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='Modify'>\n";

		echo "</form>\n";
		echo "</table>\n";
	} else {	
	   $save = true;
       if ($catname=="") { $save = false; $msg = "Name field is empty!";}
       if ($catdesc=="") { $save = false; $msg = "catdescription field is empty!";}
       
       if($save){
          $stmt = $db->stmt_init();
          $stmt = $db->prepare("UPDATE category SET catname=?,catdesc=? WHERE cid=?");
          $stmt->bind_param('ssi',$catname,$catdesc,$cid);
          $stmt->execute();  
          $stmt->close();        
          echo "<meta http-equiv='refresh' content='0;URL=index.php?page=articles'>";
       }else{
          echo "<div align='center' id='errorText'><b>$msg</b></div>";
       }
	}
}

I do not understand one thing. Why are you passing parameters $catname and $catdesc if you do not use them but instead immediatelly assign some values to them? This is unusual. And check the values of $ok which you are passing and and $save since the update part depends on them.

If i use Add it's working fine

function addcat($catname,$catdesc,$ok=false){
    global $db;
    
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
		echo "<form name='form_arg' method='post' action='index.php?page=articles&op=addCategory&ok=true'>";
			echo "<tr><td width='25%'><b>Title</b><td><input type='text' name='catname' size='40' maxlength='255'>\n";
			echo "<tr><td><b>catdescription</b><td><input type='text' name='catdesc' size='40' maxlength='255'>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='Add'>\n";
		echo "</form>\n";
		echo "</table>\n";
	} else {
	   $save = true;

       if ($catname=="") { $save = false; $msg = "Name field is empty!";}
       if ($catdesc=="") { $save = false; $msg = "catdescription field is empty!";}
       
       if($save){
          $stmt = $db->prepare("INSERT INTO category VALUES (?, ?, ?)");
          $stmt->bind_param('iss', $cid, $catname, $catdesc);
          $stmt->execute();          
          echo "<meta http-equiv='refresh' content='0;URL=index.php?page=articles'>";
       }else{
       	echo "<div align='center' id='errorText'><b>$msg</b></div>";
       }
	}
}

I suggest you do some debugging. Echo values of variables and see what they contain. And also your function for aditing categories can have only two arguments, other are not used editcat($cid, $ok=false) .

So I would do this with the code (note that I assigned both queries to variables):

<?php
function editcat($cid,$ok=false) {
    global $db;

    $id = intval($cid);

    $q_sel = "SELECT * FROM category WHERE cid='$cid'";

    $result = $db->query($q_sel);

    $row = $result->fetch_array();
    $cid = $row['cid'];
    $catname = $row['catname'];
    $catdesc = $row['catdesc'];

    if (!$ok) {
        echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
        echo "<form name='form_arg' method='post' action='index.php?page=articles&op=editCategory&cid=$id&ok=true'>";

            echo "<tr><td width='25%'><b>Name</b><td><input type='text' name='catname' size='40' maxlength='255' value=\"$catname\">\n";
            echo "<tr><td><b>catdescription</b><td><input type='text' name='catdesc' size='40' maxlength='255' value=\"$catdesc\">\n";
            echo "<tr><td colspan='2'><input type='submit' name='Submit' value='Modify'>\n";

        echo "</form>\n";
        echo "</table>\n";
    } else {
       $save = true;
       if ($catname=="") { $save = false; $msg = "Name field is empty!";}
       if ($catdesc=="") { $save = false; $msg = "catdescription field is empty!";}

       if($save){

          $qry_upd = "UPDATE category SET catname=?,catdesc=? WHERE cid=?";

          $stmt = $db->stmt_init();
          $stmt = $db->prepare($qry_upd);
          $stmt->bind_param('ssi',$catname,$catdesc,$cid);
          $stmt->execute();
          $stmt->close();
          echo "<meta http-equiv='refresh' content='0;URL=index.php?page=articles'>";
       }else{
          echo "<div align='center' id='errorText'><b>$msg</b></div>";
       }
    }

    // *** DEBUG SECTION ***
    echo '<div><h2>DEBUGGING</h2><pre>';

    // check the query that selects the categories
    echo "<p>Query to select categories: $q_sel</p>";

    // display the $row array that was read from the table
    echo('<p>' . print_r($row, 1) . '</p>');

    // display cat name and cat desc
    echo "<p>catname: $catname, catdesc: $catdesc</p>";

    // display the $ok and $save variables
    if($ok) {
        echo "<p>ok: true, ";
    } else {
        echo "<p>ok: false, ";
    }
    if($save) {
        echo "save: true</p>";
    } else {
        echo "save: false</p>";
    }

    // display the update query
    echo "<p>Query to update categories: $q_upd</p>";

    // display the $msg variable
    echo "<p>Message: $msg</p>";

    echo '</pre></div>';
}
?>

Now you can check if all the values are what you expect. If you wish you can post debug information.

I am receiving this error now

DEBUGGING

Query to select categories: SELECT * FROM category WHERE cid='1'

Array
(
    [0] => 1
    [cid] => 1
    [1] => Demo1
    [catname] => Demo1
    [2] => demo1
    [catdesc] => demo1
)

catname: Demo1, catdesc: demo1

ok: false, 

Notice:  Undefined variable: save in /Applications/MAMP/htdocs/afaaro/pages/articles/index.php on line 136

save: false



Notice:  Undefined variable: q_upd in /Applications/MAMP/htdocs/afaaro/pages/articles/index.php on line 143

Query to update categories: 



Notice:  Undefined variable: msg in /Applications/MAMP/htdocs/afaaro/pages/articles/index.php on line 146

Message:

These are all the code in the page

if (isset($_GET['op'])) { $op = $_GET['op']; } else { $op = ""; }
if (isset($_GET['ok'])) { $ok = $_GET['ok']; } else { $ok = false; }
if (isset($_GET['cid'])) { $cid = $_GET['cid']; } else { $cid = ""; }
if (isset($_POST['catname'])) { $catname = $_POST['catname']; } else { $catname = ""; }
if (isset($_POST['catdesc'])) { $catdesc = $_POST['catdesc']; } else { $catdesc = ""; }


function catList(){
    global $db;
    
		$n = 0;
		echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
		echo "<thead>\n";
			echo "<a href='index.php?page=articles&op=addCategory'>Add category </a>";
			echo "<tr><td>Title<td>catdesc</td><td width='1%'>&nbsp;</td></tr>\n";
		echo "</thead>\n";
		echo "<tbody>\n";
			if ($result = $db->query("SELECT * FROM category ORDER BY catname")) {
				while($row = $result->fetch_assoc()){
					$cid = intval($row['cid']);
					$name = $row['catname'];
					$catdesc = $row['catdesc'];
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					echo "<tr><td class='$class'><b>$name</b><td>$catdesc</td><td class='$class' nowrap><div align='right'><a href='index.php?page=articles&op=editCategory&cid=$cid' title='Modify'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='index.php?page=articles&op=delCategory&cid=$cid' title='Delete'><img src='images/delete.gif' alt='Delete' border='0'></a></div></td></tr>\n";	
				}
			} else {
				echo "<tr><td align='center' id='errorText' class='clean'><b>No Category defined</b></td></tr>";
			}
		echo "</tbody>\n";
		echo "</table>";
}

function addcat($catname,$catdesc,$ok=false){
    global $db;
    
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
		echo "<form name='form_arg' method='post' action='index.php?page=articles&op=addCategory&ok=true'>";
			echo "<tr><td width='25%'><b>Title</b><td><input type='text' name='catname' size='40' maxlength='255'>\n";
			echo "<tr><td><b>catdescription</b><td><input type='text' name='catdesc' size='40' maxlength='255'>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='Add'>\n";
		echo "</form>\n";
		echo "</table>\n";
	} else {
	   $save = true;

       if ($catname=="") { $save = false; $msg = "Name field is empty!";}
       if ($catdesc=="") { $save = false; $msg = "catdescription field is empty!";}
       
       if($save){
          $stmt = $db->prepare("INSERT INTO category VALUES (?, ?, ?)");
          $stmt->bind_param('iss', $cid, $catname, $catdesc);
          $stmt->execute();          
          echo "<meta http-equiv='refresh' content='0;URL=index.php?page=articles'>";
       }else{
       	echo "<div align='center' id='errorText'><b>$msg</b></div>";
       }
	}
}

function editcat($cid,$ok=false) {
    global $db;
 
    $id = intval($cid);
 
    $q_sel = "SELECT * FROM category WHERE cid='$cid'";
 
    $result = $db->query($q_sel);
 
    $row = $result->fetch_array();
    $cid = $row['cid'];
    $catname = $row['catname'];
    $catdesc = $row['catdesc'];
 
    if (!$ok) {
        echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
        echo "<form name='form_arg' method='post' action='index.php?page=articles&op=editCategory&cid=$id&ok=true'>";
 
            echo "<tr><td width='25%'><b>Name</b><td><input type='text' name='catname' size='40' maxlength='255' value=\"$catname\">\n";
            echo "<tr><td><b>catdescription</b><td><input type='text' name='catdesc' size='40' maxlength='255' value=\"$catdesc\">\n";
            echo "<tr><td colspan='2'><input type='submit' name='Submit' value='Modify'>\n";
 
        echo "</form>\n";
        echo "</table>\n";
    } else {
       $save = true;
       if ($catname=="") { $save = false; $msg = "Name field is empty!";}
       if ($catdesc=="") { $save = false; $msg = "catdescription field is empty!";}
 
       if($save){
 
          $qry_upd = "UPDATE category SET catname=?,catdesc=? WHERE cid=?";
 
          $stmt = $db->stmt_init();
          $stmt = $db->prepare($qry_upd);
          $stmt->bind_param('ssi',$catname,$catdesc,$cid);
          $stmt->execute();
          $stmt->close();
          echo "<meta http-equiv='refresh' content='0;URL=index.php?page=articles'>";
       }else{
          echo "<div align='center' id='errorText'><b>$msg</b></div>";
       }
    }
}

function delcat($cid,$ok=false){
	global $db;
	
	if ($ok) {
		$db->query("DELETE FROM category WHERE cid='$cid'");
		echo "<meta http-equiv='refresh' content='0;URL=index.php?page=articles'>";		
	} else {
		echo "<div align='center'><b>Are you sure you want to delete?</b><br><a href='index.php?page=articles&op=delCategory&cid=$cid&ok=true' title='Yes'>Yes</a> - <a href='index.php?page=articles' title='No'>No</a></div>";
	}  
}

	openTable();
	
		switch($op) {
			case "addCategory":
				addcat($catname,$catdesc,$ok);
			break;
			
            case "editCategory":
                editcat($cid,$ok);
            break;
            
            case "delCategory":
                delcat($cid,$ok);
            break;
            
			case "showCategories":			
			default:
				catList();
			break;
		}
	
	closeTable();

anyhelp

From debugging info it is evident that $ok is false and and $save does not get assigned (is not existing). Therefore the first block of if statement if (!$ok) gets executed (lines 13 to 21) and no update takes place. $ok comes from $_GET and if you want to update to take place, $ok has to be set to true. So look at the page that sends $_GET (a form?).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.