dongqigu0429 2015-06-20 12:50
浏览 23
已采纳

PHP数学在sql值ZERO中不起作用

I am trying to math between value of 0 but in php code it's work properly but when that value come from database then it does not work.

Here is a demo to test only PHP code:

<?php
$a = 0;
echo $a+1;
?>

The result of this code

1

That ran properly, but when it came from database table then the "+" math does not work properly.

Here is my full code:

<?php 
ob_start();
require('config2.php');

// move_up & move_down ** up_down //

$move_up = $_POST['move_up'];

$move_down = $_POST['move_down'];

$up_down1 = $_POST['up_down'];

$up_down11 = $_POST['up_down'];

$forid = $_POST['forid'];

$up = $up_down1-1;

$down = $up_down11+1;

if($move_up) {
    if($up_down1) {
        $insert=mysql_query("UPDATE widgets_mata SET `up_down` = '$up' WHERE `widgets_mata_id` = $forid");
        header("Location: yyy.php");
    }
}

if($move_down) {
    if($up_down11) {
        $insert2=mysql_query("UPDATE widgets_mata SET `up_down` = '$down' WHERE `widgets_mata_id` = $forid");
        header("Location: yyy.php");
    }
}   
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php $query2=mysql_query("SELECT * FROM widgets_mata ORDER BY up_down ASC");
while($rows2=mysql_fetch_assoc($query2))
{ 
    $widgets_mata_id2=$rows2['widgets_mata_id'];
    $widgets_id2=$rows2['widgets_id'];
    $up_down2=$rows2['up_down'];
    $field2=$rows2['field'];

    if($field2=='R') {
        $query3 = "SELECT * FROM widgets WHERE `widgets_id`=$widgets_id2";
        $result3 = mysql_query($query3);

        while($rows3 = mysql_fetch_assoc($result3))
        {
            $widgets_id3=$rows3['widgets_id'];
            $fu_name3=$rows3['fu_name'];
            $func_name3=$rows3['func_name'];
            $func3=$rows3['func'];
?>
  <h3> <?php echo $fu_name3; ?></h3>
  <div>
   Plaese select your options 
   <br />
   <br />
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <label>
  <input name="forid" type="hidden" id="hiddenField" value="<?php echo $widgets_mata_id2; ?>" />
  <input name="up_down" type="hidden" id="hiddenField" value="<?php echo $up_down2; ?>" />
    <input type="submit" name="move_up" value="Move Up" /> &nbsp; &nbsp;
    <input type="submit" name="move_down" value="Move Down" />
    <br />
    <br />
    <input type="submit" name="button" id="button" value="Save" />
  </label>
  &nbsp;
  &nbsp;
  <label>
    <input type="submit" name="button2" id="button2" value="Delete" />
  </label>
  &nbsp;
  &nbsp;
  <label>
    <input type="submit" name="button3" id="button3" value="Cancel" />
  </label>
</form>


  </div>
<?php 
        }
    }
}
?>
</body>
</html>

Here is my config2.php

<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("myweb");
?>

Here is my SQL

CREATE TABLE IF NOT EXISTS `widgets_mata` (
  `widgets_mata_id` int(11) NOT NULL,
  `widgets_id` int(11) NOT NULL,
  `up_down` int(10) DEFAULT '1',
  `field` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `widgets_mata`
--

INSERT INTO `widgets_mata` (`widgets_mata_id`, `widgets_id`, `up_down`, `field`) VALUES
(1, 1, 0, 'R'),
(2, 2, 2, 'R'),
(3, 3, 3, 'R');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `widgets_mata`
--
ALTER TABLE `widgets_mata`
  ADD PRIMARY KEY (`widgets_mata_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `widgets_mata`
--
ALTER TABLE `widgets_mata`
  MODIFY `widgets_mata_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

My problem is with the "0" here:

enter image description here

Then the addition is not working. But when the value is "1"or"2"or"3" then the code works properly.

If you don't understand my question so please run my codes I give full code here

  • 写回答

1条回答 默认 最新

  • doulai1910 2015-06-20 13:54
    关注

    I having run you code and I just found you solution. in your Phpmyadmin database in side mate table cell name up_down update that, please goto Structure>up_down click Change>Attributes{UNSIGNED ZEROFILL} select that>Save And then you can test that you will find the different. enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!