Change Mysql column information with UPDATE

Asked

Viewed 872 times

3

In summary through PHP below I can send the specific information already recorded in the column in the BD, only that, in addition to just "sending" the data there, I would like a function that changes such information without the page being changed. I have an idea how to do this using the following code:

if ($action == 'okay') {
$conexao = mysql_query("update settingsMDP set name='$name', version='$version', startacp='$startacp' WHERE id='1'") or die ("Eita.. Deu errado!");
}

If they go to see, if the form action is "okay" then, it runs the UPDATE, but... This does not occur I don’t know why.

This is the PHP.

<?php
// definições de host, database, usuário e senha
$server = "";
$usuario = "";
$banco   = "";
$senha = "";
// conecta ao banco de dados
$conexao = mysql_connect($server, $usuario, $senha);
$conexao = mysql_select_db("$banco",$conexao);
if(!$conexao) {
    echo mysql_error();
    exit;
}

$conexao = mysql_query("select * from settingsMDP");

//********************************************mudei aqui*****************************
$exibe = mysql_fetch_assoc($conexao);

 if (empty($_REQUEST['action'])) $action = ''; else $action = $_REQUEST['action'];
 if ($action == 'okay') {
 $conexao = mysql_query("update settingsMDP set name='$name', version='$version', startacp='$startacp' WHERE id='1'") or die(mysql_error());
}
?>

<form method="post" action="<?php $_PHP_SELF ?>">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
        <tr>
            <td width="100">Nome do MDP: </td>
            <td><input name="name" type="text" id="name" value="<?php echo $exibe["name"]; ?>"></td>
        </tr>
        <tr>
            <td width="100">Versão: </td>
            <td><input name="name" type="text" id="name" value="<?php echo $exibe["version"]; ?>"></td>
        </tr>
        <tr>
            <td width="100">Abertura do painel:</td>
            <td><input name="start_acp" type="text" id="startacp" value="<?php echo $exibe["startacp"]; ?>"></td>
        </tr>
        <tr>
            <td width="100"> </td>
            <td> </td>
        </tr>
        <tr>
            <td width="100"> </td>
            <td><input name="update" type="submit" id="update" value="Salvar"></td>
        </tr>
    </table>
</form>
  • When you are developing avoid putting custom errors, change: or die ("Eita.. Deu errado!"); for : or die(mysql_error());. Where it comes from $action? in your form there are 2 fields the same name(name) I believe one of them should be version.

  • Give a var_dump($action) there, I think the problem is in $action.

  • var_dump? I just edited the code. Is this what you mentioned @Inkeliz? @rray I already modified the custom term. But why is it advisable not to use?

  • Another additional that will not interfere much in the problem. I recommend putting "{" keys out of the variables in the string, thus: set name='{$name}',. To help PHP identify them.

  • It would be to see what’s in the $action.

  • With mysql_error() you receive the error message from the bank, something like You have a syntax error at ...... which is much better than a Deu erro! that gives you no clue about the mistake.

  • 1

    mysql_* functions have been obsolete since PHP 5.5. Use PDO or mysqli. See http://www.ultimatephp.com.br/php-por-que-nao-utilizar-funcoes-mysql

Show 2 more comments

1 answer

3


I will tinker with what I think is what you want. I will avail to solve some other problems but not all.

<?php
// definições de host, database, usuário e senha
$server = "";
$usuario = "";
$banco   = "";
$senha = "";
// conecta ao banco de dados
$conexao = mysqli_connect($server, $usuario, $senha);
mysqli_select_db($conexao, $banco);
if(!$conexao) {
    echo mysqli_error();
    exit;
}

if ($_POST["update"] == "Salvar") {
    $name = mysqli_real_escape_string($conexao, $_POST["name"]);
    $version = mysqli_real_escape_string($conexao, $_POST["version"]);
    $startacp = mysqli_real_escape_string($conexao, $_POST["startacp"]);
    $resultado = mysqli_query($conexao, "update settingsMDP set name='$name', version='$version', startacp='$startacp' WHERE id='1'") or die (mysqli_error());
    echo "Gravou";
} else {
    $resultado = mysqli_query($conexao, "select * from settingsMDP");
    $exibe = mysqli_fetch_array($resultado, MYSQLI_ASSOC);
?>

<form method="post">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
        <tr>
            <td width="100">Nome do MDP: </td>
            <td><input name="name" type="text" id="name" value="<?php echo $exibe["name"]; ?>"></td>
        </tr>
        <tr>
            <td width="100">Versão: </td>
            <td><input name="version" type="text" id="version" value="<?php echo $exibe["version"]; ?>"></td>
        </tr>
        <tr>
            <td width="100">Abertura do painel:</td>
            <td><input name="startacp" type="text" id="startacp" value="<?php echo $exibe["startacp"]; ?>"></td>
        </tr>
        <tr>
            <td width="100"> </td>
            <td> </td>
        </tr>
        <tr>
            <td width="100"> </td>
            <td><input name="update" type="submit" id="update" value="Salvar"></td>
        </tr>
    </table>
</form>
<?php
}
?>

I put in the Github for future reference.

I can’t test it but it’s something like this.

Since you are using the same page to grab the data and refresh you need to decide which of the two will do. This can be done if the Submit is present or not.

I made the so-called sanitization of the contents that came to avoid SQL Injection.

I switched to Myslqi which is a more modern function and which is recommended.

I fixed an inattention error in the name of HTML fields.

There was also confusion with the variable $conexao that caused the loss of the reference to the connection. You can’t reuse the variable like this.

I left the flame of form no parameter that will call its own URL.

And I got a few more details. I would still change a lot of things in that code but I better not mess around too much with what you’re able to do or you’ll get lost. A quality code for use in production would have to be much better than this. Although I’ve seen a lot website running around with even worse :P

  • Does the $connection variable not correspond to the connection? I was confused now at this point, because it rescues the connection with the BD, isn’t it? Regarding its edition, now I got some errors: http://staff.zz.mu/mods/templates/siteconfig.php This modern version requires which version of PHP?

  • Yes, and you use it for various things, not just for the connection. mysqli is available since version 4.1. I will fix these problems, in fact forgot to change the parameters that are most organized in it.

  • 1

    I’ve improved the code, but it’s so full of errors that it’s hard to find everyone without thresh all of it. It has typos/inattention, confusions of variable use, logic, etc.

  • You’re more than perfect friend @bigown Thanks for solving my questions.

  • by what I saw running already improved, right? But still has other problems, you keep asking there specific things.

  • I would have another question for later. That’s solved. ;)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.