dongxiaolin2801 2016-04-14 12:30
浏览 23
已采纳

检查并更新数据库条目php

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");
}
  • 写回答

1条回答 默认 最新

  • dqf2015 2016-04-14 13:05
    关注

    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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了