0

How do i check the value of a database entry and when its 0 update it to 1 in php?

At the moment it always comes back with Number Not used even when i set the 0 to 1 in the database my self....

Table:

========================================================

MasterNumbers:
number          |   used
01513242        |   0
01012233        |   0
23566545        |   0

========================================================

code:

$name = "01513242";
$checkForUsed  = mysql_query("SELECT * FROM MasterNumbers WHERE `used`= '$name'");
$numrowsUsed = mysql_num_rows($checkForUsed);

if ($numrowsUsed == 0 )
{
    //update used (on 01513242) form 0 to 1
    die("Number Not used");
}
else
{
    die("Number is used");
}
kevin ver
  • 831
  • 4
  • 14
  • 37
  • 1
    you missed the semicolon after $name variable, is this typo or your code is like this. – JiteshNK Apr 14 '16 at 12:31
  • 2
    `WHERE number = '$name'` – Daan Apr 14 '16 at 12:32
  • `update MasterNumbers set used = 1 where number = ? and used = 0`. And please, stop using `mysql_query`. – Alex Blex Apr 14 '16 at 12:33
  • @user2314870 Yea that the most obvious and important error they have made – RiggsFolly Apr 14 '16 at 12:33
  • thats a typo on my end, a user normally types in a number – kevin ver Apr 14 '16 at 12:33
  • 1
    Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Apr 14 '16 at 12:36
  • Also you dont actually **get the result of your query anywhere** so you can test is the value of `used` is zero – RiggsFolly Apr 14 '16 at 12:38
  • 1
    I think maybe the best answer you could be given is **Hit the books** – RiggsFolly Apr 14 '16 at 12:39
  • Try to mysqli or PDO below mysqli example `$checkForUsed = mysqli_query("SELECT * FROM MasterNumbers WHERE used = %d"); $checkForUsed = sprintf($checkForUsed, $name); $numrowsUsed = mysqli_num_rows($checkForUsed);` – JiteshNK Apr 14 '16 at 12:40
  • try to use var_dump for $numrowsUsed and check what you get. – Sela Yair Apr 14 '16 at 14:35

1 Answers1

1

I did not check the following class, since I don't want to set up a database for this. Some class like this should do the job.

<?php
    class update_numrow_class{
        // Database settings
        protected $_mysqli = false;
        protected function __construct($number){
            $this->username => 'db_username',
            $this->password => 'db_password',
            $this->host => 'localhost',
            $this->database => 'db_name',
            $this->charset => 'utf8',
            $this->number => $number
        }

        // Function to connect to the database
        function connect(){
            $mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
            if ( $mysqli->connect_errno ) {
                $this->_mysqli = false;
            }else {
                $mysqli->set_charset($this->charset);
                $this->_mysqli = $mysqli;
            }
        }

        // Function to execute an sql query
        function execute_query($sql){
            if($results = $this->_mysqli->query($sql)){
                return $results;
            }else{
                return false;
            }
        }

        // Function to check if the number was used
        function check_number(){
            $this->connect();
            $number_result = $this->execute_query("SELECT used FROM MasterNumbers WHERE number =" . $this->number);
            if($number_result == 0){
                return false;
            }else{
                return true;
            }
        }

        // Function to update the used value
        function update_number(){
            $number_check = $this->check_number();
            if(!$number_check){
                $this->connect();
                $update_result = $this->execute_query("UPDATE MasterNumbers SET used = 1 WHERE number = " . $this->number);
                return 'Number: ' . $this->number . ' successful updated.';
            }else{
                return 'Number: ' . $this->number . ' was already used.';
            }
        }
    }
?>

Would be used like:

<?php
    require_once('update_numrow_class.php');
    $update_object = new update_numrow_class(01513242);
    $update_response = $update_object->update_number();
?>
d4ne
  • 158
  • 2
  • 14