Jump to content

problem update not working


daniele

Recommended Posts

Hello everyone I have a problem with an update I have created a menu and I have to make sure they can hide or show the menu I created three classes but does not work
class 1

public function selectAction($action,$id){

switch($action){
case ('novisibol') :

$visibol= 1;
if( !$res=$this->_modelAdmin->visibleMenu($visibol,$id)){
echo 'errore nel cambio dello stato';

}else {
header ('Location: admin.php?menu');
}

break ;

case ('visibol') :
$visibol= 0 ;

if( !$res=$this->_modelAdmin->visibleMenu($visibol,$id)){
echo 'errore nel cambio dello stato';

}else {
header ('Location: admin.php?menu');
}

Class 2

public function visibleMenu($id,$visibol){

if( empty($id) ) return false ;
if ($visibol > 1) return false ;


if ($visibol==0){
$this->_db->update( 'menu',array('menu_visibol'=>1),'menu_id='.$id );
}else {
$this->_db->update( 'menu',array('menu_visibol'=>0),'menu_id='. $id );
}

return true ;

}
Class 3

public function update( $table, $values, $conditions = '1' ){
$first = true;

$query = "UPDATE " . $table;
$query .= " SET ";

foreach( $values as $name => $value ){
if( ! $first ){
$query .= ", ";
}

$query .= $name . " = " . $value;
$first = false;
}

$query .= " WHERE ";
$query .= $conditions;

return mysqli_query( $this->_conn, $query );
}

 

thanks for the help and good holidays

Link to comment
Share on other sites

does not work

 

 

it's pointless to tell us that something does not work. we already know that or you wouldn't be posting on a programming help forum.

 

we don't have the ability to run your code with your data on your server and we are not sitting right next to you to have seen what you saw when you ran your code. you have got to tell us what error or other symptom you are getting that leads you to believe that your code isn't working.

Link to comment
Share on other sites

I am reposting your code in a much more readable format:

public function selectAction($action,$id){

switch($action){
case ('novisibol') :

$visibol= 1;
if( !$res=$this->_modelAdmin->visibleMenu($visibol,$id)){
echo 'errore nel cambio dello stato';

}else {
header ('Location: admin.php?menu');
}

break ;

case ('visibol') :
$visibol= 0 ;

if( !$res=$this->_modelAdmin->visibleMenu($visibol,$id)){
echo 'errore nel cambio dello stato';

}else {
header ('Location: admin.php?menu');
}

public function visibleMenu($id,$visibol){

if( empty($id) ) return false ;
if ($visibol > 1) return false ;


if ($visibol==0){
$this->_db->update( 'menu',array('menu_visibol'=>1),'menu_id='.$id );
}else {
$this->_db->update( 'menu',array('menu_visibol'=>0),'menu_id='. $id );
}

return true ;

}

public function update( $table, $values, $conditions = '1' ){
$first = true;

$query = "UPDATE " . $table;
$query .= " SET ";

foreach( $values as $name => $value ){
if( ! $first ){
$query .= ", ";
}

$query .= $name . " = " . $value;
$first = false;
}

$query .= " WHERE ";
$query .= $conditions;

return mysqli_query( $this->_conn, $query );
}

In what you call your 3rd class, it seems a bit off, but I am not sure. Before return mysqli_query( $this->_conn, $query ); add this before it: error_log(var_export($query,true));

When the query runs, it will post it to your error log and then you will be able to see if your query is actually doing what you want it to do.

Edited by parkerj
Link to comment
Share on other sites

the script does not give me error simply does not do what I want.

The third class and so full:

 

class DbConn
{
private $_conn='';

public function __construct(){

$this->_conn=mysqli_connect('localhost','root','','sito');
if($this->_conn->errno){
echo "errore di conessione".$this->_conn->error;
}
}

public function select( $fields, $tables, $conditions = '1' ){
$query = "SELECT ";
$query .= implode( ', ', $fields );
$query .= " FROM ";
$query .= implode( ', ', $tables );
$query .= " WHERE ";
$query .= $conditions;

error_log(var_export($query,true));
return mysqli_query( $this->_conn, $query );
}


public function update( $table, $values, $conditions = '1' ){
$first = true;

$query = "UPDATE " . $table;
$query .= " SET ";

foreach( $values as $name => $value ){
if( ! $first ){
$query .= ", ";
}

$query .= $name . " = " . $value;
$first = false;
}

$query .= " WHERE ";
$query .= $conditions;

return mysqli_query( $this->_conn, $query );
}



public function delete( $table, $conditions = '0' ){
$query = "DELETE FROM " . $table;
$query .= " WHERE ";
$query .= $conditions;

return mysqli_query( $this->_conn, $query );
}


public function fetch_assoc($res)
{

return mysqli_fetch_assoc($res);
}

public function escape($string)
{

return mysqli_escape_string($this->_conn, $string);
}

public function __destruct()
{

$this->_conn->close();
}

}

 

if you can help you put also the view

 

<?php
require_once ($_SERVER['DOCUMENT_ROOT']."/controllers/controller_admin.php");
require_once ($_SERVER['DOCUMENT_ROOT']."/modules/module_admin.php");
require_once($_SERVER['DOCUMENT_ROOT']."/libreria/DbConn.php");


$action = (isset($_GET['action'])) ? $_GET['action'] : null ;
$id = (isset($_GET['id'])) ? $_GET['id'] : null ;
$controllerAdmin=new controller_admin($action,$id);
$result=$controllerAdmin->menuAdmin();
?>
<?php if(!empty($result)): ?>
<h2>gestione menu</h2>
<table>
<thead>
<tr>
<th>nome Categoria</th>
<th>Visibilita</th>
<th colspan="2">Modifica</th>
</tr>
</thead>
<?php foreach ($result as $risultato): ?>
<?php if($risultato['menu_visibol']== 1){
$action="?menu&action=novisibol";
$text="novisibol";
$class="novisibol";
} else{
$action="?menu&action=visibol";
$text="visibol";
$class="visibol";

}

?>
<tr>
<td><a href="?menu&action=edit&id=<?php echo $risultato['menu_id']; ?>"
title="<?php echo $risultato['menu_title']; ?>"><?php echo $risultato['menu_name'] ; ?></a>
</td>
<td><a title="<?php echo $text ;?>" class="<?php echo $class ; ?>"
href="<?php echo $action . '&id='.$risultato['menu_id']; ?>"><?php echo $text ;?></a>
</td>
<td><a href="?menu&action=edit&id=<?php echo $risultato['menu_id']; ?>" title="modifica">Modifica</a></td>
<td><a href="?menu&action=delete&id=<?php echo $risultato['menu_id']; ?>" title="elimina">Elimina</a></td>

</tr>
<?php endforeach ; ?>
</table>
<?php endif ;?>

 

 

thanks for the help

Link to comment
Share on other sites

I think what parkerj was trying to tell you is to put your script in a code tag that is in the editor and properly formatted. 

 

for example:

<?php

class DbConn {

    private $_conn = '';

    public function __construct() {

        $this->_conn = mysqli_connect('localhost', 'root', '', 'sito');
        if ($this->_conn->errno) {
            echo "errore di conessione" . $this->_conn->error;
        }
    }

    public function select($fields, $tables, $conditions = '1') {
        $query = "SELECT ";
        $query .= implode(', ', $fields);
        $query .= " FROM ";
        $query .= implode(', ', $tables);
        $query .= " WHERE ";
        $query .= $conditions;

        error_log(var_export($query, true));
        return mysqli_query($this->_conn, $query);
    }

    public function update($table, $values, $conditions = '1') {
        $first = true;

        $query = "UPDATE " . $table;
        $query .= " SET ";

        foreach ($values as $name => $value) {
            if (!$first) {
                $query .= ", ";
            }

            $query .= $name . " = " . $value;
            $first = false;
        }

        $query .= " WHERE ";
        $query .= $conditions;

        return mysqli_query($this->_conn, $query);
    }

    public function delete($table, $conditions = '0') {
        $query = "DELETE FROM " . $table;
        $query .= " WHERE ";
        $query .= $conditions;

        return mysqli_query($this->_conn, $query);
    }

    public function fetch_assoc($res) {

        return mysqli_fetch_assoc($res);
    }

    public function escape($string) {

        return mysqli_escape_string($this->_conn, $string);
    }

    public function __destruct() {

        $this->_conn->close();
    }

}

if you can help you put also the view

<?php
require_once ($_SERVER['DOCUMENT_ROOT'] . "/controllers/controller_admin.php");
require_once ($_SERVER['DOCUMENT_ROOT'] . "/modules/module_admin.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libreria/DbConn.php");


$action = (isset($_GET['action'])) ? $_GET['action'] : null;
$id = (isset($_GET['id'])) ? $_GET['id'] : null;
$controllerAdmin = new controller_admin($action, $id);
$result = $controllerAdmin->menuAdmin();
?>
<?php if (!empty($result)): ?>
    <h2>gestione menu</h2>
    <table>
        <thead>
            <tr>
                <th>nome Categoria</th>
                <th>Visibilita</th>
                <th colspan="2">Modifica</th>
            </tr>
        </thead>
    <?php foreach ($result as $risultato): ?>
        <?php
        if ($risultato['menu_visibol'] == 1) {
            $action = "?menu&action=novisibol";
            $text = "novisibol";
            $class = "novisibol";
        } else {
            $action = "?menu&action=visibol";
            $text = "visibol";
            $class = "visibol";
        }
        ?>
            <tr>
                <td><a href="?menu&action=edit&id=<?php echo $risultato['menu_id']; ?>"
                       title="<?php echo $risultato['menu_title']; ?>"><?php echo $risultato['menu_name']; ?></a>
                </td>
                <td><a title="<?php echo $text; ?>" class="<?php echo $class; ?>"
                       href="<?php echo $action . '&id=' . $risultato['menu_id']; ?>"><?php echo $text; ?></a>
                </td>
                <td><a href="?menu&action=edit&id=<?php echo $risultato['menu_id']; ?>" title="modifica">Modifica</a></td>
                <td><a href="?menu&action=delete&id=<?php echo $risultato['menu_id']; ?>" title="elimina">Elimina</a></td>

            </tr>
    <?php endforeach; ?>
    </table>
<?php endif; ?>
Link to comment
Share on other sites

Sorry, what I should have wrote was this instead:

error_log(var_export(mysqli_query( $this->_conn, $query ),true));

If that still does not populate error messages on your server, then if you are using Firefox or Chrome, use the network monitor when submitting the form to make sure the fields being posted match the database fields or see if there is some other problem.

Link to comment
Share on other sites

the script does not give me error simply does not do what I want.

 

 

there are at least a dozen different things that could cause your code to not do what you want, ranging from a database connection that is failing, to invoking the page in a way that isn't causing the php code to be ran. you have to tell or show us what output your code is producing and what exactly is wrong with that output in order to narrow down the possibilities.

 

you also put the debugging line of code that parkerj suggest in the wrong place. you stated that the UPDATE part of the code is where the problem seems to be, yet, you put his suggestion into the SELECT part of the code.

Link to comment
Share on other sites

finally, some information from you that helps, that you should have supplied in the first post in this thread.

 

1) that IS an error message. it's being produced in your code when the visibleMenu() method returns a false value (unless you have have that same error message in other places in your code and it's actually coming from somewhere else and not the call to the visibleMenu() method.)

 

2) the visibleMenu() method will return a false value when -

if( empty($id) ) return false ;
if ($visibol > 1) return false ;

3) since, $visibol is being set by your code to either a 0 or a 1 right before calling the visibleMenu() method, that means that $id is probably empty.

 

4) $id is coming from -

$action = (isset($_GET['action'])) ? $_GET['action'] : null;
$id = (isset($_GET['id'])) ? $_GET['id'] : null;
$controllerAdmin = new controller_admin($action, $id);

this means that there is probably no $_GET['id'] in the url. there is apparently a $_GET['action'], otherwise the code where the   errore nel cambio dello stato is at wouldn't be running.

 

5) this is the code producing the url -

if ($risultato['menu_visibol'] == 1) {
  $action = "?menu&action=novisibol";
  $text = "novisibol";
  $class = "novisibol";
} else {
  $action = "?menu&action=visibol";
  $text = "visibol";
  $class = "visibol";
}

and this -

<td><a title="<?php echo $text ;?>" class="<?php echo $class ; ?>"
href="<?php echo $action . '&id='.$risultato['menu_id']; ?>"><?php echo $text ;?></a>
</td>

so, do the links on your page have an &id=some_value in them and that same part of the url is present in the address bar of your browser on the page where you get the errore nel cambio dello stato message (in case you have some url rewriting that's not carrying over that value)?

Link to comment
Share on other sites

the visibleMenu() method will return a false .I made a var_dump before the data and if there are

var_dump($id,$visibol);
if( empty($id) ) return false ;
if ($visibol > 1) return false ;

making a var_dump after nothing happens as if the data arrived

Link to comment
Share on other sites

lol, you did a var_dump() on the values and looked at them, but didn't think it would be helpful to show us what they were?

 

i'm going to venture a guess, the $id is probably a zero, which is considered to be an empty() value. if so, you should not be using a zero as an id (identifier), especially since the column in your database table defining the ids should be an auto-increment integer column, which won't normally start at zero.

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.