简体   繁体   中英

Value gets inserted more than one time to MySql table from php code

Here is my code

function add_to_do_list()
 {
//stop_duplicate_entry_in_db();
$db_connection = open_database_connection();
mysql_select_db('todolist');
$new_task = $_POST['entertask'];
$sql = "INSERT INTO todolist (name) VALUES ('$new_task')";
mysql_query($sql, $db_connection);
close_database_connection($db_connection);
}


function add_new_reject_duplicate()
{
$new_task = $_POST['entertask'];
$conn = open_database_connection();


$sql = 'SELECT * FROM todolist';
mysql_select_db('todolist');

  $retval = mysql_query( $sql, $conn );
  if(! $retval )
  {
   die('Could not get data: ' . mysql_error());
  }

  while($row = mysql_fetch_assoc($retval))
  //while($row = mysql_fetch_array($retval))
  {
$existing_task =  $row['name'];

//echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td></tr>";
if ($existing_task == $new_task) 
{
    global $error_msg;      
    $error_msg = "The task already exists.";
    //$_SESSION['task_add_error'] = $error_msg;
}
else 
{
    //echo $existing_task."<br><br>";
    add_to_do_list();


}
}

close_database_connection($conn);
}

When I run this code two strange things happen. 1. It checks with every value of the table and if one value matches without stopping it addes new task more than once 2 Even if the task is new it adds multiple times I'm confused. Please help.

Your function call is inside the foreach loop, that's why the data gets inserted several times.

There are many other issues in your code.

  1. It is bad practice to select all the lines and do your test in PHP. You should test in sql.
  2. Do not use select * , but enumerate the columns.
  3. Don't forget to escape the data before using it in the query. This is needed to prevent sql injection.
  4. Use mysqli instead of mysql extension. The mysql extension is deprecated in PHP 5.5.
  5. Variable name : mysql_query returns a result resource not a return value .

Taking in account all points but 4 the code will look like (Omitting the error tests for brevity) :

function add_new_reject_duplicate()
{
$conn = open_database_connection();
$new_task = mysql_real_escape_string ($_POST['entertask']);
$sql = "SELECT id FROM todolist
    WHERE name = '$new_task'";
mysql_select_db('todolist');
$result = mysql_query( $sql, $conn );
// error check here
if(!mysql_fetch_row($result)){
      add_to_do_list();
}
}

sorry i think this is what you meant:

$addTask=true;
    //search for the task, only if it doesn't exist in the db do we add it
while($row = mysql_fetch_assoc($retval))
{
    $existing_task =  $row['name'];

    if ($existing_task == $new_task) 
    {
        global $error_msg;      
        $error_msg = "The task already exists.";
        $addTask=false;
        break;
    }
}

if($addTask)
{
    add_to_do_list();
}

try this code with some modification in while loop

function add_new_reject_duplicate()
{
$new_task = $_POST['entertask'];
   $conn = open_database_connection();


   $sql = 'SELECT * FROM todolist';
   mysql_select_db('todolist');

    $retval = mysql_query( $sql, $conn );
     if(! $retval )
   {
   die('Could not get data: ' . mysql_error());
    }
  $ifexist=0;
  while($row = mysql_fetch_assoc($retval))
   {
   $existing_task =  $row['name'];

    if ($existing_task == $new_task) 
     {
     global $error_msg;      
     $error_msg = "The task already exists.";
  //$_SESSION['task_add_error'] = $error_msg;
      $ifexist=1;
     break;
       }

  }
    close_database_connection($conn);
     if($ifexist==0)
     {
     add_to_do_list();
   }
   }

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