Jump to content

Store image path in mysql


nelquintin

Recommended Posts

I have a upload script that save images in an image folder. What I would like to do is pass the path in to another php page and then insert the image path into the database so as to link text and images together.

below is the code...

 

<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>Images Upload </strong></td>
</tr>
<tr>
<td>Select file 
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="sumbit" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
//set where you want to store files
//in this example we keep file in folder upload 
//$_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1= "images/".$_FILES['ufile']['name'][0];
$path2= "images/".$_FILES['ufile']['name'][1];
$path3= "images/".$_FILES['ufile']['name'][2];

//copy file to where you want to store file
copy($_FILES['ufile']['tmp_name'][0], $path1);
copy($_FILES['ufile']['tmp_name'][1], $path2);
copy($_FILES['ufile']['tmp_name'][2], $path3);
//$_FILES['ufile']['name'] = file name
//$_FILES['ufile']['size'] = file size
//$_FILES['ufile']['type'] = type of file
echo "File Name :".$_FILES['ufile']['name'][0]."<BR/>"; 
echo "File Size :".$_FILES['ufile']['size'][0]."<BR/>"; 
echo "File Type :".$_FILES['ufile']['type'][0]."<BR/>"; 
echo "<img src=\"$path1\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$_FILES['ufile']['name'][1]."<BR/>"; 
echo "File Size :".$_FILES['ufile']['size'][1]."<BR/>"; 
echo "File Type :".$_FILES['ufile']['type'][1]."<BR/>"; 
echo "<img src=\"$path2\" width=\"150\" height=\"150\">";
echo "<P>";

echo "File Name :".$_FILES['ufile']['name'][2]."<BR/>"; 
echo "File Size :".$_FILES['ufile']['size'][2]."<BR/>"; 
echo "File Type :".$_FILES['ufile']['type'][2]."<BR/>"; 
echo "<img src=\"$path3\" width=\"150\" height=\"150\">";

///////////////////////////////////////////////////////

// Use this code to display the error or success.

$filesize1=$_FILES['ufile']['size'][0];
$filesize2=$_FILES['ufile']['size'][1];
$filesize3=$_FILES['ufile']['size'][2];

if($filesize1 && $filesize2 && $filesize3 != 0) 
{
echo "Uploaded";
}

else {
echo "ERROR.....";
}

//////////////////////////////////////////////

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}

if($filesize3==0) {
echo "There're something error in your third file";
echo "<BR />";
}

?>

<html>
<head>
<title>Add an Property</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<?php
// Get the PHP file containing the DbConnector class
require_once('../includes/DbConnector.php');
require_once('../includes/Validator.php');

// Create an instance of DbConnector
$connector = new DbConnector();

// Check whether a form has been submitted. If so, carry on
if ($_POST){

// Validate the entries
$validator = new Validator();

$validator->validateTextOnly($_POST['price'],'Price');
$validator->validateTextOnly($_POST['description'],'Description');

// Check whether the validator found any problems
if ( $validator->foundErrors() ){
echo 'There was a problem with: <br>'.$validator->listErrors('<br>'); // Show the errors, with a line between each
}else{

// Create an SQL query (MySQL version)
// The 'addslashes' command is used 5 lines below for added security
// Remember to use 'stripslashes' later to remove them (they are inserted in front of any
// special characters
//"SELECT * FROM `properties` WHERE `id` = '1'"
$insertQuery = "INSERT INTO `properties` (`price`, `description`, `imagepath`) VALUES ('{$_POST['price']}' , '{$_POST['description']}' , '{$_POST['imagepath']}')";
// Save the form data into the database 
if ($result = $connector->query($insertQuery)){

	// It worked, give confirmation
	echo '<center><b>Property added to the database</b></center><br>';

}else{

	// It hasn't worked so stop. Better error handling code would be good here!
	exit('<center>Sorry, there was an error saving to the database</center>');


}
}
}
?>

<body>
<form name="form1" method="post" action="newproperty.php">
        <p> Price:
          <input name="price" type="text" id="price">
        </p>
        <p> Description:
          <input name="description" type="text" id="description">
        </p>
        <a href="upload.php">Add pictures</a>
        </p>
        <p align="center">
          <input type="submit" name="Submit" value="Submit">
        </p>
        
</form>
</body>
</html>

 

Link to comment
Share on other sites

ive tried this now? any ideas please

<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="test.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>Property Upload </strong></td>
</tr>
<tr>
<td>
Price:
<input name="price" type="text" id="price"></td>
</tr>
<tr>
<td>Description:
<input name="description" type="text" id="description"></td>
</tr>        
      

<td>Select file 
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="sumbit" /></td>
</tr>

</table>
</td>
</form>
</tr>
</table>
<?php

// Get the PHP file containing the DbConnector class
require_once('../includes/DbConnector.php');
require_once('../includes/Validator.php');

// Create an instance of DbConnector
$connector = new DbConnector();

// Check whether a form has been submitted. If so, carry on
if ($_POST){

// Validate the entries
$validator = new Validator();

$validator->validateTextOnly($_POST['price'],'Price');
$validator->validateTextOnly($_POST['description'],'Description');
//set where you want to store files
//in this example we keep file in folder upload 
//$_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1= "images/".$_FILES['ufile']['name'][0];
$path2= "images/".$_FILES['ufile']['name'][1];
$path3= "images/".$_FILES['ufile']['name'][2];

//copy file to where you want to store file
copy($_FILES['ufile']['tmp_name'][0], $path1);
copy($_FILES['ufile']['tmp_name'][1], $path2);
copy($_FILES['ufile']['tmp_name'][2], $path3);

// Check whether the validator found any problems
if ( $validator->foundErrors() ){
echo 'There was a problem with: <br>'.$validator->listErrors('<br>'); // Show the errors, with a line between each
}else{

// Create an SQL query (MySQL version)
// The 'addslashes' command is used 5 lines below for added security
// Remember to use 'stripslashes' later to remove them (they are inserted in front of any
// special characters
//"SELECT * FROM `properties` WHERE `id` = '1'"
$insertQuery = "INSERT INTO `properties` (`price`, `description`, `path1`, `path2`, `path3`) VALUES ('{$_POST['price']}' , '{$_POST['description']}' , '{$_POST['path1']}' , '{$_POST['path2']}' , '{$_POST['path3']}' ,)";
// Save the form data into the database 
if ($result = $connector->query($insertQuery)){

	// It worked, give confirmation
	echo '<center><b>Property added to the database</b></center><br>';

}else{

	// It hasn't worked so stop. Better error handling code would be good here!
	exit('<center>Sorry, there was an error saving to the database</center>');


}
}
}
?>

</body>
</html>


 

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.