Saber Posted February 21, 2015 Share Posted February 21, 2015 Hi, Please check the code below, I am able to insert the records in the database through CSV file but unable to update. No error is generating but not updating <?php //Upload File if (isset($_POST['submit'])) { 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, 2000, ",")) !== FALSE){ $Sql=mysql_query("select * from test where id ='$id'"); if(mysql_num_rows($Sql) > 0) { $import="update test set `name`='$data[1]', `gender`='$data[2]', `designation`='$data[3]' where id='$data[0]'"; mysql_query($import) or die(mysql_error()); } else { $import="insert into test set `name`='$data[1]', `gender`='$data[2]', `designation`='$data[3]', `id`='$data[0]'"; mysql_query($import) or die(mysql_error()); } } fclose($handle); print "Update done"; Link to comment https://forums.phpfreaks.com/topic/294766-insert-working-but-update-not-working/ Share on other sites More sharing options...
marius_haugan Posted February 21, 2015 Share Posted February 21, 2015 See how much easier your code is to read now? if (isset($_POST['submit'])) { 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, 2000, ",")) !== FALSE){ $Sql=mysql_query("select * from test where id ='$id'"); if(mysql_num_rows($Sql) > 0) { $import="update test set `name`='$data[1]', `gender`='$data[2]', `designation`='$data[3]' where id='$data[0]'"; mysql_query($import) or die(mysql_error()); } else { $import="insert into test set `name`='$data[1]', `gender`='$data[2]', `designation`='$data[3]', `id`='$data[0]'"; mysql_query($import) or die(mysql_error()); } } fclose($handle); print "Update done"; Link to comment https://forums.phpfreaks.com/topic/294766-insert-working-but-update-not-working/#findComment-1506313 Share on other sites More sharing options...
Saber Posted February 21, 2015 Author Share Posted February 21, 2015 @ marius May I know the error???????????? Link to comment https://forums.phpfreaks.com/topic/294766-insert-working-but-update-not-working/#findComment-1506314 Share on other sites More sharing options...
Saber Posted February 21, 2015 Author Share Posted February 21, 2015 nobody here to help me out/???????? Link to comment https://forums.phpfreaks.com/topic/294766-insert-working-but-update-not-working/#findComment-1506320 Share on other sites More sharing options...
mac_gyver Posted February 21, 2015 Share Posted February 21, 2015 $Sql=mysql_query("select * from test where id ='$id'"); ^^^ in the above line in your code, there is no $id variable in your code, so that query is never finding anything, so the update branch logic never runs. if you had php's error_reporting set to E_ALL and display_errors set to ON, php would be outputting an undefined variable error to alert you to the problem with the $id variable. you don't need to select the data in order to find out if you should insert or update something, mysql has an INSERT ... ON DUPLICATE KEY UPDATE query - http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html you can also use a LOAD DATA LOCAL INFILE query to import the data from a file. Link to comment https://forums.phpfreaks.com/topic/294766-insert-working-but-update-not-working/#findComment-1506339 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.