Jump to content

Problem with Data insert into DB!


Buglish

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>";
?>
Link to comment
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...
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.