javascript - 将数据插入mysql数据库

标签 javascript php mysql sql

当我单击提交按钮时,我有表单,它会逐行显示所有数据 一切都工作正常,但我的问题是如何将所有值添加到 mysql 数据库 单击“提交到数据库”按钮。请任何人帮助我 这是我的完整代码

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="custom.css" type="text/css">

<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Add Sales</title>

	<script type="text/javascript">
	function add_values(){
     if(document.getElementById('edit_guid').value==""){
     if( document.getElementById('stk').value!="" ){
     
     if(document.getElementById('stk').value!=0){
                   
    sell=document.getElementById('pric').value;
    disc=document.getElementById('stk').value;
    item=document.getElementById('guid').value;
    roll=parseInt(document.getElementById('roll_no').value);
	$('<tr id='+item+'><td><lable id='+item+'roll >'+roll+'</label></td><td><input type=text  readonly="readonly" value='+sell+'></td><td><input type=text  readonly="readonly" value='+disc+' ></td></tr>').fadeIn("slow").appendTo('#item_copy_final');
    document.getElementById('stk').value="";
    document.getElementById('pric').value="";
    document.getElementById('roll_no').value=roll+1;
    document.getElementById('guid').value="";	
}
}else{
     alert('Please Select An Item');
    }}}   
        </script>
        <body>
				<form name="form1" method="post" id="form1" action="">                  
                  <input type="hidden" id="roll_no" value="1" >   	
                 <div align="center">
                  <input type="hidden" id="guid">
                  <input type="hidden" id="edit_guid">
                        
                  <table class="form" >  
					   <tr>
                          <td>price</td>						  
                          <td>stk</td>
                        </tr>
			<tr>
			<td><input type='text' class='form-control' id="pric" name="pric"></td>
			<td><input type='text' class='form-control' id="stk" name="stk"></td>
			<td><input type="button" onclick="add_values()"  id="add_new_code" value="submit" class="round"></div></form></td></tr>
             </table>
				<div style="overflow:auto ;max-height:300px;  ">
                  <table class="form" id="item_copy_final" style="margin-left:45px "></table>
                  </div>
                  </div>
         <div class="mytable_row ">
		 <form>
		 <div align="center">
		 <table>
		 <td><input type="button" value="submit_to_database" class="round"></td>
		 </table>
		 </div>
		 </form>
  
</body>
</html>

最佳答案

更新您的代码 -

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="custom.css" type="text/css">

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Add Sales</title>

  <script type="text/javascript">
    function add_values(){
      if(document.getElementById('edit_guid').value==""){
        if( document.getElementById('stk').value!="" ){

          if(document.getElementById('stk').value!=0){

            sell=document.getElementById('pric').value;
            disc=document.getElementById('stk').value;
            item=document.getElementById('guid').value;
            roll=parseInt(document.getElementById('roll_no').value);
            $('<tr id='+item+'><td><lable id='+item+'roll >'+roll+'</label></td><td><input type=text name="price[]" readonly="readonly" value='+sell+'></td><td><input type=text name="stk[]" readonly="readonly" value='+disc+' ></td></tr>').fadeIn("slow").appendTo('#item_copy_final');
            document.getElementById('stk').value="";
            document.getElementById('pric').value="";
            document.getElementById('roll_no').value=roll+1;
            document.getElementById('guid').value=""; 
            $("#pric").focus();
          }
        }
        else{
          alert('Please Select An Item');
        }
      }
    }   
  </script>
</head>
<body>
  <form name="form1" method="post" id="form1" action="">                  
    <input type="hidden" id="roll_no" value="1" >     
    <div align="center">
      <input type="hidden" id="guid">
      <input type="hidden" id="edit_guid">

      <table class="form" >  
        <tr><td>price</td><td>stk</td><td>&nbsp;</td></tr>
        <tr>
          <td><input type='text' class='form-control' id="pric" name="pric"></td>
          <td><input type='text' class='form-control' id="stk" name="stk"></td>
          <td><input type="button" onclick="add_values()"  id="add_new_code" value="submit" class="round"></td>
        </tr>
      </table>
    </div>
  </form>
  <div style="overflow:auto ;max-height:300px;" align="center">
    <form method="post" action="test.php">
      <table class="form" id="item_copy_final" style="margin-left:45px "></table>
      <table>
        <tr><td><input type="submit" name="submit_to_database" value="submit_to_database" class="round"></td></tr>
      </table>
    </form>
  </div>

</body>
</html>

编写一个 PHP 脚本页面,在其中发布数据并将值添加到 mysql 数据库中。

“test.php”

<?php
mysql_connect("hostname", "database_user", "database_password") or die("Could not connect: " . mysql_error());
mysql_select_db("database_name");

if(isset($_POST["submit_to_database"])){
  for($i=0; $i<count($_POST["price"]); $i++){
    mysql_query("INSERT INTO `your_table` SET `your_price_field` = '".$_POST["price"][$i]."', `your_stk_field` = '".$_POST["stk"][$i]."'");
  }
}
?>

关于javascript - 将数据插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44092967/

相关文章:

javascript - jquery 中合适的替换

javascript - 我可以在 Web 应用程序的客户端使用 .env 文件吗?

php - 有没有办法在不登录的情况下使用 PHP 拉取 Instagram 提要?

php - 将数据从 MySQL 数据库加载到 HTML 文本框

mysql - 使用 codeigniter 插入和选择阿拉伯语

mysql - 有没有办法遍历 MySQL 触发器中的所有字段而不是重复自己?

javascript - .fetch() 集合backbone.js 不起作用

javascript - 丢弃空格或空字符

PHP mb_ereg_replace 不替换而 preg_replace 按预期工作

php - 防止从 MySQL 和 PHP 发送重复记录