php - simple upload of image in database for sign up user -
using php , pdo able make sign page out saving image
$firstname = trim($_post['fn']); //at minimus clear whitespace. $lastname = trim($_post['ln']); $username = trim($_post['un']); $password = trim($_post['pw']); $confirmpassword= trim($_post['cp']); $stmt = $dbh->prepare("insert registration (fname,lname,username,password) values (?,?,?,?)"); $stmt->bindvalue(1,$firstname,pdo::param_str); $stmt->bindvalue(2,$lastname,pdo::param_str); $stmt->bindvalue(3,$username,pdo::param_str); $stmt->bindvalue(4,$password,pdo::param_str); if($stmt->execute()){ echo "your registration completed..."; }
i found tutorial complicated me understand , not explained looking ideas or tutorial easy understand on how upload image..any idea appreaciated
form
<form method="post" action="crud.php"> <tr> <td> </td> <td> <input type="file" name="image" /> </td> </tr> <tr> <td>first name</td> <td> <input type="text" name="fn"> </td> </tr> <tr> <td>last name</td> <td> <input type="text" name="ln"> </td> </tr> <tr> <td>user name</td> <td> <input type="text" name="un"> </td> </tr> <tr> <td>password</td> <td> <input type="password" name="pw"> </td> </tr> <tr> <td>confirm password</td> <td> <input type="password" name="cp"> </td> </tr> <tr> <td> <input id="button" type="submit" value="back" name="back"/> </td> <td> <input id="button" type="submit" value="signup" name="signup"/> </td> </tr> <tr><td><div style="font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div></td></tr> </form>
here simple example of how achieve that:
if(is_uploaded_file($_files['image']['tmp_name'])){ $folder = "upload/"; $file = basename( $_files['image']['name']); $full_path = $folder.$file; if(move_uploaded_file($_files['image']['tmp_name'], $full_path)) { echo "succesful upload, have image!"; $firstname = trim($_post['fn']); $lastname = trim($_post['ln']); $username = trim($_post['un']); $password = trim($_post['pw']); $confirmpassword= trim($_post['cp']); $stmt = $dbh->prepare("insert registration (fname,lname,username,password, img_url) values (?,?,?,?,?)"); $stmt->bindvalue(1,$firstname,pdo::param_str); $stmt->bindvalue(2,$lastname,pdo::param_str); $stmt->bindvalue(3,$username,pdo::param_str); $stmt->bindvalue(4,$password,pdo::param_str); $stmt->bindvalue(5,$full_path,pdo::param_str); if($stmt->execute()){ echo "your registration completed..."; }else{ echo 'your registration not completed...'; } } else { echo "upload received! process failed"; } }else{ echo "upload failure ! nothing uploaded"; }
in query have included field called img_url
.
the pdo insert query executed once image uploaded successfully.
Comments
Post a Comment