Jump to content

Duplicate Entries in Database


Legacial

Recommended Posts

Hi guys,

 

I'm new to this forum, and I know most of you here are php gurus, so I salute you.

 

I'm designing a website where student's can put their CVs online and employers get to download these CVs in pdf format using fpdf. What I want the code to do is when the employer clicks on a 'download cv link', they download the CV and their id (taken as a session) together with the student id is stored in a table to keep count on the number of CVs an individual employer downloads.

 

The code works fine, the CV is downloadable. The problem comes where the employer id and student id need to be saved in the database, although both IDs are stores, the insert occurs twice, meaning that 2 duplicate rows are output instead of only one. I've tried searching for the problem, but I can seem to find it.

 

Here's the full page:

//Get connection
<?php require_once('../Connections/conn.php'); ?>
//Get student id and employer id
<?php
$id = $_GET['Stud_ID'];
$eid = $_GET['Emp_ID'];
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_conn, $conn);
$query_student = "SELECT * FROM students WHERE students.stud_ID=".$id."";
$student = mysql_query($query_student, $conn) or die(mysql_error());
$row_student = mysql_fetch_assoc($student);
$totalRows_student = mysql_num_rows($student);

mysql_select_db($database_conn, $conn);
$query_Acad_Q_Strath = "SELECT * FROM stud_acad_course, acad_courses, acad_institutions WHERE stud_acad_course.stud_ID=".$id."  AND stud_acad_course.acad_inst_ID = acad_institutions.acad_inst_ID AND stud_acad_course.acad_course_ID=acad_courses.acad_course_ID ";
$Acad_Q_Strath = mysql_query($query_Acad_Q_Strath, $conn) or die(mysql_error());
$row_Acad_Q_Strath = mysql_fetch_assoc($Acad_Q_Strath);
$totalRows_Acad_Q_Strath = mysql_num_rows($Acad_Q_Strath);

mysql_select_db($database_conn, $conn);
$query_Prof_Q = "SELECT * FROM stud_prof_course, prof_courses, acad_institutions WHERE stud_prof_course.stud_ID= ".$id."  AND stud_prof_course.prof_course_ID=prof_courses.prof_course_ID AND stud_prof_course.acad_inst_ID = acad_institutions.acad_inst_ID";
$Prof_Q = mysql_query($query_Prof_Q, $conn) or die(mysql_error());
$row_Prof_Q = mysql_fetch_assoc($Prof_Q);
$totalRows_Prof_Q = mysql_num_rows($Prof_Q);

mysql_select_db($database_conn, $conn);
$query_Work_Xp = "SELECT * FROM work_experience, titles_lookup WHERE work_experience.stud_ID=".$id." AND work_experience.work_type = 'previous'  AND work_experience.title_ID=titles_lookup.title_ID  ";
$Work_Xp = mysql_query($query_Work_Xp, $conn) or die(mysql_error());
$row_Work_Xp = mysql_fetch_assoc($Work_Xp);
$totalRows_Work_Xp = mysql_num_rows($Work_Xp);

mysql_select_db($database_conn, $conn);
$query_Projectss = "SELECT * FROM stud_projects WHERE stud_projects.stud_ID=".$id."";
$Projectss = mysql_query($query_Projectss, $conn) or die(mysql_error());
$row_Projectss = mysql_fetch_assoc($Projectss);
$totalRows_Projectss = mysql_num_rows($Projectss);

mysql_select_db($database_conn, $conn);
$query_Publics = "SELECT * FROM publications WHERE stud_ID =".$id."";
$Publics = mysql_query($query_Publics, $conn) or die(mysql_error());
$row_Publics = mysql_fetch_assoc($Publics);
$totalRows_Publics = mysql_num_rows($Publics);

mysql_select_db($database_conn, $conn);
$query_Abiltis = "SELECT * FROM stud_abilities WHERE stud_abilities.stud_ID=".$id."";
$Abiltis = mysql_query($query_Abiltis, $conn) or die(mysql_error());
$row_Abiltis = mysql_fetch_assoc($Abiltis);
$totalRows_Abiltis = mysql_num_rows($Abiltis);

mysql_select_db($database_conn, $conn);
$query_Mmbrshps = "SELECT * FROM stud_prof_association, prof_association_lookup WHERE stud_ID = ".$id."  AND prof_association_lookup.prof_inst_ID=stud_prof_association.prof_inst_ID";
$Mmbrshps = mysql_query($query_Mmbrshps, $conn) or die(mysql_error());
$row_Mmbrshps = mysql_fetch_assoc($Mmbrshps);
$totalRows_Mmbrshps = mysql_num_rows($Mmbrshps);

mysql_select_db($database_conn, $conn);
$query_lang = "SELECT * FROM languages, stud_lang WHERE stud_lang.stud_ID= ".$id." AND  stud_lang.lang_ID=languages.lang_ID";
$lang = mysql_query($query_lang, $conn) or die(mysql_error());
$row_lang = mysql_fetch_assoc($lang);
$totalRows_lang = mysql_num_rows($lang);

mysql_select_db($database_conn, $conn);
$query_sklls = "SELECT * FROM stud_it_skills, it_skills WHERE stud_it_skills.stud_ID=".$id." AND stud_it_skills.skill_ID=it_skills.skill_ID";
$sklls = mysql_query($query_sklls, $conn) or die(mysql_error());
$row_sklls = mysql_fetch_assoc($sklls);
$totalRows_sklls = mysql_num_rows($sklls);

mysql_select_db($database_conn, $conn);
$query_tmeuncted = "SELECT * FROM time_unaccounted WHERE stud_ID=".$id."";
$tmeuncted = mysql_query($query_tmeuncted, $conn) or die(mysql_error());
$row_tmeuncted = mysql_fetch_assoc($tmeuncted);
$totalRows_tmeuncted = mysql_num_rows($tmeuncted);

mysql_select_db($database_conn, $conn);
$query_refs = "SELECT * FROM referees, titles_lookup WHERE referees.stud_ID=".$id." AND referees.title_ID=titles_lookup.title_ID ";
$refs = mysql_query($query_refs, $conn) or die(mysql_error());
$row_refs = mysql_fetch_assoc($refs);
$totalRows_refs = mysql_num_rows($refs);


require('../fpdf/fpdf.php');

class PDF extends FPDF
{

//Page header
function Header()
{
$this->SetFont('Times','I',13);			//Times italian 13
$this->Cell(80);				// Move to the right
$this->Cell(50,10,'Curriculum Vitae ',0,0,'C');	// Title
$this->Ln(20);					// Line break
}


//Page footer
function Footer()
{
$this->SetY(-15);			// Position at 1.5 cm from bottom
$this->SetFont('Arial','I',;		// Arial italic 8
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');	// Page number
}

function ChapterTitle($label)
{
    // Arial 12
    $this->SetFont('Arial','',13);
    // Background color
    $this->SetFillColor(200,220,255);
    // Title
    $this->Cell(0,6,"$label",0,1,'L',true);
    // Line break
    $this->Ln(4);
}

function PrintChapter($title)
{
    $this->ChapterTitle($title);
}

function MultiCellBltArray($w, $h, $blt_array, $border=0, $align='J', $fill=0)
    {
        if (!is_array($blt_array))
        {
            die('MultiCellBltArray requires an array with the following keys: bullet, margin, text, indent, spacer');
            exit;
        }
                
        //Save x
        $bak_x = $this->x;
        
        for ($i=0; $i<sizeof($blt_array['text']); $i++)
        {
            //Get bullet width including margin
            $blt_width = $this->GetStringWidth($blt_array['bullet'] . $blt_array['margin'])+$this->cMargin*2;
            
            // SetX
            $this->SetX($bak_x);
            
            //Output indent
            if ($blt_array['indent'] > 0)
                $this->Cell($blt_array['indent']);
            
            //Output bullet
            $this->Cell($blt_width, $h, $blt_array['bullet'] . $blt_array['margin'], 0, '', $fill);
            
            //Output text
            $this->MultiCell($w-$blt_width, $h, $blt_array['text'][$i], $border, $align, $fill);
            
            //Insert a spacer between items if not the last item
            if ($i != sizeof($blt_array['text'])-1)
                $this->Ln($blt_array['spacer']);
            
            //Increment bullet if it's a number
            if (is_numeric($blt_array['bullet']))
                $blt_array['bullet']++;
        }
    
        //Restore x
        $this->x = $bak_x;
    }
}

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$offset = 30;
$offset1 = 5;
$pdf->SetFont('Arial','B',16);
$pdf->Cell(200,10, "".$row_student['stud_lname']. " " .$row_student['stud_fname'] ,0,1,'C');
$pdf->Ln();
$pdf->PrintChapter('Personal Information');
$pdf->SetFont('Helvetica','',12);
$pdf->Cell(40,10,"Date of Birth:  ".$row_student['stud_DOB'],0,1);
$pdf->Cell(40,10,"Gender:          ".$row_student['stud_gender'],0,1);
$pdf->Cell(40,10,"Marital Status: ".$row_student['stud_marital_status'],0,1);
$pdf->Cell(40,10,"Nationality:     ".$row_student['stu_nationality'],0,1);
$pdf->Cell(40,10,"ID Number:      ".$row_student['stud_ID_no'],0,1);
$pdf->Cell(40,10,"Mobile:          ".$row_student['stud_mobile'],0,1);
$pdf->SetTextColor(0,0,255);
$pdf->SetFont('','U');
$pdf->Write(10,"E-mail:          ".$row_student['stud_email'],'mailto:'.$row_student['stud_email']);
//$pdf->Cell(40,10,"E-mail:          ".$row_student['stud_email'],'mailto:'.$row_student['stud_email']);
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('','');
$pdf->Ln();
$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Academic Qualifications");
$pdf->SetFont('Helvetica','',12);
do{
$pdf->SetFont('Helvetica','B',12);
$pdf->Cell(40,10, "" .date("Y", strtotime($row_Acad_Q_Strath['course_from'])). " - " .date("Y", strtotime($row_Acad_Q_Strath['course_to'])). "      " .$row_Acad_Q_Strath['acad_course_name'] ,0,1);
//$pdf->Cell(40,10, "",0,1);
//$pdf->Cell($offset);
//$pdf->Cell(40,10,"".$row_Acad_Q_Strath['acad_course_name'],0,1);
$pdf->Cell($offset);
$pdf->SetFont('Helvetica','',12);
$pdf->Cell(40,10, "".$row_Acad_Q_Strath['acad_inst_name'],0,1);
$pdf->Ln();	
}while ($row_Acad_Q_Strath = mysql_fetch_assoc($Acad_Q_Strath));

$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Professional Qualifications");
$pdf->SetFont('Helvetica','',12);
do{
$pdf->SetFont('Times','B',12);
$pdf->Cell(40,10,"".date("Y", strtotime($row_Prof_Q['course_from'])). " - " .date("Y", strtotime($row_Prof_Q['course_to'])). "         " .$row_Prof_Q['acad_inst_name'],0,1);
$pdf->Cell($offset);
$pdf->SetFont('Helvetica','',12);
$pdf->Cell(40,10,"".$row_Prof_Q['prof_course_name'],0,1);
$pdf->Ln();
}while ($row_Prof_Q = mysql_fetch_assoc($Prof_Q));
$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Work Experience");
do{
$pdf->SetFont('Helvetica','B',12);
$pdf->Cell(40,10,"".date("F Y",strtotime($row_Work_Xp['work_date_from'])). " - " .date("F Y",strtotime($row_Work_Xp['work_date_to'])). "  ".$row_Work_Xp['work_org'],0,1);
$pdf->SetFont('Helvetica','I',12);
$pdf->Cell(40,10,"".$row_Work_Xp['work_position'],0,1);
$pdf->Cell($offset);
$pdf->SetFont('Helvetica','',12);
$pdf->Cell(40,10,"".$row_Work_Xp['work_description'],0,1);
$pdf->Ln();
}while ($row_Work_Xp = mysql_fetch_assoc($Work_Xp));

$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Projects");
$pdf->SetFont('Helvetica','',12);
do{
$pdf->Cell(40,10,$row_Projectss['stud_Project'],0,1);
}while ($row_Projectss = mysql_fetch_assoc($Projectss));
$pdf->Ln();
$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Publications");
$pdf->SetFont('Helvetica','',12);
do{
$pdf->Cell(40,10,"" .$row_Publics['publ_date']. " - ".$row_Publics['publ_name'],0,1);
}while ($row_Publics = mysql_fetch_assoc($Publics));
$pdf->Ln();
$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Memberships");
$pdf->SetFont('Helvetica','',12);
do{
$pdf->Cell(40,10,"".$row_Mmbrshps['year_joined']. " - " .$row_Mmbrshps['prof_inst_name'],0,1);
}while ($row_Mmbrshps = mysql_fetch_assoc($Mmbrshps));
$pdf->Ln();
$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Skills");
$pdf->SetFont('Helvetica','',12);
/*$column_width = $pdf->w-30;
$test1 = array();
$test1['bullet'] = chr(149);
$test1['margin'] = ' ';
$test1['indent'] = 0;
$test1['spacer'] = 0;
$test1['text'] = array();
$txt = $row_sklls['skill_name'];
$count--;
for ($i=0; $i<=$count; $i++)
{
    $test1['text'][$i] = $row_sklls['skill_name'];
}
$pdf->SetX(10);
$pdf->MultiCellBltArray($column_width-$pdf->x, 6, $test1);
$pdf->Ln(10);*/
do{
$pdf->Cell(40,10,$row_sklls['skill_name'],0,1);
}while ($row_sklls = mysql_fetch_assoc($sklls));
$pdf->Ln();
$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Abilities");
$pdf->SetFont('Helvetica','',12);
do{
$pdf->Cell(40,10,"" .$row_Abiltis['stud_Ability'],0,1);
}while ($row_Abiltis = mysql_fetch_assoc($Abiltis));
$pdf->Ln();
$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Language");
$pdf->SetFont('Helvetica','',12);
do{
$pdf->Cell(40,10,$row_lang['lang_name'],0,1);
}while ($row_lang = mysql_fetch_assoc($lang));
$pdf->Ln();
$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Time Unaccounted For");
do{
$pdf->SetFont('Helvetica','B',12);
$pdf->Cell(40,10,"From: ".date("F Y",strtotime($row_tmeuncted['date_from'])). " - " .date("F Y", strtotime($row_tmeuncted['date_to'])),0,1);
$pdf->SetFont('Helvetica','',12);
$pdf->Cell(40,10,"Reason: ".$row_tmeuncted['reason'],0,1);
}while ($row_tmeuncted = mysql_fetch_assoc($tmeuncted));
$pdf->Ln();
$pdf->SetFont('Arial','B',16);
$pdf->PrintChapter("Referees");
do{
$pdf->SetFont('Helvetica','B',12);
$pdf->Cell(40,10,"".$row_refs['ref_name'],0,1);
$pdf->SetFont('Helvetica','',12);
$pdf->Cell(40,10,"".$row_refs['ref_position'],0,1);
$pdf->Cell(40,10,"".$row_refs['ref_org'],0,1);
$pdf->Cell(40,10,"P.O. Box ".$row_refs['ref_pobx']. " " .$row_refs['ref_poscod']. " " .$row_refs['ref_city'],0,1);
$pdf->Cell(40,10,"Tel: ".$row_refs['ref_phone_no'],0,1);
$pdf->Cell(40,10,"Email: ".$row_refs['ref_email'],0,1);
$pdf->Ln();
}while ($row_refs = mysql_fetch_assoc($refs));
$pdf->Output();

//insert IDs into database
$sql1 = mysql_query("INSERT INTO cv_download (emp_ID,stud_ID) VALUES ('$eid','$id')") or die(mysql_error());
  
$sql2 = mysql_query("INSERT INTO cv_download_count (emp_ID,count_value) VALUES ('$eid',1) ON DUPLICATE KEY UPDATE count_value=count_value+1") or die(mysql_error());

?>

 

 

Any help would be greatly appreciated!! Thanks!

Link to comment
Share on other sites

Sorry about that, I'm using MySQL version 5.5.16.

 

I'm designing a website where student's can put their CVs online and employers get to download these CVs in pdf format using fpdf. What I want the code to do is when the employer clicks on a 'download cv link', they download the CV and their id (taken as a session) together with the student id is stored in a table to keep count on the number of CVs an individual employer downloads.

 

The code works fine, the CV is downloadable. The problem comes where the employer id and student id need to be saved in the database, although both IDs are stores, the insert occurs twice, meaning that 2 duplicate rows are output instead of only one. I've tried searching for the problem, but I can seem to find it.

 

Here's the full page:

//Get connection
<?php require_once('../Connections/conn.php'); ?>
//Get student id and employer id from another page
<?php
$id = $_GET['Stud_ID'];
$eid = $_GET['Emp_ID'];

The real problem comes with the sql queries: they end up inserting two rows instead of one.

//insert IDs into database
$sql1 = mysql_query("INSERT INTO cv_download (emp_ID,stud_ID) VALUES ('$eid','$id')") or die(mysql_error());
  
$sql2 = mysql_query("INSERT INTO cv_download_count (emp_ID,count_value) VALUES ('$eid',1) ON DUPLICATE KEY UPDATE count_value=count_value+1") or die(mysql_error());

?>

 

My initial guess was that fpdf was the root cause of the problem, but I'm not too sure. Could someone please tell me if I'm doing anything wrong? Thanks.

Link to comment
Share on other sites

  • 2 weeks later...

We don't have any information on how you are invoking this script.  I don't see anything that would explain a double insert, other than the script being run 2x.  Based on the information we have so far, I'd try Firefox with firebug turned on, and look at the net tab.

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.