简体   繁体   English

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

[英]how to submit multiple data in mysql using php and jquery

how to submit multiple values in a table please help me im using this code this code submit only 1 entry in database but i want to enter multiples value in database how can i do this 如何在一个表中提交多个值,请使用此代码帮助我,此代码仅在数据库中提交1个条目,但我想在数据库中输入多个值,我该怎么做

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

this is entry.html 这是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>

this is php page submit.php 这是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('','','','','','','','');
 }

         ?>

if u want to add multiple value from single form add comma seprated value in input box and then use explode eg: 如果您想从单一形式添加多个值,请在输入框中添加逗号分隔的值,然后使用explode,例如:

 $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()); 
}

Why don't you try the .post() jQuery function ? 为什么不尝试使用.post()jQuery函数? it's very simple to implement 实施起来很简单

$.post( "example.php", { name: "John", time: "2pm", etc, etc, etc });
   .done(function() {
          alert( "second success" );
    })
   .fail(function() {
          alert( "error" );
   })
   .always(function() {
         alert( "finished" );
   });

Then in your php, you just have to insert into mysql with a loop like a foreach : 然后在您的php中,您只需要使用foreach之类的循环插入mysql中即可:

<?php 
       foreach ( $_POST as $p ){
           mysql_query( YOUR QUERY );
       }
       return ( 'DONE !' ); // your return values
?>

You can use foreach or for loop to execute multiple insert sql query. 您可以使用foreachfor loop执行多个insert sql查询。

You should submit your form values to get those values in php code.This question is exactly related to your question. 您应该submit form values以在php代码中获取这些值。此问题与您的问题完全相关。 How to insert into MYSQL row from multiple $_POST arrays 如何从多个$ _POST数组插入MYSQL行

use your code like this fiddle example http://jsfiddle.net/cjramki/mPR45/ 像这样的小提琴示例使用您的代码http://jsfiddle.net/cjramki/mPR45/

I hope this link will useful to you. 希望此链接对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用PHP提交jQuery添加行数据到MySQL - How to submit Jquery Add Rows data to mysql using PHP 如何使用php和javascript在mysql中提交多行 - how to submit multiple row in mysql using php and javascript 如何检查成功的数据在PHP文件中使用MySQL提交 - How to check successful data Submit using MySQL in a PHP file 如何使用jquery php mysql删除带有复选框的多行 - How to delete multiple rows with checkbox using jquery php mysql 如何使用jQuery AJAX与PHP一起使用<a>标签</a>将表单提交到mySQL数据库<a>?</a> - How to use jQuery AJAX with PHP to submit a form into a mySQL database with using an <a> tag? 如何使用带有多个提交按钮的JQuery提交表单? - How to Submit Form using JQuery with Multiple Submit Buttons? 如何使用PHP,Angular.js和MySQL由单个用户提交的多行插入值 - How to insert value in multiple row by a single user submit using PHP,Angular.js and MySQL 如何使用onclick甚至CHECKBOX更新数据而无需在PHP和MySQL中提交按钮 - How to update data using onclick even CHECKBOX without button submit in php and mysql 如何使用javascript和node.js-mysql而不使用php将HTML表单数据提交到MySql数据库表中? - How can I submit HTML form data into MySql database table using javascript along with node.js - mysql and without php? 如何使用jQuery和MySQL在同一页面上提交和显示? - How to Submit and Display on the same page using jQuery and MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM