Jump to content


Photo

Problem with Data insert into DB!


  • Please log in to reply
1 reply to this topic

#1 Buglish

Buglish
  • New Members
  • Pip
  • Newbie
  • 7 posts
  • LocationSouth Africa

Posted 23 August 2006 - 08:24 AM

Hi I am a newbie at PHP and MySQL.
I have this function that is suppose to insert the values into the temp database.
I am sure the values are being passed and splited from file(CSV) into the correct variables.
But when it must write to database the Data is not writen.
Is there something wrong with my syntax?
I do get the Last echo, that means all fields has been processed...but still no data in DB.
//------------------------------------------------------------------
<?php
function breakname($fullnamein)
    {
    $fullnamebreak = explode(" ",$fullnamein);
    //inticate that these variables are the gobal ones not local ones
    global $title,$initials,$surname;
    for($p=0;$p<3;$p++)
    {
switch($p)
{
case 0 :$title = $fullnamebreak[$p];break;
case 1 :$initials = $fullnamebreak[$p];break;
case 2 :$surname = $fullnamebreak[$p];break;
}
}  
    }
//$link = "/EclipseWorkspace/LPCL101/files/*.php";
//Test File  /EclipseWorkspace/LPCL101/files/eov321_2006.csv

//add the refix http to the link to use abolute access
$link = "http://localhost/".$link;
$parts = explode(" ",$format); //break the input format into parts

$db = mysql_connect("localhost","root","password1");//connect to database

//open file or catch error
$file =  fopen($link, "r")
or exit("Unable to open file!<br><br>" .
"<a href='extract_data_file.php'>Back</a>");

//create a temp password for student = 12345
$password = 12345;
$x=1; //Type holder
//Read a line of the file until the end is reached
while(!feof($file))
{
$line= fgets($file);//read line
$linebreak = explode(",",$line );//break up the line
for($i=0;$i<3;$i++)
{
switch($parts[$i])
{
case "%name"  : breakname($linebreak[$i]);
break;
case "%num"  : $lnumber = $linebreak[$i];
break;
case "%course": $course = $linebreak[$i];
break;
}
}
//Write line data to database  
mysql_select_db("Students",$db);
  $sql = "INSERT INTO Students (S_StudentNum,S_Password,S_Surname,S_Title,S_Initials)
VALUES ('$lnumber','$password','$surname','$title','$initials')";
$result = mysql_query($sql);

//Write line data into Course links
mysql_select_db("CourseLinks",$db);
$sql = "INSERT INTO CourseLinks (CL_Type,CL_Person,CL_Course)
VALUES ('$x','$lnumber','$course')";
$result = mysql_query($sql);
}
fclose($file);
echo "Thank you! Information updated.<br>";
?>

#2 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 August 2006 - 08:57 AM

You don't check for errors returned after executing queries.

<?php
$result = mysql_query($sql) or die('Query: "'.$sql.'" failed, MySQL returned: "'.mysql_error().'"');
?>

It IS in the FAQ you know...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users