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 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在