![Antiguo](http://static.forosdelweb.com/fdwtheme/images/statusicon/post_old.gif)
20/04/2012, 16:24
|
| | Fecha de Ingreso: abril-2005
Mensajes: 196
Antigüedad: 19 años, 9 meses Puntos: 2 | |
Insertar valores de array en tabla sql Hola,
estoy haciendo un script que tras añadir unos campos de formulario y unas imágenes adjuntas, añade los valores a la base de datos.
Las imágenes se suben al servidor y los valores del formulario entran a la bbdd, pero no se como meter el array de imágenes $filearray[$k]= $filename; dentro de la tabla en los campos img, img1, img2, img3, img4
Podéis echarme un cable por favor?
Gracias!!
Código:
<?php
error_reporting(0);
$change="";
$abc="";
define ("MAX_SIZE","5120");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
$slike = array(file, file1, file2, file3, file4);
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$filearray = array();
$filearray1 = array();
$k=0;
foreach($slike as $slika){
$image =$_FILES[$slika]["name"];
$uploadedfile = $_FILES[$slika]['tmp_name'];
if ($image)
{
$filename = stripslashes($_FILES[$slika]['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
}
else
{
$size=filesize($_FILES[$slika]['tmp_name']);
if ($size > MAX_SIZE*5024)
{
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES[$slika]['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES[$slika]['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=130;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$image_name=(time()+$k).'.'.$extension;
$filename = "images/". $image_name;
$filename1 = "images/small_". $image_name;
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
$filearray[$k]= $filename;
$filearray1[$k]= $filename1;
$k++;
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}
}
}
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
// mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
$change=' <div class="msgdiv">Image Uploaded Successfully!</div>';
include "conecta.php";
$sql="INSERT INTO Anuncios (passwd, Apodo, Ciudad, CP, AnoNac, PrecioHora, telefono, email, texto, img, img1, img2, img3, img4)
VALUES
(
'$_POST[passwd]',
'$_POST[Apodo]',
'$_POST[Ciudad]',
'$_POST[CP]',
'$_POST[AnoNac]',
'$_POST[PrecioHora]',
'$_POST[telefono]',
'$_POST[email]',
'$_POST[texto]',
'$_POST[img]',
'$_POST[img1]',
'$_POST[img2]',
'$_POST[img3]',
'$_POST[img4]'
)";
if (!mysql_query($sql,$conecta))
{
die('Error: ' . mysql_error());
}
echo "Añadido correctamente";
include "desconecta.php";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head><body>
<div align="left" id="err">
<?php echo $change; ?>
</div>
<form method="post" action="" enctype="multipart/form-data" name="form1">
<table width="600" border="0" align="left" cellpadding="0" cellspacing="0">
<?php
foreach($slike as $c){
?>
<tr>
<td width="100">
<div>Imagen: </div>
</td>
<td>
<div align="left">
<input size="480" name="<?php echo $c?>" type="file"/>
</div>
</td>
</tr>
<?php
}
?>
<tr>
<Td></Td>
<Td>Max file size: <b>5</b>MB</span></Td>
</tr>
Password: <input type="text" name="passwd" /><br>
Apodo: <input type="text" name="Apodo" /><br>
Ciudad: <input type="text" name="Ciudad" /><br>
Codigo Postal: <input type="text" name="CP" /><br>
AnoNac: <input type="text" name="AnoNac" /><br>
PrecioHora: <input type="text" name="PrecioHora" /><br>
Telefono: <input type="text" name="telefono" /><br>
email: <input type="text" name="email" /><br>
Texto: <input type="text" name="texto" /><br>
<tr>
<Td></Td>
<Td valign="top" height="35px"><input type="submit" id="mybut" value="Upload" name="Submit"/></Td>
</tr>
</table>
</form>
</body></html>
|