dsgffz2579 2014-04-04 16:08
浏览 10
已采纳

SQL将页面查询的结果发布到db

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>
  • 写回答

1条回答 默认 最新

  • dssqq64884 2014-04-04 20:12
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用