How to input image in database using pdo? - php

I use PDO to input data to database. I try use this scripts but it doesn't work for image file.
This error message:
Notice:
Undefined index: gambar in C:\xampp\htdocs\data\add-buku.php on line
23 SQLSTATE[HY093]: Invalid parameter number: number of bound
variables does not match number of tokens
How to fix it? Thanks in advance
<?php if(isset($_POST['input-buku'])){
$id_buku=trim($_POST['id_buku']);
$judul_buku=trim($_POST['judul_buku']);
$isbn=trim($_POST['ISBN']);
$harga=trim($_POST['harga']);
$gambar=trim($_FILES['gambar']['name']);
if(strlen($gambar)>0){
if(is_uploaded_file($_FILES['gambar']['tmp_name'])){
move_uploaded_file($_FILES['gambar']
['tmp_name'],"../images/buku".$gambar);
} } try {
$stmt = $con->prepare("SELECT id_buku from tb_buku where id_buku=:id_buku ");
$stmt->execute(array(':id_buku'=>$id_buku));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if($row > 0)
{
$error[] = "maaf ID yang Anda inputkan sudah ada !";
}
else
{
if($crud->create_buku($id_buku, $judul_buku,$isbn, $harga $gambar))
{
$crud->redirect('add-buku.php?berhasil');
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}?>
HTML
<form role="form" method="post">
<div class="form-group">
<label for="id_buku">ID Buku</label>
<input type="text" class="form-control" name="id_buku" value="<?php if(isset($error)){echo $id_buku;}?>" placeholder="Masukan ID Buku">
</div><div class="form-group">
<label for="ISBN">ISBN</label>
<input type="text" class="form-control" name="ISBN" placeholder="Masukan ISBN"></div>
<div class="form-group">
<label for="judul_buku">Judul Buku</label>
<input type="text" class="form-control" name="judul_buku" value="<?php if(isset($error)){echo $judul_buku;}?>" placeholder="Masukan Judul Buku"></div>
<div class="form-group">
<label for="harga">Harga</label>
<input type="text" class="form-control" name="harga" value="<?php if(isset($error)){echo $harga;}?>" placeholder="Masukan Harga Buku"></div>
<div class="form-group">
<label for="gambar">Gambar</label>
<input type="file" name="gambar" value="<?php if(isset($error)){echo $gambar;}?>">
<p class="help-block">Pilih Gambar Buku</p></div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button></div>
</form>

May be you have not added enctype="multipart/form-data" in your form. This is also give like that error. Better post your html code also

Related

How to retain the form inputs when submit button is clicked?

So I got a form that gets the student basic information for temporary admission, so I have a function that when register/submit button is clicked it checks if the Or number is already exist in database when it does it displays error message and the inputted data stays in their own fields.
the function works but when a user inputted a name for example "james joseph" and or number exist in database it will retain the name "james" only.
is there a way that it retains both?
Here's my code for the form
<form class="form-group" method="post" action="index.php">
<div class="row"><!--Top row-->
<div class="col-lg-5"><p><h3>Register an Enrollee</h3></p></div>
<div class="col-lg-3"></div>
<div class="col-lg-4">
</div>
</div>
<br>
<br>
<div class="row">
<div class="col-lg-4">
<label>Academic Year </label>
<?php
if (isset($_GET['academic'])) {
$academic=$_GET['academic'];
echo '<input type="text" name="academic" class="form-control" required value='.$academic.'>';
# code...
}
else
{
echo '<input type="text" name="academic" class="form-control" required>';
}
?>
</div>
<div class="col-lg-4">
<label for="year_level">Year Level</label>
<?php
if (isset($_GET['year'])) {
$year=$_GET['year'];
echo '<input type="text" name="year" class="form-control" required value='.$year.'>';
# code...
}
else
{
echo '<input type="text" name="year" class="form-control" required>';
}
?>
</div>
<div class="col-lg-4">
<label>OR Number</label>
<?php
if (isset($_GET['or'])) {
$or=$_GET['or'];
echo '<input type="text" name="or" class="form-control" required value='.$or.'>';
# code...
}
else
{
echo '<input type="text" name="or" class="form-control" required>';
}
?>
</div>
</div>
<br>
<div class="row">
<div class="col-lg-4">
<label>First Name</label>
<?php
if (isset($_GET['firstname'])) {
$firstname=$_GET['firstname'];
echo '<input type="text" name="firstname" class="form-control" required value='.$firstname.'>';
# code...
}
else
{
echo '<input type="firstname" name="firstname" class="form-control" required>';
}
?>
</div>
<div class="col-lg-4">
<label>Last Name</label>
<?php
if (isset($_GET['lastname'])) {
$lastname=$_GET['lastname'];
echo '<input type="text" name="lastname" class="form-control" required value='.$lastname.'>';
# code...
}
else
{
echo '<input type="text" name="lastname" class="form-control" required>';
}
?>
</div>
<div class="col-lg-4">
<label>Middle Initial</label>
<?php
if (isset($_GET['middle'])) {
$middle=$_GET['middle'];
echo '<input type="text" name="middle" class="form-control" required value='.$middle.'>';
# code...
}
else
{
echo '<input type="text" name="middle" class="form-control" required>';
}
?>
</div>
</div><!--End of Second row-->
<br>
<button class="btn btn-primary btn-block" type="submit" name="register">Register</button>
</form>
Here is the php code that checks and gets the inputted data from the form
if (isset($_POST['register'])) {
# code...
$year_level=$_POST['year'];
$academic_year=$_POST['academic'];
$or_number=$_POST['or'];
$stud_fname=$_POST['firstname'];
$stud_lname=$_POST['lastname'];
$stud_mi=$_POST['middle'];
$or_check_query="SELECT * FROM tbl_user WHERE or_number=$or_number";
$result=mysqli_query($conn,$or_check_query);
$or=mysqli_fetch_assoc($result);
if($or['or_number']===$or_number) {
header("Location: index.php?signup=failed&academic=$academic_year&year=$year_level&or=$or_number&firstname=$stud_fname&lastname=$stud_lname&middle=$stud_mi");
# code...
}
else {
$sql= "INSERT INTO tbl_user(or_number,academic,year,firstname,lastname,middle)
VALUES ('$or_number','$academic_year','$year_level','$stud_fname','$stud_lname','$stud_mi')";
mysqli_query($conn,$sql);
header("Location: index.php?signup=success");
}
}

How to get particular columns from database

I want only 4 columns from database in below
I have to get only 4 column from database for displaying in textfield please give suggestion for that. The table is shown below thank you
$sql_getnm = "SELECT * FROM util WHERE util_head IN ('wc_email', 'wc_contact_us', 'wc_mobile', 'wc_google_map');";
$result_getnm = $connect->query($sql_getnm);
while($row_getnm = $result_getnm->fetch_array())
$util_value_email_data=?
$util_value_mobile_data=?;
$util_value_map_data=?;
$util_value_data=?;
html form
<form id="submitForm" method="post" role="form" name="hl_form" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="mobile_number">Email*</label>
<input class="form-control" id="util_value_email" name="util_value_email" value="<?Php echo $util_value_email_data; ?>" maxlength="250" placeholder="Enter Email Address" type="text">
</div>
<div class="form-group">
<label for="mobile_number">Mobile*</label>
<input class="form-control" id="util_value_mobile" name="util_value_mobile" value="<?Php echo $util_value_mobile_data; ?>" maxlength="250" placeholder="Enter Mobile Number" type="text">
</div>
<div class="form-group">
<label for="mobile_number">Map*</label>
<input class="form-control" id="util_value_map" name="util_value_map" value="<?Php echo $util_value_map_data; ?>" maxlength="250" placeholder="Enter Map Address" type="text">
</div>
<div class="form-group">
<label for="description" class="required">Description*</label>
<textarea class="form-control" style="resize: none;" id="util_value" name="util_value" rows="3" placeholder="Enter Description"><?Php echo $util_value_data; ?></textarea>
</div>
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Below is the code from which you can get all the four data that you want.
$sql_getnm = "SELECT * FROM util WHERE util_head IN ('wc_email', 'wc_contact_us', 'wc_mobile', 'wc_google_map');";
$result_getnm = $connect->query($sql_getnm);
while($row_getnm = $result_getnm->fetch_array()) {
if($row_getnm['util_head'] == 'wc_email'){
$util_value_email_data = $row_getnm['util_value'];
}
if($row_getnm['util_head'] == 'wc_contact_us'){
$util_value_contact_data = $row_getnm['util_value'];
}
if($row_getnm['util_head'] == 'wc_mobile'){
$util_value_mobile_data = $row_getnm['util_value'];
}
if($row_getnm['util_head'] == 'wc_google_map'){
$util_value_map_data = $row_getnm['util_value'];
}
}
You need to replace * with columns name with comma separation.
$sql_getnm = "SELECT Columname1,Columname2,Columname3,Columname4 FROM util WHERE util_head IN ('wc_email', 'wc_contact_us', 'wc_mobile', 'wc_google_map');";
$result_getnm = $connect->query($sql_getnm);
while($row = mysql_fetch_array($result_getnm, MYSQL_ASSOC)
$util_value_email_data= $row['Columname1'];
$util_value_mobile_data= $row['Columname2'];
$util_value_map_data= $row['Columname3'];
$util_value_data= $row['Columname4'];

How to check which form is submited in PHP

I have a problem with a Registration/LogIn form submission.
I have two forms in my main php as follows:
<form role="form" action="profile.php" onsubmit="return validateForm1()" method="post" class="login-form" name="form1" id="form1">
<div class="form-group">
<label class="sr-only" for="form-username">Username</label>
<input type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username">
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Password</label>
<input type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<button type="submit" class="btn" id="btn1" name="btn1">Sign in!</button>
</form>
and
<form role="form" action="profile.php" onsubmit="return validateForm2()" method="post" class="registration-form" name="form2" id="form2">
<div class="form-group">
<label class="sr-only" for="form-first-name">Username</label>
<input type="text" name="form-first-name" placeholder="Username..." class="form-first-name form-control" id="form-username-2">
</div>
<div class="form-group">
<label class="sr-only" for="form-last-name">Email</label>
<input type="text" name="form-last-name" placeholder="Email..." class="form-last-name form-control" id="form-email">
</div>
<div class="form-group">
<label class="sr-only" for="form-email">Password</label>
<input type="password" name="form-email" placeholder="Password..." class="form-email form-control" id="form-password-2">
</div>
<div class="form-group">
<label class="sr-only" for="form-about-yourself">Confirm Password</label>
<input type="password" name="form-confirm-pass" placeholder="Confirm Password..." class="form-email form-control" id="form-password-3">
</div>
<button type="submit" class="btn" name="btn2" id="btn2">Sign me up!</button>
</form>
Then I have the profile.php as follows:
<?php
if (isset($_POST['btn2'])) {
$usr = $_POST['form-username-2'];
$pass = $_POST['form-password-2'];
$email = $_POST['form-email'];
echo $usr;
echo $pass;
echo $email;
}
?>
As far as I tried I can't get the values echoed right on the other side, there is nothing printed
I'm trying to get the values only if I press the register button.
I tried the SERVER option but it works with both buttons, but I want it to work with the second.
Could you please help me out with this?
Thank you very much (sorry if my English is not good in advance...)
EDIT:
I provide you the Javascript code as I figured out without it it works... Please tell me whats wrong with the javascript validations...
<script>
function validateForm1()
{
var name = document.forms["form1"]["form-username"].value;
var pass = document.forms["form1"]["form-password"].value;
var format = /[!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
if (pass.length < 7){
alert("Please enter at least 7 character password");
return false;
}
if (!format.test(pass)){
alert("Please enter at a symbol in password");
return false;
}
return true;
}
</script>
<script>
function validateForm2()
{
var name = document.forms["form2"]["form-username-2"].value;
var mail = document.forms["form2"]["form-last-name"].value;
var pass1 = document.forms["form2"]["form-password-2"].value;
var pass2 = document.forms["form2"]["form-password-3"].value;
var passformat = /[!##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
var mailformat = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (pass1 != pass2){
alert("Confirmation password doesn't match");
return false;
}
if (pass1.length < 7){
alert("Please enter at least 7 character password");
return false;
}
if (!passformat.test(pass1)){
alert("Please enter at a symbol in password");
return false;
}
if (!mailformat.test(mail)){
alert("Please enter a valid email");
return false;
}
return true;
}
</script>
you just need to add double test :
if (isset($_POST['btn1'])) {
...
} elseif(isset($_POST['btn2'])) {
...
}
Try this :
$.validate({
lang: 'en',
modules : 'security'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script>
<form role="form" action="profile.php" method="post" class="login-form" name="form1" id="form1">
<div class="form-group">
<label class="sr-only" for="form-username">Username</label>
<input data-validation="length" data-validation-length="min4" type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username">
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Password</label>
<input data-validation="length" data-validation-length="min8" type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<button type="submit" class="btn" id="btn1" name="btn1">Sign in!</button>
</form>
<form role="form" action="profile.php" method="post" class="registration-form" name="form2" id="form2">
<div class="form-group">
<label class="sr-only" for="form-username-2">Username</label>
<input data-validation="length" data-validation-length="min4" type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username-2">
</div>
<div class="form-group">
<label class="sr-only" for="form-email">Email</label>
<input data-validation="email" type="text" name="form-email" placeholder="Email..." class="form-email form-control" id="form-email">
</div>
<div class="form-group">
<label class="sr-only" for="form-password-2">Password</label>
<input data-validation="confirmation length" data-validation-length="min8" type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password-2">
</div>
<div class="form-group">
<label class="sr-only" for="form-confirm-pass">Confirm Password</label>
<input type="password" name="form-password_confirmation" placeholder="Confirm Password..." class="form-password form-control" id="form-confirm-pass">
</div>
<button type="submit" class="btn" name="btn2" id="btn2">Sign me up!</button>
</form>
profile.php
<?php
if (isset($_POST['btn1'])) {
$username = $_POST['form-username'];
$password = $_POST['form-password'];
echo $username;
echo $password;
} elseif(isset($_POST['btn2'])) {
$username = $_POST['form-username'];
$email = $_POST['form-email'];
$password = $_POST['form-password'];
echo $username;
echo $email;
echo $password;
}
?>
$_POST will not contain the "submit" button value "bt1" when you submit the 2nd form, and vice-versa.
Best practice is to use a hidden field instead to determine what form you are in. For instance, use this inside the 1st form: <input type="hidden" name="which_form" value="form1"/>
and then <input type="hidden" name="which_form" value="form2"/> inside the 2nd form.
Then you can check the value of $_POST['which_form'] to determine what form was posted.

I want to add multiple images into MySQL database

I'm working on this feature in my project where I want to add three(3) images along side other details into my database row and I want this images to be in separate columns in one row.... So far below is my code. the code is not working yet... The images and texts are not uploading to the database....
Please help me out guys. What am I doing wrong. Thanks.
Below is my code sample:
//THE PHP SECTION//
<?php
session_start();
include 'includes/config.php';
if (isset($_POST['post']) && isset($_POST['itemtype'])) {
$title = mysqli_real_escape_string($link, $_POST['title']);
$itemtype = mysqli_real_escape_string($link, $_POST['itemtype']);
$description = mysqli_real_escape_string($link, $_POST['description']);
$image1 = $_FILES['image1']['name'];
$image1_tmp_name = $_FILES['image1']['tmp_name'];
$image2 = $_FILES['image2']['name'];
$image2_tmp_name = $_FILES['image2']['tmp_name'];
$image3 = $_FILES['image3']['name'];
$image3_tmp_name = $_FILES['image3']['tmp_name'];
$price = mysqli_real_escape_string($link, $_POST['price']);
$category = mysqli_real_escape_string($link, $_POST['category']);
$name = mysqli_real_escape_string($link, $_POST['name']);
//image file directory
$target1 = 'images/user_ads/'.basename($image1);
$target2 = 'images/user_ads/'.basename($image2);
$target3 = 'images/user_ads/'.basename($image3);
if (move_uploaded_file($_FILES['image1_tmp_name'], $target1)) {
}
if (move_uploaded_file($_FILES['image2_tmp_name'], $target2)) {
}
if (move_uploaded_file($_FILES['image3_tmp_name'], $target3)) {
}
//insert into ost database
$insert = "INSERT INTO products(Title,Product_Type,Description,Image1,Image2,Image3,Price,Category,Name,PostedDate)VALUES('$title','$itemtype','$description','$target1','$target2','$target3','$price','$category','$name',NOW())";
$insertKwary = mysqli_query($link, $insert);
if ($insertKwary) {
$msg = "<div class='alert alert-danger alert-success'>Product Submitted</div>";
}else{
$msg = "<div class='alert alert-danger alert-success'>Product Not Submitted...Try again</div>";
}
}
?>
//THE HTML SECTION//
<div class="col-md-8 col-md-offset-2">
<?php if(isset($msg)) { echo $msg; } ?>
<form action="" method="POST" enctype="multipart/form-data" class="postAdForm" id="postAdForm">
<div class="form-group">
<label for="Ad_title">Item Title</label>
<input type="text" name="title" class="form-control title" id="title" required=""/>
</div>
<div class="form-group">
<label for="itemtype">Item Type</label>
<select class="form-control" name="itemtype" id="itemtype">
<option>Sale</option>
<option>Request</option>
</select>
</div>
<div class="form-group">
<label for="description">Item Description</label>
<textarea name="description" class="form-control description" id="description" rows="7" required=""></textarea>
</div>
<div class="form-group">
<label for="price">First Image</label>
<input type="file" name="image1" class="form-control image1" id="image1" required="" />
</div>
<div class="form-group">
<label for="price">Second Image</label>
<input type="file" name="image2" class="form-control image2" id="image2" required="" />
</div>
<div class="form-group">
<label for="price">Third Image</label>
<input type="file" name="image3" class="form-control image3" id="image3" required="" />
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="text" name="price" class="form-control price" id="price" required="" />
</div>
<div class="form-group">
<label for="category">Item Category</label>
<select class="form-control" name="category" id="category">
<option>Sale</option>
<option>Request</option>
</select>
</div>
<div class="form-group">
<label for="price">Name</label>
<input type="text" name="name" class="form-control name" id="name" required="" readonly="" />
</div>
<div class="form-group">
<input type="submit" name="post" class="btn btn-post post" id="post" value="POST AD" />
</div>
</form>
</div>
I've also attached an image of the error i'm getting..
image of web page showing thee errors i get..error gotten
The error is due to these lines:
if (move_uploaded_file($_FILES['image1_tmp_name'], $target1)) {
}
if (move_uploaded_file($_FILES['image2_tmp_name'], $target2)) {
}
if (move_uploaded_file($_FILES['image3_tmp_name'], $target3)) {
}
You're setting $image1_tmp_name, not $_FILES['image1_tmp_name'].
Try this:
if (move_uploaded_file($image1_tmp_name, $target1)) {
}
if (move_uploaded_file($image2_tmp_name, $target2)) {
}
if (move_uploaded_file($image3_tmp_name, $target3)) {
}
Edit: Using mysqli_error() to print the error helped solve additional issues (Mentioned in comments)

php coding Form submission to server using php

I have typed this coding by using php and mysql. In If statement condition Id part is not inserted into database. But other fields are inserted into database. I couldn't find out the error. Can anyone tell what is the error?
<?php
$conn=mysqli_connect("localhost","root","database","user") or die("unable to connect");
//echo "Connected Successfully";
?>
<h2>Form Submission to Server</h2>
<?php
if(isset($_POST["submit"]))
{// get user inputs
$error="";
$resultmessage="";
$id=$_POST["id"];
$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$email=$_POST["email"];
$password=$_POST["password"];
//error messages
$missingfirstname="<p>Enter your First name</p>";
$missinglastname="<p>Enter your Last name</p>";
$missingemail="<p>Enter your Email id</p>";
$invalidmail="<p>enter your valid mail id</p>";
$missingpwd="<p>Enter your password</p>";
if(!$firstname)
{
$error.=$missingfirstname;
}
else
{
$firstname=filter_var($firstname,FILTER_SANITIZE_STRING);
}
if(!$lastname)
{
$error.=$missinglastname;
}
else
{
$lastname=filter_var($lastname,FILTER_SANITIZE_STRING);
}
if(!$email)
{
$error.=$missingemail;
}
else
{
$email=filter_var($email,FILTER_SANITIZE_EMAIL);
if(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
$error.=$invalidmail;
}
}
if(!$password)
{
$error.=$missingpwd;
}
if($error)
{
$resultmessage='<div class="alert alert-danger">'.$error.'</div>';
echo $resultmessage;
}
else
{
//no errors,prepare variables for query
$tblname="attend4";
$firstname=mysqli_real_escape_string($conn,$firstname);
$lastname=mysqli_real_escape_string($conn,$lastname);
$email=mysqli_real_escape_string($conn,$email);
$password=mysqli_real_escape_string($conn,$password);
$password=md5($password);
//execute the query
if(!$id)
{
$sql="INSERT INTO attend4(firstname,lastname,email,password)VALUES('$firstname','$lastname','$email','$password')";
}
else
{
$sql="INSERT INTO attend4(id,firstname,lastname,email,password)VALUES('$id',$firstname','$lastname','$email','$password')";
} if(mysqli_query($conn,$sql))
{
$resultmessage="Data added successfully";
echo $resultmessage;
}
else
{
$resultmessage="unable to add";
echo $resultmessage;
}
}
}
?>
<form action="33.populateusigform.php" method="post">
<div class="form-group">
<label for="number">Number</label>
<input type="number" name="id" id="id" class="form-control" maxlength="4" placeholder="Enter your Number">
</div>
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname" maxlength="30" class="form-control" placeholder="Enter your Firstname">
</div>
<div class="form-group">
<label for="lastname">Lastname</label>
<input type="text" name="lastname" id="lastname" maxlength="30" placeholder="Enter your lastname" class="form-control">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" id="email" maxlength="30" placeholder="Enter your Email Id"class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" maxlength="30" placeholder="Enter your Password" class="form-control">
</div>
<input type="submit" name="submit" class="btn btn-success center-block" value="Send data">
</form>
I suspect your issue is with the if($id) which may act strangely if given 0, triggering the sql query without the id term.
I would use if(empty($id)) instead it will be more reliable.
Too many problems...
1) you have kept your action "33.populateusigform.php" for form but are inserting data from same file....
2) have you kept your id to auto increment?
3) what is use to save id in your case, it will automatically be saved if you use as primary key in db.
Need to check your db schema for further assistance.

Categories