javascript - 如何使用php和jquery在mysql中提交多个数据

标签 javascript php jquery mysql mysqli

如何在表中提交多个值请帮助我使用此代码 此代码仅在数据库中提交 1 个条目,但我想在数据库中输入多个值,我该怎么做

现场演示 http://jsfiddle.net/eruZC/3/

这是entry.html

      <script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>

  <link rel="stylesheet" type="text/css" href="http://fiddle.jshell.net/css/result-light.css">

  <style type='text/css'>

  </style>



<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(document).ready(function(){
    var id=1;

    $("#butsend").click(function(){
        $("#table1").append('<tr valign="top"><td width="100px">'+(id++)+'</td><td width="100px">'+$("#sname").val()+'</td><td width="100px">'+$("#age").val()+'</td><td width="100px"><a href="javascript:void(0);" class="remCF">Remove</a></td></tr>');
    });

    $("#table1").on('click','.remCF',function(){
        $(this).parent().parent().remove();
    });
});
});//]]>  

</script>


    </head>
    <body>
      <form action="submit.php" d="form1" name="form1" method="post">
    <label>Student Name</label><input type="text" name="sname" id="sname"></br>
    <label>Student Age</label><input type="text" name="age" id="age"></br>
    <input type="button" name="send" value="Add" id="butsend"></br>
     <input type="button" name="submit" value="Submit" ></br>
    </form>
    <table id="table1" name="table1" border="">
    <tbody>
    <tr><th width="100px">ID</th><th width="100px">Name</th><th width="100px">Age</th><th width="100px"></th><tr>

    </tbody>
    </table>

    </body>


    </html>

这是php页面submit.php

<?php
/* 
 NEW.PHP
 Allows user to create a new entry in the database
*/

 // creates the new record form
 // since this form is used multiple times in this file, I have made it a function that is easily reusable
 function renderForm($sname, $age, $error)
 {
 ?>


 <?php 
 // if there are any errors, display them
 if ($error != '')
 {
 echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
 }
 ?> 




 <?php 
 }




 // connect to the database
 include('connect-db.php');

 // check if the form has been submitted. If it has, start to process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $sname = mysql_real_escape_string(htmlspecialchars($_POST['sname']));
 $age = mysql_real_escape_string(htmlspecialchars($_POST['age']));



 // check to make sure both fields are entered
 if ($name == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($sname, $age, $error);
 }
 else
 {
 // save the data to the database
  mysql_query("INSERT stock SET sname='$sname', age='$age'")
 or die(mysql_error()); 

 // once saved, redirect back to the view page

 }
 }
 else
 // if the form hasn't been submitted, display the form
 {
 renderForm('','','','','','','','');
 }

         ?>

最佳答案

如果您想从单个表单添加多个值,请在输入框中添加逗号分隔值,然后使用explodeegp>

 $sname = mysql_real_escape_string(htmlspecialchars($_POST['sname']));
$namearray = explode(',',$sname);
foreach ($namearray as $sname){
 mysql_query("INSERT stock SET sname='$sname', age='$age'")
 or die(mysql_error()); 
}

关于javascript - 如何使用php和jquery在mysql中提交多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20837694/

相关文章:

javascript - 如何组合两个过滤器

jquery - 将滚动条添加到具有子菜单项的菜单项,而不隐藏子菜单项

javascript - 未捕获的语法错误 : Setter must have exactly one formal parameter

javascript - 如何在不指定高度的情况下仅向一个div添加垂直滚动条?

php - 如何检查第一列的一行是否与第二列相似?

PHP If 语句混淆

javascript - 将数据推送到没有索引的数组

javascript - 用另一个 div 切换一个 div

javascript - 客户端未收到页面发送的电子邮件

jquery - 调整字段大小并在单击外部重新调整大小