如何在每列中使用PHP将HTML数组存储到Mysql中

时间:2018-03-14 09:17:56

标签: javascript php jquery html mysql

我想知道如何将以下HTML和Javascript中的数组存储在Mysql数据库中,并将每个字段添加到MYSQL数据库中的以下列: title,category,pdfname(从HTML发送的文件的名称) 并且HTML中的文件存储在名为" pdffile"的目录中。在服务器上 对于Javascript添加的每个记录集。

以下是JAVASCRIPT代码:



<SCRIPT language="javascript">
function addRow(tableID) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	if(rowCount < 10){                            // limit the user from creating fields more than your limits
		var row = table.insertRow(rowCount);
		var colCount = table.rows[0].cells.length;
		for(var i=0; i <colCount; i++) {
			var newcell = row.insertCell(i);
			newcell.innerHTML = table.rows[0].cells[i].innerHTML;
		}
	}else{
		 alert("Maximum Number of Books is 10");
			   
	}
}

function deleteRow(tableID) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	for(var i=0; i<rowCount; i++) {
		var row = table.rows[i];
		var chkbox = row.cells[0].childNodes[0];
		if(null != chkbox && true == chkbox.checked) {
			if(rowCount <= 1) {               // limit the user from removing all the fields
				alert("Cannot Remove all the Books.");
				break;
			}
			table.deleteRow(i);
			rowCount--;
			i--;
		}
	}
}
</SCRIPT>


Below is the HTML code:

<!-- begin snippet: js hide: false console: true babel: false -->
&#13;
</HEAD>
    <div style="border:auto solid 1px; font-size:15px; background-color:red; width:100%;">
    <form action="" enctype="multipart/form-data" id="pdf_form" method="post" name="pdf_form">

    	
      <table id="dataTable" class="form" border="1">
      <tbody>
    	<p>
    	<td >
    		<input type="checkbox" name="chk[]" checked="checked" />
    	</td>
    	<td>
    	<label for="title">Title of PDF</label>
    	<input type="text" id="title" name="title[]">

    	
    	<td>
    	<label for="pdffile">PDF supported. 2MB Maximum)</label>
    	<input type="file" id="pdffile" name="pdffile[]">
    	
    	</td>
    	
    	<td>
    	<label for="category">Category</label>
    	<select id="category" name="category[]"> 
        <option>Science</option>
        <option>Technology</option>
        <option>Biblical</option>
        <option>Business</option>
        <option>Medical</option>
        <option>Engineering</option>
        <option>World</option>
    	</select>
    	</td>
    	
    	</p>
      </tr>
     </tbody>
    </table>


    <p> 
      <input type="button" value="Add PDF" onClick="addRow('dataTable')" /> 
      <input id="button" name="submit" type="submit" value="Save PDF(s)" />
      
      <p>(All actions apply only to entries with check marked check boxes only.)</p>
    </p>
    </form>
    </div>

    //PHP for Upload

    <?php
error_reporting(E_ALL);
   include("session.php");
   session_start();
   
   if($_SERVER["REQUEST_METHOD"] == "POST") { 
      
  	$pdffile = $_FILES['pdffile']['name'];
  	$pdffile_name = $_POST['pdffile'];
  	// Get text
  	$pdffile_name = mysqli_real_escape_string($con, $_POST['pdffile_name']);

  	// image file directory
  	$target = "books/".basename($pdfstore);  
      
$item_title = $_POST['title'];
$item_category = $_POST['category'];
$item_pdfname_name = $_POST['pdfname_name'];

foreach ($_FILES as $pdfname)

  if (move_uploaded_files($pdffile['tmp_name'], 'pdfstorage/' . $file['pdffile']))

for($count = 0; $count<count($item_title); $count++){

  $title = mysqli_real_escape_string($con,$item_title[$count]);
  $category = mysqli_real_escape_string($con,$item_category[$count]);
  $pdfname = mysqli_real_escape_string($con, $item_pdfname[$count]);
  
 {
    


  $sql = "INSERT INTO pdftable (title, category, pdfname) VALUES('$title', '$category', '$pdfname')";
}
if (move_uploaded_file($_FILES['pdffile']['tmp_name'], $target));

  if ($con->query($sql) === TRUE) {
    echo "<div style='border:auto solid 1px; font-size:15px; color:green; background-color:auto; width:100%;'> PDF with Title $title Added Successfully. </div>";
  }
  else {
    echo "Error: " . $sql . "<br>" . $con->error;
  };
};
};
?>
&#13;
&#13;
&#13;

每次,字段&#34;类别[]&#34;和&#34;标题[]&#34;被添加到数据库中,但是,文件不会被移动到&#34; pdfstorage&#34;服务器上的文件夹和文件名未添加到数据库中。 我需要MYSQL代码才能成功地将文件和字段添加到数据库中。将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
<SCRIPT language="javascript">
function addRow(tableID) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	if(rowCount < 10){                            // limit the user from creating fields more than your limits
		var row = table.insertRow(rowCount);
		var colCount = table.rows[0].cells.length;
		for(var i=0; i <colCount; i++) {
			var newcell = row.insertCell(i);
			newcell.innerHTML = table.rows[0].cells[i].innerHTML;
		}
	}else{
		 alert("Maximum Number of Books is 10");
			   
	}
}

function deleteRow(tableID) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	for(var i=0; i<rowCount; i++) {
		var row = table.rows[i];
		var chkbox = row.cells[0].childNodes[0];
		if(null != chkbox && true == chkbox.checked) {
			if(rowCount <= 1) {               // limit the user from removing all the fields
				alert("Cannot Remove all the Books.");
				break;
			}
			table.deleteRow(i);
			rowCount--;
			i--;
		}
	}
}
</SCRIPT>
&#13;
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>

	<div style="border:auto solid 1px; font-size:15px; background-color:red; width:100%;">
    <form action="" enctype="multipart/form-data" id="pdf_form" method="post" name="pdf_form">

    	
      <table id="dataTable" class="form" border="1">
      <tbody>
    	<p>
    	<td >
    		<input type="checkbox" name="chk[]" checked="checked" />
    	</td>
    	<td>
    	<label for="title">Title of PDF</label>
    	<input type="text" id="title" name="title[]">

    	
    	<td>
    	<label for="pdffile">PDF supported. 2MB Maximum)</label>
    	<input type="file" id="pdffile" name="pdffile[]">
    	
    	</td>
    	
    	<td>
    	<label for="category">Category</label>
    	<select id="category" name="category[]"> 
        <option>Science</option>
        <option>Technology</option>
        <option>Biblical</option>
        <option>Business</option>
        <option>Medical</option>
        <option>Engineering</option>
        <option>World</option>
    	</select>
    	</td>
    	
    	</p>
      </tr>
     </tbody>
    </table>


    <p> 
      <input type="button" value="Add PDF" onClick="addRow('dataTable')" /> 
      <input id="button" name="submit" type="submit" value="Save PDF(s)" />
      
      <p>(All actions apply only to entries with check marked check boxes only.)</p>
    </p>
    </form>
    </div>

    <!-- //PHP for Upload -->



    <?php
    $con = mysqli_connect("localhost", "root","", "pdf") or die ("Error".mysqli_error($con));

if($_SERVER["REQUEST_METHOD"] == "POST") { 

    if(isset($_POST['submit']) && $_FILES['pdffile']['size'] > 0)
	{
        $fileName = $_FILES['pdffile']['name'];
        $tmpName  = $_FILES['pdffile']['tmp_name'];
        $fileSize = $_FILES['pdffile']['size'];
        $fileType = $_FILES['pdffile']['type'];

        $item_title = $_POST['title'];
		$item_category = $_POST['category'];

        for($count = 0; $count<count($item_title); $count++){
        	

        	  $title = mysqli_real_escape_string($con,$item_title[$count]);
			  $category = mysqli_real_escape_string($con,$item_category[$count]);
			  $pdfname = mysqli_real_escape_string($con, $fileName[$count]);  //document name
			  $tmpname = mysqli_real_escape_string($con, $tmpName[$count]);
			  $folder = "uploads/";

			  if(!get_magic_quotes_gpc())
		        {
		            $pdfname = addslashes($pdfname);
		        }

			  $doc_path = move_uploaded_file($tmpname, $folder.$pdfname);

			  $sql = "INSERT INTO zipcode (country, city, zipCode) VALUES('$title', '$category', '$pdfname')";

			  if ($con->query($sql) === TRUE) {
    			echo "<div style='border:auto solid 1px; font-size:15px; color:green; background-color:auto; width:100%;'> PDF with Title $title Added Successfully. </div>";
  			}
			  else {
			    echo "Error: " . $sql . "<br>" . $con->error;
			  };
        	}
        
    }    
}  


?>

</body>
</html>
&#13;
&#13;
&#13;