Hi guys Im sorry but I have yet another php mysql problem! Im getting garbage inserted into the database w/ the line of code:
$AddRoster="INSERT into Roster (ID, Last, First, SSN, Email, CourseID) VALUES
(NULL, '$data[0]', '$data[1]', '$data[2]', '$NewUserEmail', '$CourseID')";
$AddRosterResult=mysql_query($AddRoster);
the other database insert into the user table works fine:
$AddUser="INSERT into User (ID, BearID, SSN, UserType) VALUES
(NULL, '$NewUserBearID', '$NewUserSSN', '$NewUserType')";
$AddUserResult=mysql_query($AddUser);
and ID in both tables is auto_increment. all of the variables have the data they are supposed to have, I have tested for that so I know its got to be that roster insert. I posted another post a few days ago about another problem in which one of you said my while loop is incorrect but didn't say how. I am really stuck now and would gladly appreciate any help on this. Here is my entire code if it helps:
<?php
$updir = "/usr/local/mysql/bin/datafiles"; //absolute path to where files are uploaded
$sizelim = "yes"; //do you want size limitations yes or no
$size = "25000"; //if you want size limited how many bytes
$certtype = "no"; //do you want certain type of file, no recommended
$type = ""; //what type of file would you like
//error if no file is selected
if ($file_name == "") {
die("No file selected to upload");
}
//error if file exists
if (file_exists("$updir/$file_name")) {
die("The file you are trying to upload already exists");
}
//error if file is to big
if ($sizelim == "yes") {
if ($file_size > $size) {
die("The file you are trying to upload is too big.");
}
}
//error if file isn't certain type
if ($certtype == "yes") {
if ($type != $file_type) {
die("The file you are trying to upload is wrong type");
}
}
@copy($file, "$updir/$file_name") or die("The file you are trying to upload couldn't be
copied to the server");
?>
<head>
<title>File Uploaded</title>
</head>
<body>
<p>
<?php
$dbname="mydb";
$dbhost="Schemp";
if (!($db = mysql_connect("localhost", "root")))
echo "error connecting to ". $dbname ."on ". $dbhost;
mysql_select_db($dbname,$db);
$content = file("/usr/local/mysql/bin/datafiles/$file_name");
//$CourseIDArray = explode ("\n", $content[0]);
//$CourseID=$CourseIDArray[0];
//echo $CourseID;
$NewUserType="Student";
$Counter=1;
while (list($key, $val) = each ($content))
{
if ($Counter==1)
{
$CourseIDArray=explode ("\n", $content[0]);
$CourseID=$CourseIDArray[0];
echo $CourseID;
}
if ($Counter>1)
{
$data = explode("\t", $val);
echo $data[0];
echo "<br>";
echo $data[1];
echo "<br>";
$NewUserSSN = $data[2];
echo $NewUserSSN;
echo "<br>";
$NewUserEmailArray= explode ("\n", $data[3]);
$NewUserEmail=$NewUserEmailArray[0];
//echo $NewUserEmail;
$NewUserBearIDArray = split('[@]', $data[3]);
$NewUserBearID = $NewUserBearIDArray[0];
echo $NewUserBearID;
$bearid_result = mysql_query("select BearID from User where BearID='$NewUserBearID' ");
$num_bearid_records = mysql_num_rows($bearid_result);
if($num_bearid_records <1)
{
$BearIDFlag=FALSE;
} else
{
$BearIDFlag=TRUE;
}
$ssn_result = mysql_query("select SSN from User where SSN='$NewUserSSN' ");
$num_ssn_records = mysql_num_rows($ssn_result);
if ($num_ssn_records < 1)
{
$SSNFlag=FALSE;
} else
{
$SSNFlag=TRUE;
}
if (($BearIDFlag==TRUE)||($SSNFlag==TRUE))
{
$ssn_roster_result=mysql_query("select * from Roster where SSN = '$NewUserSSN' ");
$num_ssn_roster_records=mysql_num_rows($ssn_roster_result);
if ($num_ssn_roster_records < 1)
{
$SSNRosterFlag=FALSE;
} else
{
$SSNRosterFlag=TRUE;
}
$bearid_roster_result=mysql_query(" select * from Roster where Email='$data[3]' ");
$num_bearid_roster_records=mysql_num_rows($bearid_roster_result);
if ($num_bearid_roster_records < 1)
{
$BearIDRosterFlag=FALSE;
} else
{
$BearIDRosterFlag=TRUE;
}
if (($BearIDRosterFlag==TRUE)||($SSNRosterFlag==TRUE))
{
die("User already is in the Roster and User table!");
} else
{
$AddRoster="INSERT into Roster (ID, Last, First, SSN, Email, CourseID) VALUES
(NULL, '$data[0]', '$data[1]', '$data[2]', '$NewUserEmail', '$CourseID')";
$AddRosterResult=mysql_query($AddRoster);
}
}
if (($BearIDFlag==FALSE)&&($SSNFlag==FALSE))
{
//Add user to the user table, assumed to all be students
$AddUser="INSERT into User (ID, BearID, SSN, UserType) VALUES
(NULL, '$NewUserBearID', '$NewUserSSN', '$NewUserType')";
$AddUserResult=mysql_query($AddUser);
$error= mysql_error();
echo $error;
echo "<br>";
$ssn_roster_result=mysql_query("select from Roster where SSN='$NewUserSSN' ");
$num_ssn_roster_records=mysql_num_rows($ssn_roster_result);
if ($num_ssn_roster_records < 1)
{
$SSNRosterFlag=FALSE;
} else {
$SSNRosterFlag=TRUE; }
$bearid_roster_result=mysql_query(" select from Roster where Email = '$data[3]'");
$num_bearid_roster_records=mysql_num_rows($bearid_roster_result);
if ($num_bearid_roster_records <1)
{
$BearIDRosterFlag=FALSE;
} else {
$BearIDRoster=TRUE; }
if (($BearIDRosterFlag==TRUE)||($SSNRosterFlag==TRUE))
{
die ("User with BearID $BearID already is in Roster table!");
} else {
$AddRoster="INSERT into Roster (ID, Last, First, SSN, Email, CourseID) VALUES
(NULL, '$data[0]', '$data[1]', '$data[2]', '$NewUserEmail', '$CourseID')";
$AddRosterResult=mysql_query($AddRoster);
}
$result = mysql_query($sql, $db);
}
}
$Counter++;
echo $Counter;
}
echo "The file is uploaded.";
?>
Laura Young