daredavil07
Addict
- Joined
- Aug 24, 2018
- Posts
- 63
- Reaction
- 17
- Points
- 83
Hello Po, nag-aaral po ako mag PHP and gumawa ng simple na attachment app. Pero everytime po ako nag papasok sa DB, nag a-add po ng separate row yung attachment link na dapat po sa iisang row lang.
DB:
Code:
May screenshot po ako sa baba. Patulong naman po. Thank you in advance!
DB:
Code:
CREATEDATABASE`order`
CREATETABLE`item`
id INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
description VARCHAR(100) NOT NULL,
path VARCHAR(100) NOT NULL,
Code:
HTML:
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
<strong>Item Name: </strong>
<textarea style="resize: none;" type="text" id="subject" name="name" rows="1" cols="53" maxlength="50" placeholder="Write here..." required></textarea><br/>
<strong>Desciption: </strong>
<textarea style="resize: none;" type="text" id="issue" name="description" rows="4" cols="53" maxlength="200" placeholder="Write here..." required></textarea><br/><br/>
<label>Upload Image:
<input id="filefield" type="file" name="filefield" multiple/>
</label><br/><br/>
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<button type="submit" name="submit" >Submit Item</button>
</label></form>
</body>
</html>
PHP:
<?php
$conn = mysqli_connect("localhost","root","","order");
if(mysqli_errno($conn))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
$name = mysqli_real_escape_string($conn, $_POST['name']);
$description = mysqli_real_escape_string($conn, $_POST['description']);
if ($name == '' || $description == '' )
{
$error = 'ERROR: Please fill required fields!';
renderForm($name, $description);
}
else
{
$query = "INSERT INTO `item`(`name`, `description`) VALUES ('$name','$description')";
$result = mysqli_query($conn, $query);
if($result)
{
echo '<script language="javascript">';
echo 'alert("Item created!")';
echo '</script>';
}
}
}
{
}
?>
//attachment portion
<?php
if(isset($_FILES['filefield'])){
$file=$_FILES['filefield'];
$upload_directory='uploads/';
$folder = "uploads";
if(!is_dir($folder)) mkdir($folder);
$ext_str = "gif,jpg,jpeg,mp3,tiff,bmp,doc,docx,ppt,pptx,txt,pdf";
$allowed_extensions=explode(',',$ext_str);
$max_file_size = 10485760;
$ext = substr($file['name'], strrpos($file['name'], '.') + 1);
if (!in_array($ext, $allowed_extensions) ) {
echo "only".$ext_str." files allowed to upload";
}
if($file['size']>=$max_file_size){
echo "only the file less than ".$max_file_size."mb allowed to upload";
}
$path=md5(microtime()).'.'.$ext;
if(move_uploaded_file($file['tmp_name'],$upload_directory.$path)){
//mysqli_connect("localhost", "root", "", "arc") or die ("Connecting to MySQL failed");
//if(move_uploaded_file($_FILES['filefield']['tmp_name'],$upload_directory.$path)){
$conn = mysqli_connect("localhost", "root", "", "arc") or die ("Connecting to DB failed");
// If you want to save image name you can get like below:
$filefield = $_FILES["filefield"]["name"];
$path = $path."/".$filefield;
$sql = "INSERT INTO item (path) VALUES ('$path')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
?>
May screenshot po ako sa baba. Patulong naman po. Thank you in advance!
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.