Adding Multiple Rows to Database Upon User Input - php

I am trying to work out how to add multiple rows in an 'Order Details' table when a form is submitted. The user is able to select multiple sizes which when submitted, should generate up to N number of rows selected by the user.
Below is my current code for the View file:
<form action="../Controller/NewProduct.php" method="POST">
<h4 align="center"> PRODUCT <b>DETAILS</b> </h4>
<br/>
<div class="form-group">
<label for="categoryid">Category</label>
<select class="form-control" name="categoryid" id="categoryid" required>
<option value="1">Tshirt</option>
<option value="2">Swim Shorts</option>
</select>
</div>
<br/>
<div class="form-group">
<label for="productid">Product ID</label>
<input name="productid" class="form-control" id="productid" required>
</div>
<br/>
<div class="form-group">
<label for="name">Product Name</label>
<input name="name" class="form-control" id="name" required>
</div>
<br/>
<div class="form-group">
<label for="description">Product Description</label><br/>
<textarea name="description" rows="4" class="description" placeholder="Enter product description here." required></textarea>
</div>
<br/>
<div class="form-group">
<label for="size">Size</label>
<select class="form-control" id="size" name="size" multiple required>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
<option value="XXL">XXL</option>
</select>
</div>
<br/>
<div class="form-group">
<label for="colour">Colour</label>
<select class="form-control" id="colour" name="colour" required>
<option value="Burgundy">Burgundy</option>
<option value="Navy Blue">Navy Blue</option>
<option value="Red">Red</option>
<option value="White">White</option>
<option value="Grey">Grey</option>
<option value="Black">Black</option>
<option value="Yellow">Yellow</option>
<option value="Green">Green</option>
<option value="Khaki">Khaki</option>
<option value="Blue">Blue</option>
</select>
</div>
<br/>
<div class="form-group">
<label for="unitprice">Unit Price</label>
<input name="unitprice" class="form-control" id="unitprice" placeholder="Do not include the £ sign." required>
</div>
<br/>
<div class="form-group">
<label for="unitsinstock">Units In Stock</label>
<input name="unitsinstock" class="form-control" id="unitsinstock" required>
</div>
<br/>
<div class="form-group">
<label for="pimage">Product Image</label>
<input name="pimage" class="form-control" id="pimage" placeholder="Enter Image URL" required>
</div>
<br/>
<div class="form-group">
<label for="discount">Discount</label>
<input name="discount" class="form-control" id="discount">
</div>
<br/>
<button type="submit" name="submit" class="btn"><i class="fa fa-check"></i> Done</button>
</form>
This this then the file which is run when the form is submitted:
if (isset($_POST['submit'])) {
require_once 'DatabaseConnection.php';
$statement = $pdo->prepare("INSERT INTO Product (categoryid, name, description, colour, unitprice, pimage, discount, productid)
VALUES (:categoryid, :name, :description, :colour, :unitprice, :pimage, :discount, :productid)");
$statement->bindParam(':categoryid', $categoryid);
$statement->bindParam(':name', $name);
$statement->bindParam(':description', $description);
$statement->bindParam(':colour', $colour);
$statement->bindParam(':unitprice', $unitprice);
$statement->bindParam(':pimage', $pimage);
$statement->bindParam(':discount', $discount);
$statement->bindParam(':productid', $productid);
$categoryid = $_POST['categoryid'];
$name = $_POST['name'];
$description = $_POST['description'];
$colour = $_POST['colour'];
$unitprice = $_POST['unitprice'];
$pimage = $_POST['pimage'];
$discount = $_POST['discount'];
$statement2 = $pdo->prepare("INSERT INTO ProductDetails (unitsinstock, size, productid) VALUES (:unitsinstock, :size, :productid)");
$statement2->bindParam(':size', $size);
$statement2->bindParam(':unitsinstock', $unitsinstock);
$statement2->bindParam(':productid', $productid);
$size = $_POST['size'];
$unitsinstock = $_POST['unitsinstock'];
$productid = $_POST['productid'];
$statement->execute();
$statement2->execute();
header('location:../View/AdminAllProducts.php');
}
?>
An example of what I am trying to achieve is like the following: https://imgur.com/a/SPr14

Pick up the sizes array and work on it in a loop:
$sizes = $_POST['size[]'];
foreach ( $sizes as $size )
{
// ... your code of statement2 :
statement2 = $pdo->prepare("INSERT INTO ProductDetails (unitsinstock, size, productid) VALUES (:unitsinstock, :size, :productid)");
$statement2->bindParam(':size', $size);
$statement2->bindParam(':unitsinstock', $unitsinstock);
$statement2->bindParam(':productid', $productid);
$unitsinstock = $_POST['unitsinstock'];
$productid = $_POST['productid'];
$statement->execute();
$statement2->execute();
}

You should change the selects that can be multiple select to be an array (ex: size([]) the in PHP you should work the request value as an array.
You can also concatenat the string in the values to be inserted in order to only do one request to the database.

Related

Sending and email with the auto Increment number attached to the email using PDO/MySQL

Hello my favorite people!
I am trying to send an email after submitting a form, with the AUTO INCREMENT number attached to the email because the AUTO INCREMENT number is the clients Job Card Reference Number. So far i have successfully created the insert script which inserts the data into the database perfectly, and also sends the email too. But does not attach the AUTO INCREMENT number into the email. The INT(11) AUTO INCREMENT primary key is "job_number" in my MySQL database.
Here is my insert page:
<form action="addnewrepairprocess.php" method="POST">
<div class="form-group">
<label for="date">Date</label>
<input type="date" name="date" id="date" class="form-control" placeholder="Job Card Date">
</div>
<div class="form-group">
<label for="client_full_name">Client Full Name</label>
<input type="text" name="client_full_name" class="form-control" id="client_full_name" placeholder="Mr. Laptop Man">
</div>
<div class="form-group">
<label for="client_email">Client Email Address</label>
<input type="email" name="client_email" class="form-control" id="client_email" placeholder="example#live.co.za">
</div>
<div class="form-group">
<label for="client_phone">Client Phone Number</label>
<input type="text" name="client_phone" class="form-control" id="client_phone" placeholder="071 984 5522">
</div>
<div class="form-group">
<label for="item_for_repair">Item For Repair</label>
<select name="item_for_repair" id="item_for_repair">
<option value="Laptop">Laptop</option>
<option value="Desktop">Desktop</option>
<option value="Television">Television</option>
<option value="Washing Machine">Washing Machine</option>
<option value="Tumble Dryer">Tumble Dryer</option>
<option value="Dishwasher">Dishwasher</option>
<option value="Microwave">Microwave</option>
<option value="Fridge">Fridge</option>
<option value="Printer">Printer</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-group">
<label for="repair_description">Repair Description</label>
<input type="text" name="repair_description" class="form-control" id="repair_description" placeholder="Laptop is dead...">
</div>
<div class="form-group">
<label for="hardware_details">Hardware Details</label>
<input type="text" name="hardware_details" class="form-control" id="hardware_details" placeholder="Black Lenovo Laptop with Charger">
</div>
<div class="form-group">
<label for="diagnostic_fee">Diagnostic Fee</label>
<input type="text" name="diagnostic_fee" class="form-control" id="diagnostic_fee">
</div>
<div class="form-group">
<label for="tech_assigned">Technician Assigned</label>
<select name="tech_assigned" id="tech_assigned">
<option value="Not Assigned Yet">Not Assigned Yet</option>
<option value="Brendon">Brendon</option>
<option value="Gabriel">Gabriel</option>
<option value="Tapiwa">Tapiwa</option>
<option value="Conrad">Conrad</option>
</select>
</div>
<div class="form-group">
<label for="current_status">Current Status</label>
<select name="current_status" id="current_status">
<option value="Pending">Pending</option>
<option value="In Progress">In Progress</option>
<option value="On Hold Spares Required">On Hold Spares Required</option>
<option value="On Hold Other Fault">On Hold Other Fault</option>
<option value="Repair Completed">Repair Completed</option>
</select>
</div>
<div class="form-group">
<label for="technician_notes">Technician Notes</label>
<input type="text" name="technician_notes" class="form-control" id="technician_notes">
</div>
<div class="form-group">
<label for="admin_notes">Admin Notes</label>
<input type="text" name="admin_notes" class="form-control" id="admin_notes">
</div>
<div class="form-group">
<label for="invoice_status">Invoice Status</label>
<select name="invoice_status" id="invoice_status">
<option value="Client Not Yet Invoiced">Client Not Yet Invoiced</option>
<option value="Client Invoiced">Client Invoiced</option>
</select>
</div>
<div class="form-group">
<label for="invoice_number">Invoice Number</label>
<input type="text" name="invoice_number" class="form-control" id="invoice_number">
</div>
<input type="submit" id="btn_create" name="btn_create" class="btn btn-primary" value="Create Job Card">
</form>
My Form Action Page:
<?php
require_once "connection.php";
if(isset($_REQUEST['btn_create']))
{
$job_number = $_REQUEST['job_number'];
$date = $_REQUEST['date'];
$client_full_name = $_REQUEST['client_full_name'];
$client_email = $_REQUEST['client_email'];
$client_phone = $_REQUEST['client_phone'];
$item_for_repair = $_REQUEST['item_for_repair'];
$repair_description = $_REQUEST['repair_description'];
$hardware_details = $_REQUEST['hardware_details'];
$diagnostic_fee = $_REQUEST['diagnostic_fee'];
$tech_assigned = $_REQUEST['tech_assigned'];
$current_status = $_REQUEST['current_status'];
$technician_notes = $_REQUEST['technician_notes'];
$admin_notes = $_REQUEST['admin_notes'];
$invoice_status = $_REQUEST['invoice_status'];
$invoice_number = $_REQUEST['invoice_number'];
if(empty($date)){
$errorMsg="Please Enter date";
}
else if(empty($client_email)){
$errorMsg="Please Enter Email Address";
}
else
{
try
{
if(!isset($errorMsg))
{
$insert_stmt=$db->prepare('INSERT INTO repairs(job_number,date,client_full_name,client_email,client_phone,item_for_repair,repair_description,hardware_details,diagnostic_fee,tech_assigned,current_status,technician_notes,admin_notes,invoice_status,invoice_number) VALUES(:job_number,:date,:client_full_name,:client_email,:client_phone,:item_for_repair,:repair_description,:hardware_details,:diagnostic_fee,:tech_assigned,:current_status,:technician_notes,:admin_notes,:invoice_status,:invoice_number)');
$insert_stmt->bindParam(':job_number', $job_number);
$insert_stmt->bindParam(':date', $date);
$insert_stmt->bindParam(':client_full_name', $client_full_name);
$insert_stmt->bindParam(':client_email', $client_email);
$insert_stmt->bindParam(':client_phone', $client_phone);
$insert_stmt->bindParam(':item_for_repair', $item_for_repair);
$insert_stmt->bindParam(':repair_description',$repair_description);
$insert_stmt->bindParam(':hardware_details', $hardware_details);
$insert_stmt->bindParam(':diagnostic_fee', $diagnostic_fee);
$insert_stmt->bindParam(':tech_assigned', $tech_assigned);
$insert_stmt->bindParam(':current_status', $current_status);
$insert_stmt->bindParam(':technician_notes', $technician_notes);
$insert_stmt->bindParam(':admin_notes', $admin_notes);
$insert_stmt->bindParam(':invoice_status', $invoice_status);
$insert_stmt->bindParam(':invoice_number', $invoice_number);
if($insert_stmt->execute())
{
$insertMsg="Created Successfully........sending email now";
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>
<?php
if(isset($_POST['btn_create'])){
$to = "EMAIL_ADDRESS"; // this is your Email address
$from = "EMAIL_ADDRESS"; // this is the sender's Email address
$job_number = $_POST['job_number'];
$date = $_POST['date'];
$client_full_name = $_POST['client_full_name'];
$item_for_repair = $_POST['item_for_repair'];
$subject = "JC$job_number has been added to ECEMS";
$message = "Hi Admin. A new job card has been added to ECEMS on the $date for $client_full_name. The item for repair is a $item_for_repair. Please start diagnostics immediately for this item.";
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
header("refresh:1;repairs.php");
}
?>
I tried to follow this tut: Send email with PHP from html form on submit with the same script
I have also tried activating errors on this page with no results:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
I am new and have NEVER done a code like this where the AUTO INCREMENT number needs to be sent in an email. Please can someone assist me. I can edit my question if more clarification is needed.
EDIT: Ive done some research and found i can use the lastInsertID (https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id function. Some help implementing this would be so appreciated.
$insertId = false;
if($insert_stmt->execute())
{
$insertId = $insert_stmt->insert_id;
$insertMsg="Created Successfully........sending email now";
}
if($insertId){
// do stuff with the insert id
}
Ok so i used the MAX method to solve this issue. i added the following to the top of my createnewrepairs.php page:
$stmt = $db->prepare("SELECT MAX(job_number) AS max_id FROM repairs"); $stmt -> execute(); $job_number = $stmt -> fetch(PDO::FETCH_ASSOC); $max_id = $job_number['max_id'];
Then on the same page, i added the following form field
<div class="form-group">
<label for="job_number">Job Number</label>
<input type="job_number" name="job_number" id="job_number" class="form-control" value="<?php echo $max_id+1;?>" readonly>
</div>
Then on the form processing page (processnewrepair.php) my code in my original post worked and generated the AUTO INCREMENT NUMBER to send in an email.

How to make the option value include more than 1 value?

I have a problem to show the 4 different data in the input fields. I have create the onchange function to get the 1 value in the input field, but how to get other different data to show in the other fields:
Below is my coding:
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;"> *</span></label>
<div class="col-lg-3">
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option>Please Select</option>
<option value="New Category_value">New Category</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
$function_code_select = $rs_incharge['function_code'];
$function_name_select = $rs_incharge['function_name']
$activity_code_select = $rs_incharge['activity_code']
$activity_name_select = $rs_incharge['activity_name']
echo '<option value="' . $rs_incharge['function_code'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
<!--<input type="text" class="form-control blank" id="parentid" name="parentid" title="parentid" onblur="capitalize(this.id, this.value);">-->
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Function Code:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_code" name="function_code" title="function_code" value="">
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Function Name:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_name" name="function_name" title="function_name">
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Code:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="activity_code" name="activity_code" title="activity_code">
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Name:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="activity_name" name="activity_name" title="activity_name">
</div>
</div>
<script>
function getComboA(selectObject) {
var value = selectObject.value;
document.getElementById("function_code").value =value;
document.getElementById("function_name").value =value;
document.getElementById("activity_code").value =value;
document.getElementById("activity_name").value =value;
}
<script>
Now my output show me the 4 same data in the input fields, like below the picture:
Output 1
Actually I want to show the 4 different data, now I just grab the first data using this code $rs_incharge['function_code'], how let these $rs_incharge['function_name'], $rs_incharge['activity_code']$rs_incharge['activity_name'] can show the different field, hope someone can guide me solve this problem. Thanks.
You should store the data in additional attributes in the <option> itself then get the values onchange and then perform your operation. This should help you.
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option selected disabled>Please Select</option>
<!--Make this selected by default-->
<option value="New Category_value" data-act_code="Your-default-act-code" data-act_name="Your-default-act-name">New Category</option
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
$function_code_select = $rs_incharge['function_code'];
$function_name_select = $rs_incharge['function_name']
$activity_code_select = $rs_incharge['activity_code']
$activity_name_select = $rs_incharge['activity_name']
// save the data here↓↓ in data attributes
echo "<option value='{$function_code_select}' data-act_code='{$activity_code_select}' data-act_name='{$activity_name_select}'>{$function_name_select}</option>";
}
?>
</select>
Then in your script
<script>
function getComboA(selectObject) {
let func_code = selectObject.value; // get the value of selected option
let func_name = selectObject.selectedOptions[0].textContent; // get the text of selected option
let act_code = selectObject.selectedOptions[0].getAttribute("data-act_code"); // get attribute of selected option
let act_name = selectObject.selectedOptions[0].getAttribute("data-act_name"); // get attribute of selected option
document.getElementById("function_code").value = func_code;
document.getElementById("function_name").value = func_name;
document.getElementById("activity_code").value = act_code;
document.getElementById("activity_name").value = act_name;
}
</script>
Below is the quick demo of what I am talking about-
function getComboA(selectObject) {
let func_code = selectObject.value; // get the value of selected option
let func_name = selectObject.selectedOptions[0].textContent; // get the text of selected option
let act_code = selectObject.selectedOptions[0].getAttribute("data-act_code"); // get attribute of selected option
let act_name = selectObject.selectedOptions[0].getAttribute("data-act_name"); // get attribute of selected option
document.getElementById("function_code").value = func_code;
document.getElementById("function_name").value = func_name;
document.getElementById("activity_code").value = act_code;
document.getElementById("activity_name").value = act_name;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option selected disabled>Please Select</option>
<!--Make this selected by default-->
<option value="New Category_value" data-act_code="Your-default-act-code" data-act_name="Your-default-act-name">New Category</option>
<!-- Provide default values here -->
<!-- After giving values via PHP your HTML code should look like this-->
<option value='Function Code 1' data-act_code='Activity Code 1' data-act_name='Activity Name 1'>Function Name 1</option>
<option value='Function Code 2' data-act_code='Activity Code 2' data-act_name='Activity Name 2'>Function Name 2</option>
<option value='Function Code 3' data-act_code='Activity Code 3' data-act_name='Activity Name 3'>Function Name 3</option>
</select>
Function Code: <input type="text" class="form-control" id="function_code" name="function_code" title="function_code" /><br>
Function Name: <input type="text" class="form-control" id="function_name" name="function_name" title="function_name" /><br>
Activity Code: <input type="text" class="form-control" id="activity_code" name="activity_code" title="activity_code" /><br>
Activity Name: <input type="text" class="form-control" id="activity_name" name="activity_name" title="activity_name" />

How to display selected drop down's ID and Name in input box using jquery?

I have one dynamic dropdown list where i get ID and Name.
And i have 2 input box above this.
If user will select any value from dropdown then its ID and Name should be display in input boxes.
How to get this in jquery ?
Below is my code for getting dropdown list from database.
<div class="col-md-12">
<?php
$m_option_selectedvalue = !empty($m_option)?$m_option:"";
$m_option_selectbox = '<select " name="id_menu" id="id_menu" class="form-control" required ><option value=""></option>';
foreach ($main_menu_list as $row):
$m_option_value = $row["id_menu"];
$m_optione_desc = $row["name_menu"];
$m_option_selected = ($m_option_selectedvalue == $m_option_value)?" selected ":"";
$m_option_selectbox = $m_option_selectbox .'<option value="'.$m_option_value.'" '.$m_option_selected.'>'.$m_optione_desc.'</option>';
endforeach;
$m_option_selectbox .= "</select>";
?>
</div>
$m_option_value = $row["id_menu"];
$m_optione_desc = $row["name_menu"];
I am getting id from $m_option_value this variable.
And name from $m_optione_desc this variable.
Below is my code where i want this 2 values:
<div class="col-md-12">
<label>Role Code </label>
<div>
<input type="text" name="role" id="role" class="form-control"/>
</div>
<label>Role Name </label>
<div class="col-md-4">
<input type="text" name="role_name" id="role_name" class="form-control"/>
</div>
</div>
try this
$('#id_menu').change(function(){
var opt = $(this).find('option:selected');
$('#role').val(opt.val());
$('#role_name').val(opt.html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="id_menu" id="id_menu" class="form-control" required ><option value=""></option>
<option value="1"> item 1</option>
<option value="2"> item 2</option>
<option value="3"> item 3</option>
</select>
<div class="col-md-12">
<label>Role Code </label>
<div>
<input type="text" name="role" id="role" class="form-control"/>
</div>
<label>Role Name </label>
<div class="col-md-4">
<input type="text" name="role_name" id="role_name" class="form-control"/>
</div>
</div>

I want to execute an SQL query to upload images in PHP. The query fetches data but doesn't execute

My Database Table
After execution Query Looks like this
After executing , the inner failed echo appears
<div class="form-group">
<label for="in-15" class="control-label">Listing Type</label>
<select id="in-15" required name="listing_type" data-placeholder="---" class="form-control">
<option label=" "></option>
<option value="Sale">Sale</option>
<option value="Rent">Rent</option>
</select>
</div>
<div class="form-group">
<label for="in-2" class="control-label">Property type</label>
<select id="in-2" required name="property_type" data-placeholder="---" class="form-control">
<option label=" "></option>
<option value="Residential">House</option>
<option value="SOHO">Flate</option>
<option value="Land">Land</option>
</select>
</div>
<div class="form-group">
<label for="in-11" class="control-label">Price</label>
<input id="in-11" name="price" type="number" min="0" placeholder="" required class="form-control">
</div>
<div class="form-group">
<label for="in-11" class="control-label">Area(In Marla)</label>
<input id="in-11" name="area" type="number" min="0" placeholder="" required class="form-control">
</div>
<div class="form-group">
<label for="in-11" class="control-label">No of Bedrooms</label>
<input id="in-11" name="beds" type="number" min="0" placeholder="" required class="form-control">
</div>
<div class="form-group">
<label for="in-11" class="control-label">No of Bethrooms</label>
<input id="in-11" name="bathroom" type="number" min="0" placeholder="" required class="form-control">
</div>
<div class="form-group">
<label for="in-11" class="control-label">No of Girages</label>
<input id="in-11" name="girages" type="number" min="0" placeholder="" required class="form-control">
</div>
<div id='form-block-3' class='form__block js-form-block'>
<div class='row'>
<div class='form-group form-group--description'>
<label for='in-13' class='control-label'>address</label>
<textarea name='address' id='in-13' required data-parsley-trigger='keyup' data-parsley-minlength='200' data-parsley-validation-threshold='10' data-parsley-minlength-message='You need to enter at least a 200 caracters long comment..' class='form-control form-control--description'>
</textarea>
</div>
</div>
</div>
<div id='form-block-3' class='form__block js-form-block'>
<div class='row'>
<div class='form-group form-group--description'>
<label for='in-13' class='control-label'>Description</label>
<textarea name='desc' id='in-13' required data-parsley-trigger='keyup' data-parsley-minlength='200' data-parsley-validation-threshold='10' data-parsley-minlength-message='You need to enter at least a 200 caracters long comment..' class='form-control form-control--description'>
</textarea>
</div>
</div>
</div>
<div class="form-group">
<label for="in-11" class="control-label">Image 1</label>
<input id="in-11" name="file1" type="file" placeholder="" required class="form-control">
</div>
<div class="form-group">
<label for="in-11" class="control-label">Image 2</label>
<input id="in-11" name="file2" type="file" placeholder="" required class="form-control">
</div>
<div class="form-group">
<label for="in-11" class="control-label">Image 3</label>
<input id="in-11" name="file3" type="file" placeholder="" required class="form-control">
</div>
<div class="form-group">
<label for="in-11" class="control-label">Image 4</label>
<input id="in-11" name="file4" type="file" placeholder="" required class="form-control">
</div>
<div class="form-group">
<label for="in-11" class="control-label">Image 5</label>
<input id="in-11" name="file5" type="file" placeholder="" required class="form-control">
</div>
</div>
<div class="row">
<button type="submit" name="add_adv" class="form__submit">Submit</button>
</div>
</form>
<?php
if(isset($_POST['add_adv']))
{
$listing_type = $_POST['listing_type'];
$property_type = $_POST['property_type'];
$beds = $_POST['beds'];
$bathroom = $_POST['bathroom'];
$price = $_POST['price'];
$girages = $_POST['girages'];
$address = $_POST['address'];
$area = $_POST['area'];
$desc = $_POST['desc'];
$file1 = $_FILES['file1']['name'];
$tmp_file1 = $_FILES['file1']['tmp_name'];
$file2 = $_FILES['file2']['name'];
$tmp_file2 = $_FILES['file2']['tmp_name'];
$file3 = $_FILES['file3']['name'];
$tmp_file3 = $_FILES['file3']['tmp_name'];
$file4 = $_FILES['file4']['name'];
$tmp_file4 = $_FILES['file4']['tmp_name'];
$file5 = $_FILES['file5']['name'];
$tmp_file5 = $_FILES['file5']['tmp_name'];
$loc1 = '../assets/img/'.$file1;
$loc2 = '../assets/img/'.$file2;
$loc3 = '../assets/img/'.$file3;
$loc4 = '../assets/img/'.$file4;
$loc5 = '../assets/img/'.$file5;
$move1 = move_uploaded_file( $tmp_file1 , $loc1 );
$move2 = move_uploaded_file( $tmp_file2 , $loc2 );
$move3 = move_uploaded_file( $tmp_file3 , $loc3 );
$move4 = move_uploaded_file( $tmp_file4 , $loc4 );
$move5 = move_uploaded_file( $tmp_file5 , $loc5 );
if( $move1 && $move2 && $move3 && $move4 && $move5 )
{
echo $insert_adver = "
INSERT INTO property
( propertytype , datetime , price , area , desc , address , bedroom , bathroom
,gerages , listingtype , seller_id , smimage , status , smimage2 , smimage3
, smimage4 , smimage5 )
values ( '$property_type' , NOW() , '$price' ,
'$area' , '$desc' , '$address' ,'$beds'
,'$bathroom' , '$girages' ,'$listing_type' , '$_SESSION[id]'
,'$file1' ,'0' ,'$file2' ,'$file3' ,'$file4' , '$file5')
";
$exec_this_query = mysqli_query( $con , $insert_adver );
if($exec_this_query)
{
echo"<script>alert('successfully uploaded ! Please wait for Approval from Admin')</script>";
}
else
{
echo"<script>alert('Inner FAILED !')</script>";
}
}
else
{
echo"<script>alert('FAILED !')</script>";
}
}
?>
Image of my Solution
The mistake was that i had used desc attribute in Table which is a reserved word..

INSERT not working?

I have a simple INSERT that is not working. Can you php/mySQL experts do a quick scan. When I process the code, it does say "successful" i.e. "1 record added'" and "successfully created directory" - but there is NO new directory I see on the server AND it adds a record in the db but all the fields are empty?
include("_inc/config.php");
$sql = "INSERT INTO members (id, active, namefirst, namelast, email, username, password, birthyear, gender, country, postalcode, education, dimension, referredby, pic_profile, pic_background, origination, customerid) VALUES ('','$_POST[active]','$_POST[namefirst]','$_POST[namelast]','$_POST[email]','$_POST[username]','$_POST[password]','$_POST[birthyear]','$_POST[gender]','$_POST[country]','$_POST[postalcode]','$_POST[education]','$_POST[dimension]','$_POST[referredby]','$_POST[pic_profile]','$_POST[pic_background]','$_POST[origination]','$_POST[customerid]')";
$newdir=$_POST['username'];
if (!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}
echo "1 record added";
echo "<br><br>";
// Build Directories
mkdir("../".$newdir,0777);
chmod("../".$newdir,0777);
mkdir("../".$newdir."/assets/",0777);
chmod("../".$newdir."/assets/",0777);
if("../".mkdir($newdir, 0777, true))
{
echo 'successfully created directory';
}
else
{
die('problem creating directory');
}
mysql_close($con)
FORM
<form role="form" name="form" action="mpx_signup_process.php">
<div class="form-group">
<label for="nameandemail">Name and Email</label>
<input name="namefirst" class="form-control" placeholder="First Name">
</div>
<div class="form-group">
<input name="namelast" class="form-control" placeholder="Last Name">
</div>
<div class="form-group">
<input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group"><hr></div>
<div class="form-group">
<label for="accountinfo">Account Information</label>
<input name="username" class="form-control" id="exampleInputPassword1" placeholder="User Name">
</div>
<div class="form-group">
<input name="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-group">
<input name="password" class="form-control" id="exampleInputPassword1" placeholder="Password Repeat">
</div>
<hr />
<div class="form-group">
<label for="personalinfo">Personal Information</label>
<h3>Country</h3>
<select name="country" class="form-control">
<option value="USA">United States</option>
<option value="UMI">United States Minor Outlying Islands</option>
<option value="USA">- - - - - -</option>
<option value="ALA">Åland Islands</option>
<option value="ALB">Albania</option>
<option value="DZA">Algeria</option>
<option value="ASM">American Samoa</option>
</select>
</div>
<div class="form-group">
<label for="referredby">Postal Code</label>
<input name="postalcode" class="form-control" id="exampleInputPostalCode" placeholder="Postal Code">
</div>
<div class="form-group">
<label for="gender">Gender</label>
<div class="radio">
<label>
<input type="radio" name="gender" id="optionsRadios4" value="01">
Male
</label>
<label>
<input type="radio" name="gender" id="optionsRadios5" value="02">
Female
</label>
<label>
<input type="radio" name="gender" id="optionsRadios6" value="03">
Other
</label>
</div>
</div>
<div class="form-group">
<label for="dateofbirth">Date of Birth</label>
<select name="birthyear" class="form-control">
<?php
for($i = 2010 ; $i > 1920; $i--){
echo "<option value=".$i.">$i</option>";
}
?>
</select>
</div>
<div class="form-group">
<label for="education">Education</label>
<select name="education" class="form-control">
<option value="01">Pre High School</option>
<option value="02">High School</option>
<option value="03">Some College</option>
<option value="04">College Graduate</option>
<option value="05">Graduate Degree</option>
</select>
</div>
<div class="form-group">
<label for="referredby">Referred By</label>
<input name="referredby" class="form-control" id="exampleInputreferredby" placeholder="Referred By">
</div>
<input type="hidden" name="active" value="no">
<input type="hidden" name="dimension" value="00">
<input type="hidden" name="pic_profile" value="mpx_profilepic.jpg">
<input type="hidden" name="pic_background" value="mpx_bsckgroundpic.jpg">
<?php
$date = date('l jS \of F Y h:i:s A');
?>
<input type="hidden" name="origination" value="<?php echo $date; ?>">
<button type="submit" class="btn btn-default">Submit</button>
</form>
In the form, add a method="POST"
<form role="form" name="form" method="POST" action="mpx_signup_process.php">
Because $_POST["username"] is empty the new directory name is empty thus the reason for not creating it I assume.
I would also suggest adding a check to determine if the record was infact inserted. The code above will ALWAYS return "1 record added" which is incorrect. Use something similar to mysql_rows_affected but using mysqli or PDO rather.
if(mysql_affected_rows() > 0)
{
echo "1 record added";
echo "<br><br>";
// Build Directories
mkdir("../".$newdir,0777);
chmod("../".$newdir,0777);
mkdir("../".$newdir."/assets/",0777);
chmod("../".$newdir."/assets/",0777);
}
else
{
echo "No records added";
}
Sure it will always "successfully created directory"!
Please look,
if("../".mkdir($newdir, 0777, true))
When mkdir eval to false (fail), you'll get
if("../" . false)
// or
if("../false") // maybe
// or just
if("../") // maybe, I'm unsure.
and it's non empty string, BTW, it always eval to true ensuring you hit the
echo 'successfully created directory';
In $sql you're not escaping the string properly
the php post variables in $sql are as $_POST[namelast], $_POST[email], etc, whereas it should be $_POST['namelast'], $_POST['email'], etc.

Categories