forumnz Posted January 3, 2008 Share Posted January 3, 2008 I have a user select their region from a drop down list. I want some regions to be isolated and some to include others (if that makes sense). Anyway, the user submits the form with the region value. The variable is named $region. I wrote this extra but, but the script still inserts 0 into all three db columns. if($region == 0) { $reg1 == 0; $reg2 == 0; $reg3 == 0; } elseif($region >=2 && $region <=13) { $reg1 == 0; $reg2 == 2; $reg3 == $region; } elseif($region >=14 && $region <=21) { $reg1 == 0; $reg2 == 14; $reg3 == $region; } Thanks for your help! Sam. Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2008 Share Posted January 3, 2008 Have you echoed $region to see what it really contains? You would also need to post your code for someone in a forum to be able to see what it might be doing that could cause incorrect values. Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429760 Share on other sites More sharing options...
forumnz Posted January 3, 2008 Author Share Posted January 3, 2008 Ok here it is: <?php $buzzname=$_POST['buzzname']; $sub=$_POST['sub']; $descr=$_POST['descr']; $keyw=$_POST['keyw']; $addst=$_POST['addst']; $addsu=$_POST['addsu']; $addci=$_POST['addci']; $region=$_POST['region']; $addpc=$_POST['addpc']; $ph=$_POST['ph']; $ph2=$_POST['ph2']; $freeph=$_POST['freeph']; $fax=$_POST['fax']; $email=$_POST['email']; if($region == 0) { $reg1 == 0; $reg2 == 0; $reg3 == 0; } elseif($region >=2 && $region <=13) { $reg1 == 0; $reg2 == 2; $reg3 == $region; } elseif($region >=14 && $region <=21) { $reg1 == 0; $reg2 == 14; $reg3 == $region; } $uid = $_SESSION['id']; include('dbcon.php'); $sql="INSERT INTO cmads (buzzname, sub, descr, keyw, addst, addsu, addci, region, region2, region3, addpc, userid, ph, ph2, freeph, fax, email) VALUES ('$buzzname','$sub','$descr','$keyw','$addst','$addsu','$addci','$reg1','$reg2','$reg3','$addpc','$uid','$ph','$ph2','$freeph','$fax','$email')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } echo "1 record added" . $reg1 . $reg2 . $reg3 . "<br>" . $region; ?> As you can see at the bottom I echoed the three regions, but it only displayed: 1 record added 13 Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429774 Share on other sites More sharing options...
drummer101 Posted January 3, 2008 Share Posted January 3, 2008 Add quotes around the numbers, right now they're all integers instead of variable values. Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429779 Share on other sites More sharing options...
forumnz Posted January 3, 2008 Author Share Posted January 3, 2008 Like this? if($region == '0') { $reg1 == '0'; $reg2 == '0'; $reg3 == '0'; } elseif($region >='2' && $region <='13') { $reg1 == '0'; $reg2 == '2'; $reg3 == $region; } elseif($region >='14' && $region <='21') { $reg1 == '0'; $reg2 == '14'; $reg3 == $region; } Still doesn't work. Any ideas? Sam. Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429781 Share on other sites More sharing options...
duclet Posted January 3, 2008 Share Posted January 3, 2008 Change the == to = within the body of the if's Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429782 Share on other sites More sharing options...
drummer101 Posted January 3, 2008 Share Posted January 3, 2008 Change the == to = within the body of the if's Bah, can't believe I missed that. Oh well, that happens when you're skimming code at work. @forumnz: I generally use d ouble quotes for everything except for inside square ([ ]) brackets. I have no idea if one or the other is better/faster in terms of code execution or parsing. Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429811 Share on other sites More sharing options...
duclet Posted January 4, 2008 Share Posted January 4, 2008 Single quotes are faster. Using double quotes will basically trigger PHP to look for variables within the string which will cause some unnecessary checking. The following article provides some optimization tips when coding in PHP so read it if you want: http://tiger-inc.com/php/optimize-your-code/ Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429833 Share on other sites More sharing options...
drummer101 Posted January 4, 2008 Share Posted January 4, 2008 so .. echo 'This is a string'; would be faster than? echo "This is a string"; Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429881 Share on other sites More sharing options...
duclet Posted January 4, 2008 Share Posted January 4, 2008 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429886 Share on other sites More sharing options...
redarrow Posted January 4, 2008 Share Posted January 4, 2008 so .. echo 'This is a string'; would be faster than? echo "This is a string"; yep becouse ' single quotes dont phase throw php like double quotes.... in other words php use the " double quotes to do somethink with.......... example <?php echo ' this is a litrall $redarrow will not be echoed out'; $redarrow='hi there i am redarrow'; echo"this is a phased $redarrow will echo"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/84380-wrong-var-posted-to-db/#findComment-429887 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.