Jump to content

Insert working but update not working


Saber

Recommended Posts

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

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";
$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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.