I have 2 bits of code which are both doing what they are supposed to:
A) This code shows and updates the field in the database based on the value in the input field
<input id="input" class="input-small" type="text" name='C_N_C' value="<?php if(isset($_POST['C_N_C'])) echo $_POST['C_N_C']; else echo $C_N_C; ?>">
B) This code counts the values in another db and returns the number or rows
<?php
$query = "SELECT COUNT(C_ID) FROM CfortoC WHERE C_ID = '{$_GET['id']}'";
$results = mysql_query($query);
$rows = mysql_fetch_array($results);
echo $rows[0];
?>
Individually these work fine!
**What I need to do is to run the query FIRST then have that value displayed in the input box. Then when "UPDATE" is clicked to update the C_N_C value to that of the query! **
I hope that makes sense! Thanks for any help!
MORE CODE
As requested here is more code!
<div class="control-group">
<label class="control-label" for="input">C Label </label>
<div class="controls">
<input id="input" class="input-small" type="text" name='C_N_C' value="<?php if(isset($_POST['C_N_C'])) echo $_POST['C_N_C']; else echo $C_N_C; ?>">
<?php if(isset($_GET['id'])) { ?>
<?php
$query = "SELECT COUNT(C_ID) FROM CfortoC WHERE C_ID = '{$_GET['id']}'";
$results = mysql_query($query);
$rows = mysql_fetch_array($results);
echo $rows[0];
?>
<?php } ?>
</div>
</div>
I believe this is what you're looking for. The first time through the page, the input will display the value of $C_N_C
, which is the result of the SELECT COUNT
statement (provided there is a value for $_GET['id']
). When a user enters data into that field and submits the form, the second time around $_POST['C_N_C']
will be set, and the input will display that value instead.
<?php
if(isset($_GET['id'])) {
$id = mysql_real_escape_string($_GET['id']);
$query = "SELECT COUNT(C_ID) FROM CfortoC WHERE C_ID = '{$id}'";
$results = mysql_query($query);
$rows = mysql_fetch_array($results);
$C_N_C = $rows[0];
}
if(isset($_POST['C_N_C'])){
$C_N_C = htmlspecialchars($_POST['C_N_C']);
}
?>
<div class="control-group">
<label class="control-label" for="input">C Label </label>
<div class="controls">
<input id="input" class="input-small" type="text" name="C_N_C" value="<?php echo $C_N_C; ?>">
</div>
</div>
Also, mysql_ functions are deprecated as of PHP 5.5. You should make the switch to mysqli functions or PDO