Jump to content

Archived

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

Buglish

Problem with Data insert into DB!

Recommended Posts

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>";
?>

Share this post


Link to post
Share on other sites
You don't check for errors returned after executing queries.

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

It IS [url=http://www.phpfreaks.com/forums/index.php/topic,95443.0.html]in the FAQ[/url] you know...

Share this post


Link to post
Share on other sites

×

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.