doutongfu9484 2013-06-21 06:30
浏览 14
已采纳

从大型表格发布数据并在错误后保留输入数据的最佳方式

This is a "best practice"/"most efficient" question. I have a large form (20+ fields). Form post into one large MySQL table.

No I can't break up the form and no, I can't break up the table (its being used to hold measurements); used by admin sales reps. Also, I don't want to use Javascript.

I know I can do this: HTML

<form action="etc.php" method="post">    
<input type="text" name="neck" value="">
<input type="text" name="arm" value="">
<input type="text" name="back" value="">
<input type="text" name="chest" value="">
<input type="text" name="legs" value="">
<submit button>

PHP

<? 
$_POST['neck']; 
$_POST['back']; 
$_POST['arm']; 
$_POST['chest']; 
$_POST['legs']; 
$postMeasurements = "INSERT INTO measurements (etc, etc, etc,) VALUES (etc, etc, etc) WHERE etc='etc'; query ($postMeasurements);
?>

But is there a faster way? Instead of having to declare each individual post, simply just run a loop that takes all the data post and inserts into the table. Even if the data has be in the same order of the columns of the table or if the input names have to be the same as the table column names is fine by me; I am just getting tired have to keep writing all these $_POST variables into.

Second question: What is the best way to hold this data in case of an error? As it stands now, I hold everything in $_SESSION (one session variable for each input), then redirect back to the form page if there is an error with an error message. then echo each $_SESSION variables as that inputs value.

Thanks,

  • 写回答

3条回答 默认 最新

  • duanniedang3946 2013-06-21 06:35
    关注

    if the fields as the exact names as the field names. post can only have the fields and nothing else

     //if $_POST has the form then, also this is very unsafe because there is no injection prevention too
    
     $sql = "INSERT INTO table (" . implode(",", array_keys($_POST)) . ")"
          . "VALUES ('" . implode("','", array_values($_POST)) . "')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL