afaaro Posted February 23, 2012 Share Posted February 23, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/ Share on other sites More sharing options...
blacknight Posted February 23, 2012 Share Posted February 23, 2012 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320358 Share on other sites More sharing options...
afaaro Posted February 23, 2012 Author Share Posted February 23, 2012 I have tried this one but it is not working at all Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320365 Share on other sites More sharing options...
blacknight Posted February 23, 2012 Share Posted February 23, 2012 add echo $cid. ' - '.$catname.' - '.$catdesc. ' - ' .(bool)$ok; after your global statement and see if the values are even passing to the function... and if youa re getting a sql error post it please Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320375 Share on other sites More sharing options...
afaaro Posted February 23, 2012 Author Share Posted February 23, 2012 When i edit it shows the same value nothing else 1 - Demo1 - demo1 - 1 Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320386 Share on other sites More sharing options...
blacknight Posted February 23, 2012 Share Posted February 23, 2012 is what it shows you what is in the database? Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320389 Share on other sites More sharing options...
mikosiko Posted February 23, 2012 Share Posted February 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320390 Share on other sites More sharing options...
afaaro Posted February 23, 2012 Author Share Posted February 23, 2012 i know but it's not updating at all Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320393 Share on other sites More sharing options...
afaaro Posted February 23, 2012 Author Share Posted February 23, 2012 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320396 Share on other sites More sharing options...
blacknight Posted February 23, 2012 Share Posted February 23, 2012 try putting every thing from openTable(); down befor your functions.... and there has to be some sort of mysql error .... if its not updating... Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320398 Share on other sites More sharing options...
mikosiko Posted February 23, 2012 Share Posted February 23, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320434 Share on other sites More sharing options...
afaaro Posted February 24, 2012 Author Share Posted February 24, 2012 still i don't understand why is happining without update Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320633 Share on other sites More sharing options...
blacknight Posted February 24, 2012 Share Posted February 24, 2012 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320708 Share on other sites More sharing options...
afaaro Posted February 24, 2012 Author Share Posted February 24, 2012 I have put error_reporting(E_ALL); ini_set("display_errors", 1); but still nothing is coming up Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320774 Share on other sites More sharing options...
mikosiko Posted February 24, 2012 Share Posted February 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320806 Share on other sites More sharing options...
mikosiko Posted February 24, 2012 Share Posted February 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320829 Share on other sites More sharing options...
afaaro Posted February 24, 2012 Author Share Posted February 24, 2012 I am using index.php in my root index.php?page=articles Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320969 Share on other sites More sharing options...
mikosiko Posted February 25, 2012 Share Posted February 25, 2012 I am using index.php in my root index.php?page=articles Uh?.... don't follow... did you read and try what was suggested in my last post?.... your answer doesn't make any sense at all.... if need further help please provide better and complete status. Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320977 Share on other sites More sharing options...
afaaro Posted February 25, 2012 Author Share Posted February 25, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1320984 Share on other sites More sharing options...
mikosiko Posted February 25, 2012 Share Posted February 25, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/257588-myqli-error/#findComment-1321067 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.