如果表存在,则Php create table更改表的名称并创建

时间:2015-04-13 04:18:42

标签: php dynamic naming

信息:

有两个数据库:

db_pup(包含我将在下面讨论的教师表)和 db_puprecords

下面的代码允许在上传新的csv文件时将旧的教师表从db_pup复制到db_puprecords,但问题是我需要动态更改要创建的表的名称每次用户上传新的csv时都会db_puprecords 1stsem2015,例如2ndsem20151stsem2016if (isset($_POST['submit'])) { mysql_query("CREATE TABLE db_puprecords.1stSEM2015 LIKE db_pup.faculty") or die ("cannot upload csv".mysql_error()); mysql_query("INSERT INTO db_puprecords.1stSEM2015 SELECT * FROM db_pup.faculty") or die ("cannot upload csv".mysql_error()); $deleterecords = "TRUNCATE TABLE faculty"; //empty the table of its current records mysql_query($deleterecords); if (is_uploaded_file($_FILES['filename']['tmp_name'])) { echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>"; echo "<h2>Displaying contents:</h2>"; readfile($_FILES['filename']['tmp_name']); } //Import uploaded file to Database $handle = fopen($_FILES['filename']['tmp_name'], "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import="INSERT into faculty(FCode,password,FName,MName,LName,Gender,image_name,BDate,Title,EmpStat,CollegeID,DepartmentID) values('$data[0]','$data[1]','$data[2]','$data[3]' ,'$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]')"; mysql_query($import) or die(mysql_error()); } fclose($handle); print "Import done"; //view upload form }else { print "Upload the csv<br />\n"; print "<form enctype='multipart/form-data' action='uploadCSV.php' method='post'>"; print "CSV to import:<br />\n"; print "<input size='100' type='file' name='filename' required ><br />\n"; print "<input type='submit' name='submit' value='Upload'></form>"; } ?>

{{1}}

0 个答案:

没有答案