Jump to content

Recommended Posts

Hello everyone

 

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

 

$db = new mysqli($dbHost, $dbUser, $dbPass, $dbName) or die (mysql_error());

 

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>";
       }
}
}

 

	switch($op) {
		case "addCategory":
			addcat($catname,$catdesc,$ok);
		break;

            case "editCategory":
                editcat($cid,$catname,$catdesc,$ok);
            break;
            
            case "delCategory":
                delcat($cid,$ok);
            break;
            
		case "showCategories":			
		default:
			catList();
		break;
	}

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/
Share on other sites

drop the

$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();  

and just use a normal sql update string....

 

$result = $db->query("UPDATE `category` SET `catname`='$catname',`catdesc`='$catdesc' WHERE `cid`='$cid'");

 

only thing i can sujest because you dident post the $stmt function file ... but that seems like a lota work for an update ...

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320358
Share on other sites

your code

$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(); 

 

has a couple details...

1) you are mixing 2 different ways to declare a prepared statement... drop your line $stmt = $db->stmt_init() .. it is not necessary considering the rest of your code.

2) all the rest of the sentences (prepare, bind_param and execute) always return TRUE on success or FALSE on error, therefore you should control if each one is successfully executed, otherwise display and control the possible error using $stmt->error and probably $stmt->errno .... just an example (for you to complete):

       
if ($stmt = $db->prepare("UPDATE category SET catname=?,catdesc=? WHERE cid=?")) {          

   // Use same IF previous example to control the prepare
   $stmt->bind_param('ssi',$catname,$catdesc,$cid);         

   // Use same IF previous example to control the execute
   $stmt->execute();            

   $stmt->close();  // you can drop this line to... close() occur automatic when the script end.
} else {
    echo "Prepare Error : " . $stmt->error . ' Num Error : . $stmt->errno;
    // here decide what to do next
}

 

that should at least give you some error message if any of your sentences is failing

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320390
Share on other sites

This is the code am using in the page

 

<?php

/**
* @author Afaaro
* @copyright 2012
* @type articles
*/

if (!defined("_LOAD")) {
    die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be actened directly!</td></tr></table>");
}

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%'> </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,$catname,$catdesc,$ok=false) {
global $db;

$cid = intval($cid);

$result = $db->query("SELECT * FROM category WHERE cid='$cid'");
$row = $result->fetch_array();
$acid = $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=$acid&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){
		if ($stmt = $db->prepare("UPDATE category SET catname=?,catdesc=? WHERE cid=?")) {          

		   // Use same IF previous example to control the prepare
		   $stmt->bind_param('ssi',$catname,$catdesc,$cid);         

		   // Use same IF previous example to control the execute
		   $stmt->execute();            

		   $stmt->close();  // you can drop this line to... close() occur automatic when the script end.
		} else {
		    echo "Prepare Error : ' . $stmt->error . ' Num Error : . $stmt->errno";
		    // here decide what to do next
		}
       }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='admin.php?page=arguments' title='No'>No</a></div>";
}  
}

openTable();
switch($op) {
	case "addCategory":
		addcat($catname,$catdesc,$ok);
	break;

        case "editCategory":
            editcat($cid,$catname,$catdesc,$ok);
        break;
            
        case "delCategory":
            delcat($cid,$ok);
        break;
            
	case "showCategories":			
	default:
		catList();
	break;
}	
closeTable();
    
?>

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320396
Share on other sites

you didn't do nothing to complete the provided code... copy and paste alone is not going to help you

 

 if($save){
		if ($stmt = $db->prepare("UPDATE category SET catname=?,catdesc=? WHERE cid=?")) {          

		   // Use same IF previous example to control the prepare
		   $stmt->bind_param('ssi',$catname,$catdesc,$cid);         

		   // Use same IF previous example to control the execute
		   $stmt->execute();            

		   $stmt->close();  // you can drop this line to... close() occur automatic when the script end.
		} else {
		    echo "Prepare Error ";			    
                           // here decide what to do next
		}

 

there are comments in the provided code... those were there for you to complete... other hint... you should have modified the lines:

			   // Use same IF previous example to control the prepare
		   $stmt->bind_param('ssi',$catname,$catdesc,$cid);     

to this per example:

		   // Use same IF previous example to control the execute
		   IF (!$stmt->bind_param('ssi',$catname,$catdesc,$cid)) {
                                 echo "Bind Param Error: " . $stmt->error . " Error # " . $stmt->errno;
                                  // here write code to execute after the error
                           }  else {
                                  //here the rest of your code... don't forget to control in the same way the execute
                           }

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320434
Share on other sites

us ither ... you are getting no sql or php errors?

have you tryed not using stmt

tryed making the changes in the database using myadmin and changing them back on the webpage?

have you tryed putting

error_reporting(E_ALL);

ini_set("display_errors", 1);

 

at the start of the page directly after the <?php ?

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320708
Share on other sites

post your current full code again

 

in the last code that you posted few posts ago the code shows a call to the function openTable();  (and closeTable()) which are not defined in any place in that code; it could or could not be the cause of your problem, only way to tell is looking to the whole code.

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320806
Share on other sites

after having a second look to your code .. in particular to the editcat() function; I see where could be your problem; you are overwriting the edited fields value; therefore even when the UPDATE is executed correctly in reality nothing is changed.... look this extract of your code:

 

function editcat($cid,$catname,$catdesc,$ok=false) {
global $db;

$cid = intval($cid);

// Here you are selecting the values from the DB the first time that you click in the edit button, after select the record your display it using the form below
        // After submit the form this very same function is called again to execute the update with the new values... however you are executing the SELECT again overwriting
       // the updated values.... 
$result = $db->query("SELECT * FROM category WHERE cid='$cid'");
$row = $result->fetch_array();
$acid = $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=$acid&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 {
.........

 

try modifying the function a little bit like this per example:

 

function editcat($cid,$catname,$catdesc,$ok=false) {
global $db;

$cid = intval($cid);

// Control if the form was submitted or not
       if (!isset($_POST['catname'])) {
   $result = $db->query("SELECT * FROM category WHERE cid='$cid'");
   $row = $result->fetch_array();
   	   $acid = $row['cid'];
   $catname = $row['catname'];
   $catdesc = $row['catdesc'];
       }
       // rest of your code here
      ...

 

my previous suggestions still valid too.

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320829
Share on other sites

Thank you so much Musiko your code worked

 

    if (!isset($_POST['catname'])) {
   $result = $db->query("SELECT * FROM category WHERE cid='$cid'");
   $row = $result->fetch_array();
   	   $acid = $row['cid'];
   $catname = $row['catname'];
   $catdesc = $row['catdesc'];
    }

 

and with this

 

       if($save){
       		$stmt = $db->stmt_init();
		$stmt = $db->prepare("UPDATE category SET catname=?,catdesc=? WHERE cid=?");
		#$db->query("UPDATE category SET catname=$catname,catdesc=$catdesc WHERE cid=$cid");
		$stmt->bind_param('ssi',$catname,$catdesc,$cid);
		$stmt->execute(); 
		echo "<meta http-equiv='refresh' content='0;URL=index.php?page=articles'>";
       }else{
       		#echo "Prepare Error : ' . $db->error . ' Num Error : . $db->errno";
            echo "<div align='center' id='errorText'><b>$msg</b></div>";
       }

 

And thanks to blacknight as well;

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320984
Share on other sites

this is incorrect

     if($save){
       		$stmt = $db->stmt_init();
		$stmt = $db->prepare("UPDATE category SET catname=?,catdesc=? WHERE cid=?");
		#$db->query("UPDATE category SET catname=$catname,catdesc=$catdesc WHERE cid=$cid");
		$stmt->bind_param('ssi',$catname,$catdesc,$cid);
		$stmt->execute(); 
		echo "<meta http-equiv='refresh' content='0;URL=index.php?page=articles'>";
       }else{
       		#echo "Prepare Error : ' . $db->error . ' Num Error : . $db->errno";
            echo "<div align='center' id='errorText'><b>$msg</b></div>";
       }

 

review my examples again

Link to comment
https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1321067
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.