Jump to content

php insert data and upload file at the same time???


DasHaas

Recommended Posts

I have a form that needs to perform different actions, insert validated data into my table, and upload a file to my server. I also need the file location to be inserted into one of the fields in my table. I got everything working except for the upload & insert file path part. Here is my code, any help would be greatly appreciated!

 

<?php
}
else
{
   // includes
include('includes/PXS_Conf.php');

    // set up error list array
    $errorList = array();
    $name = ucfirst($_POST['txtname']);
    $email = ($_POST['txtemail']);
    $phone = ($_POST['txtphone']);
    $password = trim($_POST['txtpassword']);
$password1 = trim($_POST['txtpassword1']);
$impressions = ($_POST['sslimpressions']);
$adtype = ($_POST['ssladtype']);
$file = ($_POST['file']);
$url = ($_POST['txturl']);
$alt = ucwords($_POST['txtalt']);

   
    // validate text input fields
    if (trim($_POST['txtname']) == '') 
    {$errorList[] = 'Please enter your name';}

    if (trim($_POST['txtemail']) == '') 
    {$errorList[] = "Please enter your email address";}
    
    if (trim($_POST['txtphone']) == '') 
    {$errorList[] = "Please enter your phone number";}
    
    if (trim($_POST['txtpassword']) == '') 
    {$errorList[] = "Please enter a password";}
    
    if (trim($_POST['txtpassword1']) == '') 
    {$errorList[] = "Please re-enter your password";}
    
    if (trim($_POST['sslimpressions']) == '') 
    {$errorList[] = "Please select a number of impressions";}    
    
    if (trim($_POST['ssladtype']) == '') 
    {$errorList[] = "Please select ad type";}	
    
    if (trim($_POST['file']) == '') 
    {$errorList[] = "Please select an image to upload";}	

    if (trim($_POST['txturl']) == '') 
    {$errorList[] = "Please enter your website url";}	

    if (trim($_POST['txtalt']) == '') 
    {$errorList[] = "Please enter your business name";}

if ($_POST['txtpassword'] != $_POST['txtpassword1'])
{$errorList[] = "Passwords did not match";}


    // check for errors
    // if none found...
    if (sizeof($errorList) == 0)
    {
        // open database connection
        $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect to MySql server!');

        // select database
        mysql_select_db($db) or die ('Unable to select database');

        // generate and execute query
        $query = "INSERT INTO PXS_Ads(href, src, alt, impressions_bought, impressions_left, ad_date, contact_name, contact_email, contact_phone, status, type, password) VALUES('$url','$file','$alt','$impressions','$impressions',NOW(),'$name','$email','$phone','0','$adtype','$password')";
        $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

        // close database connection
        mysql_close($connection);

        // print result
        echo '<center>
	Request subitted!<br />
	We will be contacting you shortly<br />
	<a href=index.php>Return to website</a>		
	</center>';

    }
    else
    {
        // errors found
        // print as list
        echo 'The following errors were encountered:'; 
        echo '<br>';
        echo '<ul>';
        for ($x=0; $x<sizeof($errorList); $x++)
        {
            echo "<li>$errorList[$x]";
        }
        echo '</ul>';
    }
}
?>

 

???Thanks in advance! ???

Well I tried the following and it did not work, I looked at the site you gave me but I had problems deciphering the code. It seems like they had some validating functions as well, I just need it to work first then I will try to add bells and whistles. I snagged this from another post but it does not work for me ???.

 

		$target_path = "images/";
	$target_path = $target_path . basename( $_FILES['file']['name']);

	if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
  		  echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";}

 

Im confused with the following parts:

file - should this be the name of the upload field in my form?

name - is this from the form?

tmp_name - is this somthing i assign or is it the original name from the users pc


<?php
// form not yet submitted
// display initial form
if (!$_POST['submit'])
{
?>
<table width="484" cellpadding="3" cellspacing="3" class="table1">
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <tr>
      <td width="199" valign="top" class="text5"><div align="right" class="form_text">name</div></td>
      <td width="262"><input name="txtname" type="text" id="txtname" /></td>
    </tr>
    <tr>
      <td valign="top" class="text5"><div align="right"><span class="form_text">email address</span>
        <div class="form_text_desc">We fight spam.</div>
</div></td>
      <td><input name="txtemail" type="text" id="txtemail" /></td>
    </tr>
    <tr>
      <td valign="top" class="text5"><div align="right" class="form_text">phone number  </div></td>
      <td><input name="txtphone" type="text" id="txtphone" maxlength="10" /></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">password
        </span>
        <div class="form_text_desc">Make it strong!</div>
      </div></td>
      <td><input name="txtpassword" type="password" id="txtpassword" maxlength="7" /></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">confirm password
        </span>
        <div class="form_text_desc">Really strong...</div>
</div></td>
      <td><input name="txtpassword1" type="password" id="txtpassword1" maxlength="7" /></td>
    </tr>
    <tr>
      <td><div align="right" class="form_text">number of impressions </div></td>
      <td><select name="sslimpressions" id="sslimpressions">
        <option>number of impressions</option>
        <option value="5000">5,000</option>
        <option value="10000">10,000</option>
        <option value="15000">15,000</option>
        <option value="20000">20,000</option>
      </select></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">ad type
        </span>
        <div class="form_text_desc">Body = 200x249 | Header = 134x23</div>
</div></td>
      <td><select name="ssladtype" id="ssladtype">
        <option>select ad size</option>
        <option value="1">header ad</option>
        <option value="2">body ad</option>
      </select></td>
    </tr>
    <tr>
      <td><div align="right" class="form_text">upload image<span class="form_text_desc"><br />
        .GIF, .JPG only, 300k max
      </span><br />
</div></td>
      <td><input type="file" name="file" /></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">your website url
        </span>
        <div class="formexpl"><span class="form_text_desc">i.e. http://www.yoursite.com </span>.</div>
</div></td>
      <td><input name="txturl" type="text" id="txturl" /></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">image description</span>
        <div class="form_text_desc">i.e YOUR  NAME ...</div>
</div></td>
      <td><input name="txtalt" type="text" id="txtalt" /></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
        <input type="submit" name="submit" value="Add" />
        <input type="reset" name="Reset" value="Reset" />
      </div></td>
    </tr>
  </form>
</table>
<?php
}
else
{
   // includes
include('includes/PXS_Conf.php');

    // set up error list array
    $errorList = array();
    $name = ucfirst($_POST['txtname']);
    $email = ($_POST['txtemail']);
    $phone = ($_POST['txtphone']);
    $password = trim($_POST['txtpassword']);
$password1 = trim($_POST['txtpassword1']);
$impressions = ($_POST['sslimpressions']);
$adtype = ($_POST['ssladtype']);
$file = ($_FILES['file']);
$url = ($_POST['txturl']);
$alt = ucwords($_POST['txtalt']);

   
    // validate text input fields an check password
    if (trim($_POST['txtname']) == '') 
    {$errorList[] = 'Please enter your name';}

    if (trim($_POST['txtemail']) == '') 
    {$errorList[] = "Please enter your email address";}
    
    if (trim($_POST['txtphone']) == '') 
    {$errorList[] = "Please enter your phone number";}
    
    if (trim($_POST['txtpassword']) == '') 
    {$errorList[] = "Please enter a password";}
    
    if (trim($_POST['txtpassword1']) == '') 
    {$errorList[] = "Please re-enter your password";}
    
    if (trim($_POST['sslimpressions']) == '') 
    {$errorList[] = "Please select a number of impressions";}    
    
    if (trim($_POST['ssladtype']) == '') 
    {$errorList[] = "Please select ad type";}	
    
    if (trim($_POST['file']) == '') 
    {$errorList[] = "Please select an image to upload";}	

    if (trim($_POST['txturl']) == '') 
    {$errorList[] = "Please enter your website url";}	

    if (trim($_POST['txtalt']) == '') 
    {$errorList[] = "Please enter your business name";}

if ($_POST['txtpassword'] != $_POST['txtpassword1'])
{$errorList[] = "Passwords did not match";}


    // check for errors
    // if none found...
    if (sizeof($errorList) == 0)
    {
	//upload file to server
	$target_path = "images/";
	$target_path = $target_path . basename( $_FILES['file']['txtname']);

	if(move_uploaded_file($_FILES['file']['name'], $target_path)) {
  		  echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";}

        // open database connection
        $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect to MySql server!');

        // select database
        mysql_select_db($db) or die ('Unable to select database');
	  
        // generate and execute query
        $query = "INSERT INTO PXS_Ads(href, src, alt, impressions_bought, impressions_left, ad_date, contact_name, contact_email, contact_phone, status, type, password)        VALUES('$url','$uploadfile','$alt','$impressions','$impressions',NOW(),'$name','$email','$phone','0','$adtype','$password')";
        $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

        // close database connection
        mysql_close($connection);

echo "<li>$errorList[$x]";

 

You can't do that can you? Wouldn't he need to use this???

echo "<li>{$errorList[$x]}";

 

No he can do that, it would be a problem if single quotes were in use, than you would need the { } curly braces around it.


	$target_path = "images/";
	$target_path = $target_path . basename( $_FILES['file']['txtname']);

	move_uploaded_file($_FILES['file']['name'], $target_path);

 

 

This is the form

 


  <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <tr>
      <td width="234" valign="top" class="text5"><div align="right" class="form_text">Your name</div></td>
      <td width="227"><input name="txtname" type="text" id="txtname" /></td>
    </tr>
    <tr>
      <td valign="top" class="text5"><div align="right"><span class="form_text">email address</span>
        <div class="form_text_desc">We fight spam</div>
</div></td>
      <td><input name="txtemail" type="text" id="txtemail" /></td>
    </tr>
    <tr>
      <td valign="top" class="text5"><div align="right" class="form_text">phone number  </div></td>
      <td><input name="txtphone" type="text" id="txtphone" maxlength="10" /></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">password
        </span>
        <div class="form_text_desc">Make it strong!</div>
      </div></td>
      <td><input name="txtpassword" type="password" id="txtpassword" maxlength="7" /></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">confirm password
        </span>
        <div class="form_text_desc">Really strong...</div>
</div></td>
      <td><input name="txtpassword1" type="password" id="txtpassword1" maxlength="7" /></td>
    </tr>
    <tr>
      <td><div align="right" class="form_text"> impressions </div></td>
      <td><select name="sslimpressions" id="sslimpressions">
        <option>number of impressions</option>
        <option value="5000">5,000</option>
        <option value="10000">10,000</option>
        <option value="15000">15,000</option>
        <option value="20000">20,000</option>
      </select></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">type
        </span>
        <div class="form_text_desc">Body = 200x249 | Header = 468x60</div>
</div></td>
      <td><select name="ssladtype" id="ssladtype">
        <option>select ad size</option>
        <option value="1">header ad</option>
        <option value="2">body ad</option>
      </select></td>
    </tr>
    <tr>
      <td><div align="right" class="form_text">upload image<span class="form_text_desc"><br />
        .GIF, .JPG only, 300k max
      </span><br />
</div></td>
      <td><input type="file" name="file" /></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">your website url<br />
      </span><span class="form_text_desc">i.e. http://www.yoursite.com</span> </div>
        </div></td>
      <td><input name="txturl" type="text" id="txturl" /></td>
    </tr>
    <tr>
      <td><div align="right"><span class="form_text">image description</span>
        <div class="form_text_desc">i.e YOUR COMPANY NAME ...</div>
</div></td>
      <td><input name="txtalt" type="text" id="txtalt" /></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
        <input type="submit" name="submit" value="Add" />
        <input type="reset" name="Reset" value="Reset" />
      </div></td>
    </tr>
  </form>

	$target_path = "images/";
	$target_path = $target_path . basename( $_POST['txtname']);

	move_uploaded_file($_FILES['file']['tmp_name'], $target_path);

 

I highly suggest looking at this site closer, as it seems you just skimmed over it and ignored most of it.

 

http://us3.php.net/features.file-upload

Archived

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

×
×
  • 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.