Page 1 of 1

data being send to two difrent rows

Posted: Thu Dec 21, 2006 9:28 am
by franknu
ok i have a form where all the data is being submitted from all the text goes to one row but the file is going to the next row, i think that my problem is on the insert code. please help here is my code

Code: Select all

<?

$host = "localhost";
$username = "townsfin_localho";
$password = "abc123";
$database = "townsfin_contacts";

$db = mysql_connect($host, $username, $password);
mysql_select_db($database);






// Business Info

 $BusinessName = addslashes ($_POST['BusinessName']);
 $Slogan = addslashes($_POST['Slogan']);
 $Business_Address = addslashes($_POST['Business_Address']);
 $Tel = addslashes($_POST['Tel']);
 $Website = addslashes($_POST['Website']);
 $Email = addslashes($_POST['Email']);
 $Member_Status = addslashes($_POST['Member_Status']);
 $Fax =addslashes($_POST['Fax']);
 $type = addslashes($_POST['type']);
 $make = addslashes($_POST['make']);

// webpage info

 $Categories = addslashes($_POST['Categories']);
 $Keyword = addslashes ($_POST['Keyword']);
 $Picture1 = addslashes (isset ($_POST['Picture1']));
 $Headline = addslashes ($_POST['Headline']);
 $Slogan2 = addslashes ($_POST['Slogan2']);
 $Description1 = addslashes ($_POST['Description1']);
 $Description2 = addslashes ($_POST['Description2']);
 $Description3= addslashes ($_POST['Description3']);
 $Contact2 = addslashes ($_POST['Contact2']);
 $Picture2 = addslashes (isset($_POST['Picture2']));
 $Picture3 = addslashes (isset($_POST['Picture3']));

 $User_Name = addslashes ($_POST['User_Name']);
 $Password = addslashes ($_POST['Password']);


if(!$db)

{
echo " Error: could not connect to database.";

exit;
   }


                   
    //Business Info

 $sql="INSERT INTO `business_info`(`BusinessName`,`Slogan`,`Business_Address`,`Tel`,`Website`,`Email`,`Member_Status`,
 `Fax`,`type`,`make`,`Categories`,`Keyword`,`picture1`,`Headline`,`Slogan2`,`Description1`,`Description2`,`Description3`,`Contact2`,
 `Picture2`,`Picture3`,`User_Name`,`Password`)

Values('$BusinessName','$Slogan','$Business_Address','$Tel','$Website','$Email','$Member_Status','$Fax','$type','$make','$Categories','$Keyword',
'$Picture1','$Headline','$Slogan2','$Description1',
'$Description2','$Description3','$Contact2','$Picture2','$Picture3','$User_Name','$Password')";



$result = mysql_query($sql);
echo mysql_error();

if($result)

         {
echo mysql_affected_rows(). ".Business Information Inserted.";

         }


$uploaddir = '/home/townsfin/public_html/business_images/';
$uploadfile = $_FILES['Picture1']['name']; // No need for basename() here

if(!empty($_FILES['Picture1'])){
   var_dump($uploaddir);

   var_dump($_FILES['Picture1']['size']);
   var_dump($_FILES['Picture1']['error']);
   var_dump($_FILE);
   var_dump($_FILES['Picture1']['type']);
   var_dump($_FILES['Picture1']['name']);
}

$fullpath = $uploaddir . $uploadfile;
if (move_uploaded_file($_FILES['Picture1']['tmp_name'], $fullpath)){
   echo("File Uploaded");
   $sql = "INSERT INTO business_info (Picture1) VALUES ('$fullpath')";
   $result = mysql_query($sql);
   if (!$result){
      echo "Error inserting data: " . mysql_error();
   }
}
else {
   echo ("file no uploaded!");
   print_r($_FILES);
   echo realpath('./');
}
?>

Posted: Thu Dec 21, 2006 10:05 am
by clinton
You need to either insert the file information along with all the other information OR use an UPDATE query. if you use the UPDATE query, you'll need to know something that will uniquely define the row you want to insert the file information into, i'm not sure if you have an 'id' (or similar) field that is set to auto increment but that does the job.

something like this will sort that out for you

Code: Select all

$last_row = mysql_insert_id ();
$sql = "UPDATE `business_info` SET  `Picture1` = '$fullPath' WHERE `id` = $last_row";
*NOTE: I haven't tested this so it might not work but something along these lines should do*

Posted: Thu Dec 21, 2006 3:12 pm
by Begby
FYI this will work to remove all the addslashes stuff at the beginning of your script. You might want to switch that to mysql_real_escape() or use prepared statements.

Code: Select all

foreach( $_POST as $key => $value )
{
  $$key = addslashes($value) ;
}