0

i am doing a count validation on two variables , $row2 with a value of 7 and $row3 with a value of 11. What i want to achieve is that the higher value can only be insert into the DB. The problem now is that $row3 value is bigger than $row2. however it always insert $row2 value into the DB. Is there any wrong with my validation codes?

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);
}

on my html table , i echo the data out like this:

 <?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];?>"  />

using a POST form i post it to another page where i need to do a count validation to see if which variable $row2 or $row3 have a higher count value and then i will insert the higher value into the DB

admin.php page

$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);
}
6
  • Which block do you think is executing? Commented Jan 18, 2012 at 15:03
  • As an aside, don't forget to account for the case where they're equal, unless you really don't want anything inserted in that case. Commented Jan 18, 2012 at 15:03
  • 1
    Are you sure that $row2 and $row3 are numeric? Because if they are strings '7' is greater then '11' and all is correct. Commented Jan 18, 2012 at 15:05
  • Please check the output of var_dump($row2, $row3); Commented Jan 18, 2012 at 15:10
  • please provide code you are using for $row2 and $row3.. You have problem in this only... Commented Jan 18, 2012 at 15:39

1 Answer 1

1

Never write connection twice... You should take connect part out of if..else statement...

Check below if it works...

$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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.