我可以上传一个CSV文件,它可以通过相同的id.with PHP上传所有MySQL存在的数据
This is my code i have a upload.php and this is working fine , but i have to fill missing data of my table by uploading CSV file but its not working whenever i upload CSV its automatically generate new id and save it but my question is for example if i have a form id number is 12 now i have to insert in a phone no , this field have all information except phone no , then how can i do
id name email phone 12 Xys text@fb.com
now by CSV i have to upload only phone no
<?php
require_once('../web.config.php'); require_once(ROOT_PATH.'global.php');
require_once('function.php');
?>
<!DOCTYPE html>
<title>Home</title>
</head><body>
<div id="control_box" class="indexWrap">
<div id="container">
<div id="form">
<?php
define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'upcsv');
@$conn = mysql_connect (DB_SERVER, DB_USER, DB_PASSWORD);
mysql_select_db (DB_NAME,$conn);
mysql_set_charset('UTF8', $conn) or die(mysql_error());
if(!$conn){
die( "Sorry! There seems to be a problem connecting to our database.");
}
function get_file_extension($file_name) {
return end(explode('.',$file_name));
}
function errors($error){
if (!empty($error))
{
$i = 0;
while ($i < count($error)){
$showError.= '<div class="msg-error">'.$error[$i].'</div>';
$i ++;}
return $showError;
}// close if empty errors
} // close function
if (isset($_POST['upfile'])){
// check feilds are not empty
if(get_file_extension($_FILES["uploaded"]["name"])!= 'csv')
{
$error[] = 'Only CSV files accepted!';
}
if (!$error){
$tot = 0;
$handle = fopen($_FILES["uploaded"]["tmp_name"], "r");
fgets($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
for ($c=0; $c < 1; $c++) {
//only run if the first column if not equal to id
if($data[0] !='id'){
mysql_query("INSERT INTO tabledb(
`id`,
`name`,
`email`,
`phone`
)VALUES(
'" . mysql_real_escape_string($data[0 ])."',
'" . mysql_real_escape_string($data[1 ])."',
'" . mysql_real_escape_string($data[2 ])."',
'" . mysql_real_escape_string($data[3 ])."'
)")or die(mysql_error());
}
$tot++;}
}
fclose($handle);
$content.= "<div class='success' id='message'> CSV File Imported, $tot records added </div>";
}// end no error
}//close if isset upfile
$er = errors($error);
$content.= <<<EOF
<h3>Import CSV Data</h3>
$er
<form enctype="multipart/form-data" action="" method="post">
File:<input name="uploaded" type="file" maxlength="20" /><input type="submit" name="upfile" value="Upload File">
</form>
EOF;
echo $content;
?>
</div>
</div>
</div>
<!-- END TEMPORARY CONTENT -->
</body>
</html>
There are somethings which are unclear but I'll try to answer on the basis of information you provided.
You mention that if form id is 20 then phone number should be added if that form id is "Prinmary key" Or even a unique index then you problem can be solved by using REPLACE query.
Since you are using INSERT query that will always INSERT new row in a table try to user REPLACE query which will first try to find same index or primary key if it got that then it will remove/update (depending upon your table storage engine) the old row. If it delete the old row then it will insert the new row.
REPLACE INTO tabledb(
`id`,
`name`,
`email`,
`phone`
)VALUES(
'" . mysql_real_escape_string($data[0 ])."',
'" . mysql_real_escape_string($data[1 ])."',
'" . mysql_real_escape_string($data[2 ])."',
'" . mysql_real_escape_string($data[3 ])."'
)
You can use the following query..
Feel free yo ask if you have any question.