簡體   English   中英

使用IF / IF..ELSE PHP語句比較兩個變量並將其插入MYSQL

[英]comparing two variables using IF/IF..ELSE statement for PHP and inserting into MYSQL

我正在對兩個變量進行計數驗證,值為$ 7的$ row2和值為11的$ row3。我想要實現的是,只能將較高的值插入數據庫。 現在的問題是$ 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);
}

永遠不要寫兩次連接...您應該從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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM