Jump to content

PHP & MYSQL Problem


designedfree4u

Recommended Posts

I am slowly learning php and im try to combine two tutorials. The form is posting fine to the database  ,but it creates two rows. one row for the blob and one for all the text.  My goal is to place all the info in one single row.

 

Thanks for the help

 

Code:

<?
$username="";
$password="";
$database="jaybirdf_RealEstate";

$Address=$_POST['Address'];
$Picture=$_POST['Picture'];
$City=$_POST['City'];
$Map=$_POST['Map'];
$Bed=$_POST['Bed'];
$Bath=$_POST['Bath'];
$Terms=$_POST['Terms'];
$SQFT=$_POST['SQFT'];
$LSQFT=$_POST['LSQFT'];
$Price=$_POST['Price'];
$file=$_POST['Image'];
$type=$_POST['FileType'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO Listing (Address,City,Map,Bed,Bath,Terms,SQFT,LSQFT,Price,Picture,Image,FileType)
VALUES ('$Address','$City','$Map','$Bed','$Bath','$Terms','$SQFT','$LSQFT','$Price','$Picture','$file','$type')";
mysql_query($query);




//File upload Part


      if ($_POST['Submit']) {
        if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
          //print_r($_FILES);
          mysql_connect("localhost", "", "") or die(mysql_error());
          mysql_select_db("jaybirdf_RealEstate");
          $photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),
$_FILES['file']['size']));
           $query = sprintf("INSERT INTO Listing(Image, FileType) VALUES
('%s', '%s')", $photo, $_FILES['file']['type']);
           if (mysql_query($query)) {
            $messages[] = "Your files is successfully store in database"; 
           } else {
            $messages[]= mysql_error();
           }
          } else {
           $messages[]="The file is bigger than the allowed size please resize";
          }
        }
      ?>
      <html>
      <head>
      <title>Add Image</title>
      </head>    
      <body>
      <? 
      if (isset($messages)) { 
        foreach ($messages as $message) {
         print $message ."<br>";
        } 
      }
  mysql_close();
      ?>

Link to comment
Share on other sites

You're saying the file posted is being submitted to a separate row? If this is what you're saying, you need to specify the row to put it in in your query. So

 

query = sprintf("INSERT INTO Listing(Image, FileType) VALUES

('%s', '%s')", $photo, $_FILES['file']['type']);

 

would become something like

 

query = sprintf("INSERT INTO Listing(Image, FileType) VALUES

('%s', '%s') WHERE Address='$Address' AND City='$City'", $photo, $_FILES['file']['type']);

 

.. but to even  better specify a particular row, you should have the primary key for your table be an auto_increment to give it a unique ID.

Link to comment
Share on other sites

Inserts always create a new row. So you have an INSERT for the data you get posted, and then an INSERT for the blob down below.

 

Your second INSERT should probably be an UPDATE and you will have to make sure you get a way to know which row to UPDATE, ideally by selecting out the ID of the row you need to update.

 

Cheers.

Link to comment
Share on other sites

     15 final test t t t t t t t t t [bLOB - 0 B]  

     16 final test t t t t t t t t   [bLOB - 0 B]  

     30 testes w w w w w w w w w [bLOB - 0 B]  

     31                     [bLOB - 4.5   KiB] image/pjpeg

 

 

When i upload it creates line 30 and line 31 what i want is

 

     15 final test t t t t t t t t t [bLOB - 0 B]  

     16 final test t t t t t t t t   [bLOB - 0 B]  

     30 testes w w w w w w w w w [bLOB - 4.5   KiB] image/pjpeg  

                         

 

Link to comment
Share on other sites

Yes, when you use the insert command, you will create a new row. You can either combine everything into one insert, or perform the first query as an insert and the second as an update query.

 

I would recommend combining the queries, since it is more efficent. This should work for you:

 

<?php
$username="jaybirdf_Admin";
$password="607101593";
$database="jaybirdf_RealEstate";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

     if ($_POST['Submit']) {
       if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
        $Address=$_POST['Address'];
           $Picture=$_POST['Picture'];
           $City=$_POST['City'];
           $Map=$_POST['Map'];
           $Bed=$_POST['Bed'];
           $Bath=$_POST['Bath'];
           $Terms=$_POST['Terms'];
           $SQFT=$_POST['SQFT'];
           $LSQFT=$_POST['LSQFT'];
           $Price=$_POST['Price'];
           $file=$_POST['Image'];
           $type=$_POST['FileType'];
         //print_r($_FILES);

         $photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),
$_FILES['file']['size']));
           $query = "INSERT INTO Listing (Address,City,Map,Bed,Bath,Terms,SQFT,LSQFT,Price,Picture,Image,FileType)
VALUES ('$Address','$City','$Map','$Bed','$Bath','$Terms','$SQFT','$LSQFT','$Price','$Picture','$photo','".$_FILES['file']['type']."')";
          if (mysql_query($query)) {
           $messages[] = "Your files is successfully store in database"; 
          } else {
           $messages[]= mysql_error();
          }
         } else {
          $messages[]="The file is bigger than the allowed size please resize";
         }
       }
     ?>
     <html>
     <head>
     <title>Add Image</title>
     </head>    
     <body>
     <? 
     if (isset($messages)) { 
       foreach ($messages as $message) {
        print $message ."<br>";
       } 
     }
  mysql_close();
     ?>

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.