简体   繁体   中英

Updating Database using Csv File

When i upload the Csv file it always says Incorrect Table ".

I checked the Table name and it was correct but i still dont know why it isn't updating the database. It is possible to update the database using Csv files? or only inserting is possible?

<?php
include 'connect.php';
$modname = $user_data['name'];
$usy = $_POST['usy'];
$usem = $_POST['usem'];
$term2 = $_POST['term2'];
if ( isset( $_FILES['userfile'] ) )
{
  $csv_file = $_FILES['userfile']['tmp_name'];

  if ( ! is_file( $csv_file ) )
    exit('File not found.');



  if (($handle = fopen( $csv_file, "r")) !== FALSE)
  {
      fgetcsv($handle); // get line 0 and move pointer to line 1
      fgetcsv($handle); // get line 1 and move pointer to line 2
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
      {  
        {
        $sql = "UPDATE `$term2` SET grade = $data[1] WHERE name_id = $data[0], section = $data[4], subject = $data[5], uploaded = $modname, school_year = $usy , semester = $usem"; 
        $exec = mysql_query($sql) or die(mysql_error());
        $sql2 = "DELETE FROM `$term2` WHERE `name_id` = '' AND `grade` = '';";  
        $exec = mysql_query($sql2) or die(mysql_error());   

        echo ("The following data has been added to the database");
        }
      }
    }
  }
?>
<form name="form2" enctype="multipart/form-data" method="POST" onSubmit="return validateForm()">
    <H1>Update CSV File</H1>
    School Year:<input type="text" name="usy"> Semester:    <select type="text" name="usem">
    <option value="1st">1st</option>
    <option value="2nd">2nd</option>
    </select>
    <br />
    Term:       <select type="text" name="term2">
    <option value="prelims">Prelims</option>
    <option value="midterm">Midterm</option>
    <option value="prefinals">Prefinals</option>
    <option value="finals">Finals</option>
    </select>
    <br />
    <br />
    <input name="userfile" type="file">
    <br />
    <input type="submit" value="Upload">



        </form>

It seems that you are making error in your sql statement

$sql = "UPDATE `$term2` SET grade = $data[1] WHERE name_id = $data[0], section = $data[4], subject = $data[5], uploaded = $modname, school_year = $usy , semester = $usem"; 

The WHERE clause must have either AND/OR in place of comma(,) for adding conditions. Comma should be used to separate columns/fields that you want to set through the update.

您的查询中有一个缺陷:“ $ sql =” UPDATE $term2 SET grade = $ data [1] WHERE ...“从$term2删除$term2 ,它应显示为:” $ sql =“ UPDATE term2 SET grade = $ data [1]在哪里...”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM