-1

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

  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>"
        ;}?>
4

2 回答 2

0

你几乎已经有了那里的功能,只需要整理一下。

尝试这样的事情:

<?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
    }
?>
于 2013-09-03T18:05:58.307 回答
0

基本思想是如果新就添加一条记录,如果没有就更新。您可以做的是使用 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
    ");

// 检查用户在 db 中是否有内容 $result = 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

小鲍比桌

跨站脚本

于 2013-09-03T18:06:12.577 回答