【问题标题】:If content exists in database, provide form to update it - else provide form to add new row如果数据库中存在内容,请提供表格以更新它 - 否则提供表格以添加新行
【发布时间】:2013-09-03 17:47:46
【问题描述】:

一切都错了。我需要在我的网站上输出一个表格,该表格将执行以下两件事中的一件:

  1. 如果用户在数据库中已有内容,则提供一个发布给自己的表单以更新现有内容。

  2. 如果用户在数据库中没有内容,提供表单让用户向数据库添加信息。

表单应自行提交以保持编码整洁。我陷入了正确的混乱。我会展示我目前所拥有的,但我陷入了混乱。

//look in db to see if content exists, if it does set variable
    $result = mysql_query(
            "SELECT * from tbl_profiles 
            WHERE user_id = $who
        ");

    while($row = mysql_fetch_array($result))
         {
             $profileText = $row['text'];
             }

// Check if user has content in db

        $result = mysql_query(
        "SELECT * FROM tbl_profiles WHERE user_id='$who'");

    if(mysql_fetch_array($result) !== false){
        echo 
        '<form action="../edit/indexUpdate.php" method="post" name="edit">
                Comments:<br />
                <textarea name="updatedText" id="comments">' .
                $profileText .'
                </textarea><br />
                <input type="submit" value="Submit" />
            </form>'
        ;}
    else{
        $profileText = $row['text'];
        echo 
        "<form action='../edit/index.php' method='post' name='add'>
                Comments:<br />
                <textarea name='comments' id='comments'>" .
                $profileText 
                ."</textarea><br />
                <input type='submit' value='Submit' />
            </form>"
        ;}?>

【问题讨论】:

    标签: php sql database forms


    【解决方案1】:

    那里的功能已经差不多了,只是需要整理一下。

    试试这样的:

    <?php
    //look in db to see if content exists, if it does set variable
    $profileText="";
    if($result = mysql_query("SELECT * from tbl_profiles WHERE user_id = $who")) {
        while($row = mysql_fetch_array($result))
            {
             $profileText .= $row['text'];
             }
        ?>
        <form action="../edit/indexUpdate.php" method="post" name="edit">
                Comments:<br />
                <textarea name="updatedText" id="comments">
                <?php echo $profileText; ?>
                </textarea><br />
                <input type="submit" value="Submit" />
        </form>
        <?php
    } else {
        ?>
         <form action='../edit/index.php' method='post' name='add'>
                Comments:<br />
                <textarea name='comments' id='comments'>
                <?php echo $profileText; ?> 
                </textarea><br />
                <input type='submit' value='Submit' />
         </form>
    <?php
        }
    ?>
    

    【讨论】:

      【解决方案2】:

      基本思想是如果新就添加一条记录,如果没有就更新。您可以做的是使用 id 来表示记录,如果是新条目,则使用 -1

      类似的东西:

      //Defaults
      $recordid=-1;
      $name='';
      $comments='';
      //look in db to see if content exists, if it does set variable
      $result = mysql_query(
              "SELECT * from tbl_profiles 
              WHERE user_id = $who
          ");
      

      // 检查用户在数据库中是否有内容 $结果 = mysql_query( "SELECT * FROM tbl_profiles WHERE user_id='$who'");

      if(mysql_fetch_array($result) !== false){
         //Yes.  Get the id
          $recordid = $result->id;
         //Get the values
         $name= $result->name;
         $comments= $result->name;
      
      }
      

      <form action="../edit/index.php" method="post" name="formdata">
        <input type="hidden" name="recordid" value="<? echo htmlspecialchars($recordid) ?>">
      
        <input type="hidden" name="name" value="<? echo htmlspecialchars($name) ?>">
        <textarea name="comments" id="comments"><? echo htmlspecialchars($comments) ?></textarea>
        <input type="submit" value="submit"/>
      </form>
      

      这样一个新的表单会有一个 -1 而一个现有的表单会有一个 id。

      另外一点,清理您的 SQL 输入以及您在 HTML 中输出的内容以阻止 SQL 注入非常重要。供您参考:

      SQL

      Little Bobby Tables

      Cross Site Scripting

      【讨论】:

      • 这看起来不错,但它在 Dreamweaver 中被标记为错误。这是 PHP 吗?
      • 它围绕“var”选项进行标记
      • @user2527750 - 很抱歉...在输入代码时让一些 JavaScript 融入其中。我已经更正了。
      猜你喜欢
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      相关资源
      最近更新 更多