Update does not update table

1

Good to execute my code I can not change the value in database.

Modify.php

include ("conexion.php");

$usuario="jose";

$linku= $_POST["linkm"];

$db = mysqli_select_db( $conexion, $basededatos ) or die ( "Upps! Pues va a ser que no se ha podido conectar a la base de datos" );




if($hola=mysqli_query("UPDATE link SET acceso=$linku WHERE usuario=$usuario")) {
    $stmt->bind_param("ss", $usuario, $linku);
echo "modificacion ok";}
else
echo "no se pudo";
  

always goes by the "could not". I would appreciate your help.

    
asked by Juan Ontivero 16.10.2018 в 02:46
source

1 answer

2

I mention the following, your code should look like this:

$conexion = new mysqli('localhost', 'root', 'password', 'database');

$usuario="jose";
$linku= $_POST["linkm"];
$query=mysqli_query($conexion, "UPDATE link SET acceso='$linku' WHERE usuario='$usuario'");

if($query){
    echo "actualizado";
}else{
    echo "no actualizado";
}

WHAT DID I DO?

  • You can also pass the name of the database in the instance of mysqli , so you do not need a function to connect and another to select the database
  • variable $query will store the query, where: first I pass the variable of $conexion and later the query; note how the variables I put them in single quotes
  • On the outside by a simple conditional evaluate whether the variable $query was executed or not, remember that it has a Boolean value so it is enough to do if(var) to check if it is executed or not
  • With the above, your code must already be functional

    UPDATE

    Considering that you are working with mysqli and that you are receiving data from the user, the ideal thing here is to work the query in a prepared way to prevent SQL INJECTION , so your query should be like this

    $conexion = new mysqli('localhost', 'root', 'password', 'database');
    $linku= $_POST["linkm"];
    $usuario="jose";
    
    $query= $conexion->prepare("UPDATE link SET acceso= ? WHERE usuario= ?");
    $query->bind_param('ss', $linku, $usuario);
    $query->execute();
    if($query){
        echo "actualizado";
    }else{
        echo "no actualizado";
    }
    

    WHAT I DID

  • Replace the direct call of the variables in the query, with placeholders ?
  • answered by 16.10.2018 / 03:10
    source