I have a form to inject data in a database. When user clicks submit button this error shows up in a <input type=text>field. I have been looking around for a solution for hours but I do not know how to fix this.
Warning: htmlentities() expects parameter 1 to be
string, array given in
C:rr\xampp\htdocs\ecommerce\helpers\helpers.php on line
11
helpers.php
<?php
function display_errors($errors){
$display = ' <ul class="bg-danger">';
foreach($errors as $error){
$display .='<li class="text-danger">'.$error. '</li> ';
}
$display .='</ul>';
return $display;
}
function sanitize ($dirty){
return htmlentities($dirty, ENT_QUOTES, "UTF-8");
}
function money($number){
return '$ '.number_format($number,2);
}
form.php
<form action="products.php?add=1" method="POST" enctype="multipart/form-data">
<div class='container_12'>
<div class="form-group col-md-3">
<label for="prod_name">Product Name*:</label>
<input type="text" name="prod_name" id="prod_name" class="form-control" value="<?=((isset($_POST['prod_name']))?sanitize($_POST):' ');?>">
</div>
<div class="form-group col-md-3">
<label for="parent">Parent Category*:</label>
<select class="form-control" id="parent" name="parent">
<option value=""<?=((isset($_POST['parent']) && $_POST['parent'] == '')?'selected':'');?>></option>
<?php while($parent = mysqli_fetch_assoc($parentQuery)): ?>
<option value=" <?=$parent['id'];?>"<?=((isset($_POST['parent']) && $_POST['parent'] == $parent['id'])?' select':'');?>><?=$parent['category_name'];?></option>
<?php endwhile; ?>
</select>
</div>
<div class='form-group col-md-3'>
<label for='child'>Second Category*:</label>
<select id='child' name='child' class='form-control'></select>
</div>
</div>
<div class='container_12'>
<div class='form-group col-md-3'>
<label for='list_price'>List Price(OPTIONAL): </label>
<input type="text" id="list_price" name="list_price" class="form-control" value="<?=((isset($_POST['list_price']))?sanitize($_POST['list_price']):'');?>">
</div>
<div class="form-group col-md-3">
<label for="price">Price*:</label>
<input type="text" id="price" name="price" class="form-control" value="<?=((isset($_POST['price']))?sanitize($_POST['price']):'');?>">
</div>
<div class='form-group col-md-3'>
<label for='prod_width'>Width* (in inches):</label>
<input type="text" id="prod_width" name="prod_width" class="form-control" value="<?=((isset($_POST['prod_width']))?sanitize($_POST['prod_width']):'');?>">
</div>
<div class='form-group col-md-3'>
<label for='prod_depth'>Height*(in inches):</label>
<input type="text" id="'prod_depth" name="'prod_depth" class="form-control" value="<?=((isset($_POST['prod_depth']))?sanitize($_POST['prod_depth']):'');?>">
</div>
</div>
<div class='container_12'>
<div class='form-group col-md-3'>
<label for='prod_height'>Depth*(in inches):</label>
<input type="text" id="prod_height" name="prod_height" class="form-control" value="<?=((isset($_POST['prod_height']))?sanitize($_POST['prod_height']):'');?>">
</div>
<div class='form-group col-md-3'>
<label for='prod_material'>Construction Material:</label>
<input type="text" id="prod_material" name="prod_material" class="form-control" value="<?=((isset($_POST['prod_material']))?sanitize($_POST['prod_material']):'');?>">
</div>
<div class='form-group col-md-6'>
<label>Quantity * :</label>
<input type="text" id="quantity" name="quantity" class="form-control" value="<?=((isset($_POST['quantity']))?sanitize($_POST['quantity']):'');?>">
</div>
</div>
<div class='container_12'>
<div class="form-group col-md-3"> <label for="image_1">Product Photo #1:</label>
<input type="file" name="image_1" id="image_1" class="form-control">
</div>
<div class="form-group col-md-3"> <label for="image_2">Product Photo #2:</label>
<input type="file" name="image_2" id="image_2" class="form-control">
</div>
<div class="form-group col-md-3"> <label for="image_3">Product Photo #3:</label>
<input type="file" name="image_3" id="image_3" class="form-control">
</div>
<div class="form-group col-md-3"> <label for="image_4">Product Photo#4:</label>
<input type="file" name="image_4" id="image_4" class="form-control">
</div>
</div>
<div class='container_12'>
<div class="form-group col-md-6">
<label for="description">Description:</label>
<textarea id="description" name="description" class="form-control" rows="6"><?=((isset($_POST['description']))?sanitize($_POST['description']):'');?></textarea>
</div>
<div class="form-group col-md-6">
<label for="care_instructions">Care Instructions*:</label>
<textarea id="care_instructions" name="care_instructions" class="form-control" rows="6"><?=((isset($_POST['care_instructions']))?sanitize($_POST['care_instructions']):'');?></textarea>
</div></div>
<div class='container_12'>
<div class="form-group pull-right">
<input type='submit' value='Add Product' class='form-control btn-success pull-right'>
</div></div>
</form>
You are passing the whole $_POST variable to serialize in the product name input (that's why the error says 'array given').
Check this line:
<input type="text" name="prod_name" id="prod_name" class="form-control" value="<?=((isset($_POST['prod_name']))?sanitize($_POST):' ');?>">
And change it with this:
<input type="text" name="prod_name" id="prod_name" class="form-control" value="<?=((isset($_POST['prod_name']))?sanitize($_POST['prod_name']):' ');?>">
Related
I am a beginner in web development, I used have an html elements, like textbox and other type of elements, I want to use them as my object and when they have a value and click a button, it will save the value of all the elements in mysql.
I have a code like this, but it cannot be inserted and anything does not happen when clicking a button.
Please help.
PHP:
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'ytp');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
if(!$db ) {
die('Could not connect: ' . mysqli_error());
}
$fname = "";
$mname = "";
$lname = "";
$funame = "";
$cnum = "";
$bday = "";
$age = "";
$add = "";
if (isset($_POST['submit'])){
$fname = $_POST['fName'];
$mname = $_POST['mName'];
$lname = $_POST['lName'];
$funame = $_POST['fuName'];
$cnum = $_POST['Cnumber'];
$bday = $_POST['bday'];
$age = $_POST['age'];
$add = $_POST["address"];
}
$sql = "INSERT INTO employee ".
"(fName,mName,lName,fuName,cNumber,bDay,Age,Address) ".
"VALUES ('$fname','$mname','$lname','$funame','$cnum','$bday','$age','$add' )";
if (! mysqli_query($db , $sql)){
echo 'Cannot Insert';
}
else{
echo 'Success';
}
?>
HTML:
<div class="content">
<div class="row">
<div class="col-md-10">
<div class="card">
<div class="card-header">
<h5 class="title">Add User Information</h5>
</div>
<div class="card-body">
<form method="POST" action="php_functions\saveEmployee.php" name="INSERT">
<div class="row">
<div class="col-md-5 pr-md-1">
<div class="form-group">
<label>Company (disabled)</label>
<input type="text" class="form-control" disabled="" placeholder="Company" value="Benchmark Valuer's Inc.">
</div>
</div>
<div class="col-md-3 px-md-1">
<div class="form-group">
<label>ID</label>
<input type="text" class="form-control" placeholder="User ID" value="" id="id" name ="id" disabled>
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" placeholder="s.sample#gmail.com" id="email" name ="email">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" placeholder="First Name" id="fName" name ="fName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Middle Name</label>
<input type="text" class="form-control" placeholder="Middle Name" id="mName" name ="mName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" placeholder="Last Name" id="lName" name ="lName">
</div>
</div>
<div class="col-md-4 pl-md-1" hidden>
<div class="form-group">
<label>Fullname</label>
<input type="text" class="form-control" placeholder="Full Name" id="fuName" name ="fuName">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Contact Number</label>
<input type="tel" class="form-control" placeholder="Contact Number" id="Cnumber" name="Cnumber">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Age</label>
<input type="number" class="form-control" placeholder="Age" id="age" name="age">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Birthday</label>
<input type="date" class="form-control" placeholder="Birthday" id="bday" name="bday">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" placeholder="Home Address" id="address" name ="address">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" placeholder="Username" id="uName">
</div>
</div>
<div class="col-md-4 px-md-1">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" placeholder="Password" id="pWord">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>UserType</label>
<input type="number" class="form-control" placeholder="0" id="uType">
</div>
</div>
</div>
<div class="row" hidden>
<div class="col-md-12">
<div class="form-group">
<label>Image Path:</label>
<input type="text" class="form-control" placeholder="C:\\" id="imageURL">
</div>
</div>
</div>
</form>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-fill btn-primary" id="submit" name="submit">Save</button>
<button class="btn btn-fill btn-success" id="btnBrowse">Browse</button>
<button class="btn btn-fill btn-danger" id="btnCancel">Cancel</button>
</div>
</div>
</div>
</div>
</div>
Your HTML form is closing before submit button. You should close that after submit button and also need to manage hierarchy of opening form tag as below:
<form method="POST" action="php_functions\saveEmployee.php" name="INSERT">
<div class="card-body">
<div class="row">
<div class="col-md-5 pr-md-1">
<div class="form-group">
<label>Company (disabled)</label>
<input type="text" class="form-control" disabled="" placeholder="Company" value="Benchmark Valuer's Inc.">
</div>
</div>
<div class="col-md-3 px-md-1">
<div class="form-group">
<label>ID</label>
<input type="text" class="form-control" placeholder="User ID" value="" id="id" name ="id" disabled>
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" placeholder="s.sample#gmail.com" id="email" name ="email">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" placeholder="First Name" id="fName" name ="fName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Middle Name</label>
<input type="text" class="form-control" placeholder="Middle Name" id="mName" name ="mName">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" placeholder="Last Name" id="lName" name ="lName">
</div>
</div>
<div class="col-md-4 pl-md-1" hidden>
<div class="form-group">
<label>Fullname</label>
<input type="text" class="form-control" placeholder="Full Name" id="fuName" name ="fuName">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Contact Number</label>
<input type="tel" class="form-control" placeholder="Contact Number" id="Cnumber" name="Cnumber">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Age</label>
<input type="number" class="form-control" placeholder="Age" id="age" name="age">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>Birthday</label>
<input type="date" class="form-control" placeholder="Birthday" id="bday" name="bday">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" placeholder="Home Address" id="address" name ="address">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 pr-md-1">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" placeholder="Username" id="uName">
</div>
</div>
<div class="col-md-4 px-md-1">
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" placeholder="Password" id="pWord">
</div>
</div>
<div class="col-md-4 pl-md-1">
<div class="form-group">
<label>UserType</label>
<input type="number" class="form-control" placeholder="0" id="uType">
</div>
</div>
</div>
<div class="row" hidden>
<div class="col-md-12">
<div class="form-group">
<label>Image Path:</label>
<input type="text" class="form-control" placeholder="C:\\" id="imageURL">
</div>
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-fill btn-primary" id="submit" name="submit">Save</button>
<button class="btn btn-fill btn-success" id="btnBrowse">Browse</button>
<button class="btn btn-fill btn-danger" id="btnCancel">Cancel</button>
</div>
</form>
Hope it helps you.
the form has been closed before the submit button.
the </form> tag should be placed after the <div class="card-footer">...</div>.
please try it. hope it will help.
I am trying to create a short code for html form. because I want to use this multiple time in my word-press page.
I have no idea how to do this . if any help please let help me
<form action="<?php echo get_page_link(2599) ?>" method="POST" >
<input type='hidden' name='page_id' value='2599'>
<div class="row">
<div class="form-group col-sm-3 col-md-2" id="bgform_text2">
<h4>SEARCH</h4>
<p>For Your Favourite Place</p>
</div>
<div class="form-group col-sm-5 col-md-2">
<label>Where to ? </label>
<input type="text" name="names" class="input-text full-width" placeholder="start typing here....">
</div>
<div class="form-group col-sm-5 col-md-4">
<div class="row">
<div class="col-xs-6">
<label>Arrive </label>
<div class="controls">
<input type="text" name="arive_time" id="departing">
</div>
</div>
<div class="col-xs-6">
<label>Departs </label>
<div class="controls">
<input type="text" name="depart_time" id="returning">
</div>
</div>
</div>
</div>
<div class="form-group col-sm-9 col-md-2">
<label>Sleeps </label>
<div class="controls">
<i class="fa fa-sort"></i>
<select name="sleeps">
<option value="" disabled="" selected=""></option>
<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></option>
</select>
</div>
</div>
<div class="form-group col-sm-3 col-md-2">
<input type="submit" value="submit" name="submit_btn" class="button">
</div>
</div>
</form>
Try this.
<?php
function my_shortcode(){
$pagelink = get_page_link(2599);
return '<form action="'.$pagelink.'" method="POST" >
<input type="hidden" name="page_id" value="2599">
<div class="row">
<div class="form-group col-sm-3 col-md-2" id="bgform_text2">
<h4>SEARCH</h4>
<p>For Your Favourite Place</p>
</div>
<div class="form-group col-sm-5 col-md-2">
<label>Where to ? </label>
<input type="text" name="names" class="input-text full-width" placeholder="start typing here....">
</div>
<div class="form-group col-sm-5 col-md-4">
<div class="row">
<div class="col-xs-6">
<label>Arrive </label>
<div class="controls">
<input type="text" name="arive_time" id="departing">
</div>
</div>
<div class="col-xs-6">
<label>Departs </label>
<div class="controls">
<input type="text" name="depart_time" id="returning">
</div>
</div>
</div>
</div>
<div class="form-group col-sm-9 col-md-2">
<label>Sleeps </label>
<div class="controls">
<i class="fa fa-sort"></i>
<select name="sleeps">
<option value="" disabled="" selected=""></option>
<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></option>
</select>
</div>
</div>
<div class="form-group col-sm-3 col-md-2">
<input type="submit" value="submit" name="submit_btn" class="button">
</div>
</div>
</form>';
}
add_shortcode('my_shortcode_name','my_shortcode');
?>
you can use this shortcode in your page
<?php echo do_shortcode('[my_shortcode_name]');?>
I think you are saying that you do not want to copy-paste the exact same code on multiple places inside your project, PHP has an include control structure method to take any file and use the contents on run-time.
Your code could eventually look something like this:
# form definition inside the main page that needs the form
<form action="<?php echo get_page_link(2599) ?>" method="POST" >
# include the template
<?php include('template.php') ?>
</form>
# template.php contains the html form
<input type='hidden' name='page_id' value='2599'>
How i can checked radio button inside echo method ?
<?php
if(isset($_GET['sid'])){
echo'
<div class="right_col" role="main">';
$a=$_GET['sid'];
$update = $obj->selectdata($a);
foreach($update as $upd)
{
$upd['sname'];
echo'
<h1 class="text-center">update student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php?sid='.$upd['id'].'" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" value="'.$upd['sname'].'" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" value="'.$upd['sfname'].'" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" value="'.$upd['email'].'" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" placeholder="Change Password" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" value="'.$upd['dob'].'" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
<label><input type="radio" name="sgender" if($s_gender == Male){echo checked=checked} value="Male" />Male</label>
</div>
<div class="radio-inline">
<label><input type="radio" name="sgender" if($s_gender == Female){echo checked=checked} value ="Female" />Female</label>
</div></div>
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" value="'.$upd['contact'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" value="'.$upd['address'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="upt_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
}
else {
echo'
<div class="right_col" role="main">
<h1 class="text-center">add student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
<label><input type="radio" name="sgender" value="Male" required/>Male</label>
</div>
<div class="radio-inline">
<label><input type="radio" name="sgender" value ="Female"required />Female</label>
</div></div>
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="stu_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
?>
Work like this:
<input type="radio" '.($s_gender == Female ? 'checked="checked"' : "").' name="sgender" value ="Female" />
<?php
if(isset($_GET['sid'])){
echo'
<div class="right_col" role="main">';
$a=$_GET['sid'];
$update = $obj->selectdata($a);
foreach($update as $upd)
{
$upd['sname'];
echo'
<h1 class="text-center">update student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php?sid='.$upd['id'].'" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" value="'.$upd['sname'].'" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" value="'.$upd['sfname'].'" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" value="'.$upd['email'].'" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" placeholder="Change Password" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" value="'.$upd['dob'].'" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
if($upd['gender']== "Male") {
echo '
<label><input cheacked= "cheacked" type="radio" name="sgender" value="Male" />Male</label>
</div>';
}
else {
echo' <div class="radio-inline">
<label><input type="radio" cheacked= "cheacked" name="sgender" value ="Female" />Female</label>
</div></div>';
}
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" value="'.$upd['contact'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" value="'.$upd['address'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="upt_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
}
else {
echo'
<div class="right_col" role="main">
<h1 class="text-center">add student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
<label><input type="radio" name="sgender" value="Male" required/>Male</label>
</div>
<div class="radio-inline">
<label><input type="radio" name="sgender" value ="Female"required />Female</label>
</div></div>
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="stu_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
?>
use this code then let me know if it resolved your issue, you had to put if and else with your problem in proper way you used wrong variable for get gender value.
I have this error in controller
A PHP Error was encountered
Severity: Notice
Message: Undefined property: dashboardController::$upload
Filename: controllers/dashboardController.php
Line Number: 70
Call to a member function data() on a non-object in
C:\xampp\htdocs\High_tack\application\controllers\dashboardController.php
on line 71
just do as i done it in my project using wamp and codeigniter. I uploaded my pictures directly into the folder named uploads in the project folder.
here is my controller named addproduct,
<?php class Addproduct extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
$this->load->view('addproduct');
}
function save() {
$name_file = $_FILES['pro_filename']['name'];
//$user=$this->session->userdata['logged_in'];
$data=array (
'pro_name'=>$this->input->post('pro_name'),
'pro_code'=>$this->input->post('pro_code'),
'hsn_code'=>$this->input->post('hsn_code'),
'pro_price'=>$this->input->post('pro_price'),
'pro_tax1'=>$this->input->post('pro_tax1'),
'pro_tax2'=>$this->input->post('pro_tax2'),
'pro_tax3'=>$this->input->post('pro_tax3'),
'pro_tax4'=>$this->input->post('pro_tax4'),
'pro_description'=>$this->input->post('pro_description'),
'pro_brand'=>$this->input->post('pro_brand'),
'pro_category'=>$this->input->post('pro_category'),
'pro_scategory'=>$this->input->post('pro_scategory'),
'pro_sscategory'=>$this->input->post('pro_sscategory'),
'pro_qauntity'=>$this->input->post('pro_qauntity'),
'pro_service'=>$this->input->post('pro_service'),
'pro_certificate'=>$this->input->post('pro_certificate'),
'pro_filename'=>$name_file,
'pro_date'=>$this->input->post('pro_date')
);
$this->load->model('base_model');
$this->base_model->save('addproduct',$data);
$this->do_upload();
$this->load->view('addproduct');
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|gif|jpeg';
$config['max_size'] = 500000;
$config['max_width'] = 200000;
$config['max_height'] = 200000;
$this->load->library('upload', $config);
$this->upload->do_upload('pro_filename');
}
}
here is my view page named addproduct
<div class="panel-body">
<div class="form">
<div class="col-md-12 sign-up text-left">
<div class="form-group ">
<label for="pro_name" class="control-label col-lg-3">Product Name</label>
<div class="col-lg-6">
<input class="form-control " id="pro_name" name="pro_name" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_code" class="control-label col-lg-3">Product Code</label>
<div class="col-lg-6">
<input class="form-control " id="pro_code" name="pro_code" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="hsn_code" class="control-label col-lg-3">HSN Code</label>
<div class="col-lg-6">
<input class="form-control " id="hsn_code" name="hsn_code" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_price" class="control-label col-lg-3">Prize</label>
<div class="col-lg-6">
<input class="form-control " id="pro_price" name="pro_price" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax1" class="control-label col-lg-3">Tax 1</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax1" name="pro_tax1" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax2" class="control-label col-lg-3">Tax 2</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax2" name="pro_tax2" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax3" class="control-label col-lg-3">Tax 3</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax3" name="pro_tax3" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax4" class="control-label col-lg-3">Tax 4</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax4" name="pro_tax4" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_description" class="control-label col-lg-3">Description</label>
<div class="col-lg-6">
<input class="form-control " id="pro_description" name="pro_description" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_brand" class="control-label col-lg-3">Brand</label>
<div class="col-lg-6">
<input class="form-control " id="pro_brand" name="pro_brand" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_category" class="control-label col-lg-3">Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_category" name="pro_category" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_scategory" class="control-label col-lg-3">Sub Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_scategory" name="pro_scategory" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_sscategory" class="control-label col-lg-3">S-Sub Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_sscategory" name="pro_sscategory" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_quantity" class="control-label col-lg-3">Qauntity</label>
<div class="col-lg-6">
<input class="form-control " id="pro_quantity" name="pro_quantity" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_service" class="control-label col-lg-3">Service Alert</label>
<div class="col-lg-6">
<input class="form-control " id="pro_service" name="pro_service" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_certificate" class="control-label col-lg-3">Certificate Alert</label>
<div class="col-lg-6">
<input class="form-control " id="pro_certificate" name="pro_certificate" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_filename" class="control-label col-lg-3">Photo:</label>
<div class="col-lg-6">
<input class="form-control" type="file" name="pro_filename" required/>
</div>
</div><br><br>
<div class="clearfix"></div>
<?php
$today = date("Y-m-d");
?>
<input type="hidden" name="pro_date" value="<?php echo $today ?>">
<div class="form-group">
<div class="col-lg-offset-3 col-lg-6">
<button class="btn btn-primary" type="submit">Save</button>
<button class="btn btn-default" >Cancel</button>
</div>
</div>
<br>
</div>
<br>
</div>
</div>
this is my code in model named base_model
public function save($table, $data) {
return $this->db->insert($table, $data);
}
delete the portion you dont want, if you dont need to upload the image to folder simply change the controller field where the upload occurs
'pro_filename'=>$this->input->post('pro_filename'),
I work on from to add more than one device when user click on add more
But How can I group multiple fields with same name as array for every device from this form, my server side language is PHP.
<form>
<div class="row">
<div class="col-md-12">
<div class="widget stacked">
<div class="widget-header">
<i class="icon-hdd"></i>
<h3>Remove Devices</h3> <a class="btn label label-success add_new_device">Add more</a>
</div>
<div class="widget-content">
<div class="form-group">
<label for="device_name">Device Name</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[device_name][]" type="text">
</div>
<div class="form-group">
<label for="device_description">Device Description</label>
<input class="form-control" data-validation="length" data-validation-length="max255" data-validation-optional="true" name="device[device_description][]" type="text">
</div>
<div class="form-group">
<label for="device_url">Device Url</label>
<input class="form-control" data-validation="url" data-validation-optional="true" data-validation-help="Ex: http://000.000.000.000/index.cgi" name="device[device_url][]" type="url">
</div>
<div class="form-group">
<label for="device_ip4">IP4</label>
<input class="form-control" data-validation="length" data-validation-length="max15" data-validation-optional="true" data-validation-help="Ex: 000.000.000.000" name="device[device_ip4][]" type="text">
</div>
<div class="form-group">
<label for="device_ip6">IP6</label>
<input class="form-control" data-validation="length" data-validation-length="max45" data-validation-help="Ex: 0000:0000:0000:0000:0000:0000" name="device[][device_ip6]" type="text">
</div>
<div class="form-group">
<label for="device_username">Device Username</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_username]" type="text">
</div>
<div class="form-group">
<label for="device_password">Device Password</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_password]" type="text">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="widget stacked">
<div class="widget-header">
<i class="icon-hdd"></i>
<h3>Remove Devices</h3> <a class="btn label label-success add_new_device">Add more</a>
</div>
<div class="widget-content">
<div class="form-group">
<label for="device_name">Device Name</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[device_name][]" type="text">
</div>
<div class="form-group">
<label for="device_description">Device Description</label>
<input class="form-control" data-validation="length" data-validation-length="max255" data-validation-optional="true" name="device[device_description][]" type="text">
</div>
<div class="form-group">
<label for="device_url">Device Url</label>
<input class="form-control" data-validation="url" data-validation-optional="true" data-validation-help="Ex: http://000.000.000.000/index.cgi" name="device[device_url][]" type="url">
</div>
<div class="form-group">
<label for="device_ip4">IP4</label>
<input class="form-control" data-validation="length" data-validation-length="max15" data-validation-optional="true" data-validation-help="Ex: 000.000.000.000" name="device[device_ip4][]" type="text">
</div>
<div class="form-group">
<label for="device_ip6">IP6</label>
<input class="form-control" data-validation="length" data-validation-length="max45" data-validation-help="Ex: 0000:0000:0000:0000:0000:0000" name="device[][device_ip6]" type="text">
</div>
<div class="form-group">
<label for="device_username">Device Username</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_username]" type="text">
</div>
<div class="form-group">
<label for="device_password">Device Password</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_password]" type="text">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="widget stacked">
<div class="widget-header">
<i class="icon-hdd"></i>
<h3>Remove Devices</h3> <a class="btn label label-success add_new_device">Add more</a>
</div>
<div class="widget-content">
<div class="form-group">
<label for="device_name">Device Name</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[device_name][]" type="text">
</div>
<div class="form-group">
<label for="device_description">Device Description</label>
<input class="form-control" data-validation="length" data-validation-length="max255" data-validation-optional="true" name="device[device_description][]" type="text">
</div>
<div class="form-group">
<label for="device_url">Device Url</label>
<input class="form-control" data-validation="url" data-validation-optional="true" data-validation-help="Ex: http://000.000.000.000/index.cgi" name="device[device_url][]" type="url">
</div>
<div class="form-group">
<label for="device_ip4">IP4</label>
<input class="form-control" data-validation="length" data-validation-length="max15" data-validation-optional="true" data-validation-help="Ex: 000.000.000.000" name="device[device_ip4][]" type="text">
</div>
<div class="form-group">
<label for="device_ip6">IP6</label>
<input class="form-control" data-validation="length" data-validation-length="max45" data-validation-help="Ex: 0000:0000:0000:0000:0000:0000" name="device[][device_ip6]" type="text">
</div>
<div class="form-group">
<label for="device_username">Device Username</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_username]" type="text">
</div>
<div class="form-group">
<label for="device_password">Device Password</label>
<input class="form-control" data-validation="length" data-validation-length="max128" data-validation-optional="true" name="device[][device_password]" type="text">
</div>
</div>
</div>
</div>
</div>
</form>
Name your fields like devices[0][description], devices[1][description] and so on.
If every object has the exact same number of fields and every one is a new record - you can use devices[][description] and browser will fill indexes for you, but this can lead to bugs in the future, if you decide to use same form for editing records.
Also see How to get form input array into PHP array