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
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";
Link to comment
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.