若数据库中存在内容,则提供表单进行更新,否则提供表单添加新行


If content exists in database, provide form to update it - else provide form to add new row

一切都出了问题。我需要在我的网站上输出一个表格,它将做2件事中的1件:

  1. 如果用户在数据库中已经有内容,请提供一个发布到self的表单以更新现有内容。

  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
//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
    }
?>

基本思想是,如果是新的,则添加一条记录,如果不是,则进行更新。你可以做的是使用一个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
    ");

//检查用户在数据库中是否有内容$result=mysql_query("从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

小鲍比桌

跨站点脚本