Hi everyone, I have been trying to write a php script to display a div and its contents depening on a result from mysql.

Inside the div is a basic input form etc, nothing special, some error & validation checking.

What I would like to do is, to only display the div and contents if a result from my database was true.

        $res = mysql_query("SELECT var, var1 FROM tbl_name WHERE var='$var' && var1='0'") or die(mysql_error()); 

            $checkvar = mysql_num_rows($res);

            if($checkvar > 0){ 

                $row = mysql_fetch_array($res);
                $var = $row['var'];

                if ($var1 == 0)
                { //if result is false
                // do not display the div and contents
                }
                else
                {
                //if the result = 1 display the div and contents here
                }
            }

Here is the complete form. Inside the form there are php tags so I am not sure how to get around this

    <!-- Begin #optIn -->
    <div id="optIn">
        <div class="optBox">
        <h3><?php if($headline){echo $headline; }?></h3>
        <p><?php if($dealdetails){echo $dealdetails; }?></p>

        <form style="width:100%;" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
        <label class="previewLabel" for="awf_field"></label>

       <select name="country" tabindex="1" class="select" value='<?php echo $country; ?>' id="country" onChange="get_country_code();">
                            <?php if($country!=''){?>
                            <option value="<?php echo $country;?>"><?php echo $country;?></option>
                            <?php }else{ ?>
                            <option value="">Please Select Country</option>
                            <?php }?>
                            <?php
                                 while($country_arr = mysql_fetch_array($result_country)){
                                    $tag_selected = ( (isset($_POST['country']) and $_POST['country'] == $country_arr['Country'] )? 'selected="selected"' : '' );
                                     ?>
                                     <option value="<?php echo $country_arr['Country']?>" <?php echo $tag_selected; ?>><?php echo $country_arr['Country']?></option>
                                     <?php
                                 }

                            ?>
                    </select>
        <input type="text" class="text" id="smsmobnum" maxlength="11" size="11" name="smsmobnum" title="Enter your full mobile number" value="<? echo $smsmobnum; ?>" tabindex="2">

        <input type="submit" name="submit" class="submit btn" title="Click &amp; Be Rewarded" value="Get This Deal Now" tabindex="502">
        <input type="hidden" name="dialingCode" id="dialingCode" maxlength="5" size="5" readonly value="<? echo $dialingCode; ?>">
        </form> 
        <div class="p">
        <?php printErrors(); ?>
        <?php echo $msg_success; ?></div>
        <p>Select your country and enter your full mobile number to subscribe. <strong>All Sms Coupons &amp; Deals Are 100% FREE To Receive.</strong></p>      
        <div align="left">
        <form action="unsms.php" method="LINK"><input type="submit" class="orange-btn" title="Remove your mobile number" value="Unsubscribe"></form>
        </div>
        </div>
        <div class="optBoxb"></div>
    </div>
    <!-- end #optIn -->

Any help would be appreciated,

Dani AI

Generated

A concise, robust fix is to run a safe query that returns the single column that controls display, set a simple boolean flag from that result, then wrap the existing markup in a single conditional. As noted, the variable used for the test was never assigned — the fetched row value must be used (or cast to an int) rather than an uninitialized variable.

Example (using PDO/prepared statement and a clear column name):

// assume $pdo is a configured PDO instance and $var is validated
$stmt = $pdo->prepare('SELECT show_form FROM tbl_name WHERE var = :var LIMIT 1');
$stmt->execute([':var' => $var]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$showForm = ($row && (int)$row['show_form'] === 1);

Keep the HTML out of the query logic: put the full form markup in the template or a separate include and only render it when the flag is true:

if ($showForm) {
    include 'optin-form.php';
}

Extra notes and troubleshooting:

  • Process POST handling (validation/saving) before deciding whether to show the form, then query to set the flag on every page load.
  • Use prepared statements (PDO/mysqli) to prevent SQL injection; avoid deprecated mysql_* functions.
  • Normalize the DB column (use a clear name like show_form or enabled) and cast to int for reliable comparisons.
  • For debugging, log the fetched row (var_export) to confirm expected values, and use htmlspecialchars() when echoing user-supplied values into attributes to prevent XSS.

This approach keeps presentation and logic separate, fixes the undefined-variable problem flagged by , and is resilient for later PHP versions.

Line 10 checks $var1, but it is not given any value. Perhaps you wanted to check $var ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.