【问题标题】:comparing two variables using IF/IF..ELSE statement for PHP and inserting into MYSQL使用 PHP 的 IF/IF..ELSE 语句比较两个变量并插入 MYSQL
【发布时间】:2012-01-18 14:59:56
【问题描述】:

我正在对两个变量进行计数验证,$row2 的值为 7,$row3 的值为 11。我想要实现的是更高的值只能插入到数据库中。现在的问题是 $row3 的值大于 $row2。但是它总是将 $row2 值插入数据库。我的验证码有问题吗?

function tweetCount($hashtag) {

$url = 'http://search.twitter.com/search.atom?q='.urlencode($hashtag).'&rpp=100&result_type=recent';

//echo "<p>Connecting to <strong>$url</strong> ...</p>";
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec ($ch);
curl_close ($ch);

//If you want to see the response from Twitter, uncomment this next part out:
//echo "<p>Response:</p>";
//echo "<pre>".htmlspecialchars($xml)."</pre>";

$affected = 0;
$twelement = new SimpleXMLElement($xml);
foreach ($twelement->entry as $entry) {
    $text = trim($entry->title);
    $author = trim($entry->author->name);
    $time = strtotime($entry->published);
    $id = $entry->id;

    //echo count($entry->text);
   // echo "<em>Posted ".date('n/j/y g:i a',$time)."</em><p>Tweet from <b><u>".$author."</u></b>: <strong>".$text."</strong>  </p>";
    //echo "<br/>";
}
    //echo count($twelemtnt);
    //echo count($entry);
    echo $number_of_tweets = count($twelement->entry);
}

在我的 html 表格中,我将数据回显如下:

 <?php echo tweetCount($row[2]); ?>

  <input type="hidden" name="row2" value="<?php echo tweetCount($row2[2]);?>"  />
  <input type="hidden" name="row2Ini" value="<?php echo $row2[1];?>"  />
  <input type="hidden" name="row2Sch" value="<?php echo $row2[2];?>"  />

使用 POST 表单我将其发布到另一个页面,我需要在其中进行计数验证,以查看哪个变量 $row2 或 $row3 具有更高的计数值,然后我会将更高的值插入数据库中

admin.php 页面

$row2 = $_POST['row2'];
$row2Ini = $_POST['row2Ini'];
$row2Sch = $_POST['row2Sch'];


if ( $row2 > $row3 )
{
    echo "<br>row2 is more than row 3";
    $con = mysql_connect("localhost","root","password");
            if (!$con)
              { die('Could not connect: ' . mysql_error());}
            mysql_select_db("schoutweet", $con);
            $sql2="INSERT INTO matchTable (schInitial, schName,position)VALUES
            ('$_POST[row2Ini]','$_POST[row2Sch]','top4')";
            if (!mysql_query($sql2,$con)){die('Error: ' . mysql_error());}echo "ROW2 record added!<BR>";
            mysql_close($con);
}
else if ($row2 < $row3)
{
    echo "row3 count is more than row 2";
    $con = mysql_connect("localhost","root","password");
            if (!$con)
              { die('Could not connect: ' . mysql_error());}
            mysql_select_db("schoutweet", $con);
    $sql="INSERT INTO matchTable (schInitial, schName,position)VALUES('$_POST[row3Ini]','$_POST[row3Sch]','top4')";
            if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}echo "ROW3 record added!<BR>";
            mysql_close($con);
}

【问题讨论】:

  • 你认为哪个块正在执行?
  • 顺便说一句,不要忘记考虑它们相等的情况,除非你真的不希望在这种情况下插入任何东西。
  • 你确定 $row2 和 $row3 是数字吗?因为如果它们是字符串 '7' 大于 '11' 并且一切都是正确的。
  • 请检查var_dump($row2, $row3);的输出
  • 请提供您用于$row2$row3.. 的代码。您仅在此有问题...

标签: php mysql if-statement


【解决方案1】:

永远不要写两次连接...您应该将连接部分从 if..else 语句中取出...

检查下面是否有效...

$con = mysql_connect("localhost","root","password");
        if (!$con)
          { die('Could not connect: ' . mysql_error());}
        mysql_select_db("schoutweet", $con);


if ( $row2 > $row3 )
{
echo "<br>row2 is more than row 3";
        $sql2="INSERT INTO matchTable (schInitial, schName,position)VALUES
        ('$_POST[row2Ini]','$_POST[row2Sch]','top4')";
        if (!mysql_query($sql2,$con)){die('Error: ' . mysql_error());}echo "ROW2 record added!<BR>";

}
else 
{
    $sql="INSERT INTO matchTable (schInitial, schName,position)VALUES('$_POST[row3Ini]','$_POST[row3Sch]','top4')";
            if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}echo "ROW3 record added!<BR>";

 }
            mysql_close($con);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 2022-11-16
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多