dongzong3053 2017-06-26 21:13
浏览 44

PHP - SQL:INSERT INTO作为多个表单和多个提交的一部分

Basically what I have is facebook-like DB, with posts and comments. What I'm trying to do is having the possibility to add a specific comment to a post. When pressing the submit of the appropriate comment - the data should go into the DB. But no matter what I do, even though I can make each submit button relate to a specific comment - I can't make the INSERT INTO work.

This is the problematic code:

echo '<div style="clear:both;"></div>';
echo '<h5 style="text-align: left;  color:red;">Here you can comment on this post:</h4>';
$iter=$name1;
 $iter2=$name1*2;
 echo "<form action='' method='Post'>";
 echo "<input type='hidden' name='ToSubmit' value='".$name1."'>";
 echo "<input class='z' type = 'submit' name ='$iter' value ='Insert'>";  
 echo "Please insert the comment (You are limited to 250 chars): <input type='text' name='$iter2'><br>";
 echo "</form>";     
 if (isset($_POST["$iter"]))
     {
      $date2=date("Y-m-d H:m:s");
      $neg= -1;
      echo $neg;
      $CommentContent=$_POST["$iter2"];
      echo $CommentContent;
      echo $date2;
      echo $USERID;
      echo $USERNAME;
      echo $name1;
      $sqlComment="INSERT INTO FComment (comment, tStamp, userId, uName, comment_commented, post_commented ) VALUES('".$_POST['$iter2'] ."', '".$date2."', '". $USERID ."', '".$USERNAME."', '".$neg."', '".$name1."');";
      $res55=sqlsrv_query($conn, $sqlComment );
       }   

I tested the INSERT INTO query on its on, it's working fine. All the echos show that the information is stored properly into the variables, it just won't add it to the DB.

Any ideas?

EDIT: So I figured half o the problem - the SQL queries had the same name, so I'd get duplicated queries. I tried solving this by going to dynamic variable:

${sqlComment}="INSERT INTO FComment (comment, tStamp, userId, uName, comment_commented, post_commented ) VALUES('".$CommentContent ."', '".$date2."', '". $USERID ."', '".$USERNAME."', '".$neg."', '".$name1."');";
${res55}=sqlsrv_query($conn, ${sqlComment});

But now a new problem arises - I need to press the submit button twice for an entry to be added, and it adds a blank record with it. Any ideas?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化