Update table attribute, via Form <?php $PHP_SELF ? >

Asked

Viewed 245 times

2

I tried to make a code here, but it didn’t go very well... they know to inform me where the error is?

<?
     if(isset($_POST['reseta_wl'])) {
        $dbhost = '127.0.0.1';
        $dbuser = 'root';
        $dbpass = '';

        $conn = mysql_connect($dbhost, $dbuser, $dbpass);

        if(! $conn ) {
           die('Could not connect: ' . mysql_error());
        }

        $id = $_SESSION['s_usuario'];

        $sql = "UPDATE game ". "SET credito = 30 ". 
           "WHERE id = $id" ;
        mysql_select_db('gbwc');
        $retval = mysql_query( $sql, $conn );

        if(! $retval ) {
           die('Could not update data: ' . mysql_error());
        }
        echo "Updated data successfully\n";

        mysql_close($conn);
     } else {
        ?>
          <form method="post" action="<?php $_PHP_SELF ?>">
           <input type="button" name="reseta_wl" value="Resetar">
          </form>
        <?
     }
  ?>
  • What error appears?

  • The mistake starts when I read mysql in the code. And not giving echo in the PHP_SELF.

  • There is no error... Actually no action is taking place when I click the button!

  • @Eduardohenrique, I have a PDO class on github. Feel free to use. https://github.com/LucaoA/connectionPDO/blob/master/Connection.php

  • For the record, if you want to send it to the same page, just omit the action. BUT, I don’t think it’s bad if you specify the address, because although it’s not a very common thing to happen, explain the URL, avoid "clickjacking" if someone calls your form in iframe. As @Magichat said, to access PHP_SELF is used $_SERVER["PHP_SELF"]

2 answers

1

0

I’ve come to this result...

<?
 if(isset($_POST['reseta_wl'])) {
    $id = $_SESSION['s_usuario'];
    $result = mysql_query("select * from game where id = '$id'");
    $usr = mysql_fetch_array($result);
    $coin = $usr['credito'];
    $sql = "UPDATE game SET credito = $coin-20 WHERE id = '$id'";
    if(mysql_query($sql, $link)){
       echo "Atualizado com sucesso.";
    } else{
       echo "Not able to execute $sql.";
    }


    mysql_close($link);
 } else {
    ?>
      <form method="post" action="">
       <input type="submit" name="reseta_wl" value="Resetar">
      </form>
    <?
 }?>

But the select q I’m trying to give in the table is not working, since the mathematical operation is not being performed. Does anyone know where I’m going wrong?

  • 1

    I advise you to open a new question with your code, asking about arithmetic operations in sql query...

  • I recommend adding a line , to debug, right after the first query: echo mysql_num_rows($result);

  • This way you can confirm if it is returning values.

Browser other questions tagged

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