insert checkboxes in sql and echo them using php - php

I am building a form to input different data to characterize events (name, date, type of event) into a sql db so that they can be searched by location later on.
Below are the html form, the php file (insert.php) to put the data in the db and finally the php of the search result.
The issue is with the checkboxes, I am trying to make it so that the multiple values get stored in the same sql column, a little like tags that can be used in the search.
My issue right now is that when I process the form, the entry in my sql table in the column of the checkbox says "Array" and then it does not display on the search result.
I don't know if the issue is that I input the data incorrectly in the table or if the results display is the issue.
Any help much appreciated.
FORM
<form class="form-horizontal" enctype="multipart/form-data" action="insert.php" method="post">
<label class="control-label"> Name Of The Event
<input type="text" placeholder="Name of the event" name="name"></label>
<div class="control-group">
<label class="control-label"> Address</label>
<input type="text" placeholder="address" name="address">
<label class="control-label"> City</label>
<input type="text" name="citycountry" class="location" autocomplete="off" id="searchTextField" placeholder="City">
<div id="map_canvas"></div>
</div>
</div>
<div class="control-group">
<label class="control-label"> Description</label>
<div class="controls">
<input type="text" name="description" placeholder="Add details about this event.">
</div>
</div>
<div class="control-group">
<label class="control-label"> Start</label>
<div class="controls">
<div id="sandbox-container">
<input type="date" name="startdate" placeholder="mm/dd/yyyy"> <!--<span class="add-on"><i class="icon-th"></i></span>-->
</div> <input type="time" class="span2" name="starttime">
</div>
</div>
<div class="control-group">
<label class="control-label"> End</label>
<div class="controls">
<div id="sandbox-container">
<input type="date" name="enddate" placeholder="mm/dd/yyyy"> <!--<span class="add-on"><i class="icon-th"></i></span>-->
</div> <input type="time" name="endtime">
</div>
</div>
<div class="control-group">
<label class="control-label"> Add Image</label>
<div class="input-append">
<input type="file" name="picture"></br>
</div> </div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" name="eventtype[]" value="Lesson"> Dance Lesson
</label>
<label class="checkbox">
<input type="checkbox" name="eventtype[]" value="Social"> Social Dancing
</label>
<label class="checkbox">
<input type="checkbox" name="eventtype[]" value="Competition"> Competition
</label>
<label class="checkbox">
<input type="checkbox" name="eventtype[]" value="Show"> Show
</label>
<label class="checkbox">
<input type="checkbox" name="eventtype[]" value="Convention"> Dance Convention
</label>
</div>
</div> <div class="control-group">
<div class="controls">
<input class="btn btn-info" type="submit">
</div>
</div>
</form>
INSERT.PHP
<?php
//preparing the patch to copy the uploaded file
$target_path = "upload/";
$target_path = $target_path . uniqid();
//adding the name of the file, finishing the path
$target_path = $target_path . $_FILES["file"]. basename( $_FILES['picture']['name']);
//moving the file to the folder
if(move_uploaded_file($_FILES['picture']['tmp_name'], "$target_path")) {
echo "The file ". basename( $_FILES['picture']['name']).
" has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
//preparing the query to insert the values
$query = "INSERT INTO complete_table (eventtype, name, address, citycountry, starttime, startdate, endtime, enddate, description,picture) VALUES ( '$_POST[eventtype]' ,'$_POST[name]','$_POST[address]','$_POST[citycountry]','$_POST[starttime]', '$_POST[startdate]','$_POST[enddate]','$_POST[endtime]','$_POST[description]', '". $target_path ."')";
//opening connection to db
$link = mysql_connect('localhost', 'root', ' ');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//selecting a db
mysql_select_db("wcs_venues", $link) or die(mysql_error());
//running the query
$result = mysql_query($query) or die (mysql_error());
//closing the connection
mysql_close($link);
SEARCH RESULTS
<?php
mysql_connect ("localhost", "root"," ") or die (mysql_error());
mysql_select_db("wcs_venues") or die(mysql_error());
$term = $_POST['term'];
$sql = mysql_query("SELECT * FROM complete_table WHERE citycountry LIKE '%$term%'")or die(mysql_error());
}
/* echo '<td width="250px;" style="float:left;"><img src="http://s3-media3.ak.yelpcdn.com/bphoto/CJziwp9QDcSPuYVjy4uasg/l.jpg" class="bigthumb" style="margin:30px; width:114px; height:auto;"></td>';*/
echo '<table class="table table-bordered">';
echo ' <tbody> <tr> <td> Name of the venue </td>';
echo '<td> </td> </tr>';
echo '<tr> <td> Address </td>';
echo '<td>'.$row['citycountry'];
echo '</td> </tr>';
echo '<tr> <td> Date </td>';
echo '<td>'.$row['date'];
echo '</td> </tr>';
echo '<tr> <td> Picture </td>';
echo '<td><img src=http://localhost/theme/'.$row['picture'] .'> </td>';
echo '</td> </tr>';
echo '<tr> <td> Eventtype </td>';
if(isset($_POST["eventtype"])) //checks if any interest is checked
{
foreach($_POST["eventtype"] as $value) //Iterate the interest array and get the values
{
echo '<td>'.$value; //print the values
}
}
echo '</td> </tr>';
echo '</table>';
}
?>

Convert $_POST['eventtype'] into string:
if( isset( $_POST['eventtype'] ) && is_array( $_POST['eventtype'] )) {
$eventtypes = join( ',', $_POST['eventtype'] );
} else {
$eventtypes = '';
}
use $eventtypes in your INSERT.

Related

Update Table Row Value with Post Button

I have some data in my mysql database which I am displaying in table using PHP like below
$requirement_qry="SELECT t1.*, t2.id as unit_id, t2.name as unit_name FROM `tbl_requirements`
t1 INNER JOIN tbl_units AS t2 on unit_type = t2.id AND project_id='".$_GET['project_id']."' AND user_id='".$userid."'";
$requirement_result=mysqli_query($mysqli,$requirement_qry);
<form action="" name="addeditcategory" method="post" class="form form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="project_id" value="<?php echo $_GET['project_id'];?>" />
<div class="section">
<div class="section-body">
<div class="form-group">
<label class="col-md-3 control-label" >Project Name :-</label>
<div class="col-md-6">
<input type="text" name="name" id="name" value="<?php if(isset($_GET['project_id'])){echo $row['name'];}?> " class="form-control" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" >Location :-</label>
<div class="col-md-6">
<input type="text" name="location" id="location" value="<?php if(isset($_GET['project_id'])){echo $row['location'];}?> " class="form-control" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Project Status :-</label>
<div class="col-md-6">
<select name="status" id="status" class="select2" disabled>
<?php if (!isset($_GET['project_id'])) { ?>
<option value="1">--Project Status--</option>
<?php } ?>
<option value="1" <?php echo $row['status'] == '1' ? 'selected' : ''; ?> >Open</option>
<option value="2" <?php echo $row['status'] == '2' ? 'selected' : ''; ?> >Closed</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Details :-</label>
</div>
<div class="col-md-6">
<textarea name="details" id="details" rows="4" class="form-control" disabled><?php echo stripslashes($row['details']);?></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Requirements :-</label>
</div>
<div class="col-md-6">
<table id="t01">
<thead>
<tr>
<th>#</th>
<th>Requirements</th>
<th>Required</th>
<th>Sent</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
while ($row1 = mysqli_fetch_array($requirement_result))
{
$id = $row1['id'];
$unit_name = $row1['unit_name'];
echo '<input type="hidden" name="reqId" id= "reqId" value="'.$id.'" />';
echo '<tr>
<td>'.$no.'</td>
<td>'.$row1['name'].'</td>
<td>'.$row1['unit_required']." ".$unit_name.'</td>
<td><input type="number" id = "received" name = "received" value ="'.$row1['unit_received'].'"/></td>
<td><button type="submit" name="submit" class="btn btn-primary" style="padding:5px 10px;">Submit</button></td>
</tr>';
$no++;
}?>
</tbody>
</table>
</div>
</div>
</div>
</form>
Above code is properly displaying my data. I have one field value from row need to update using submit button. Its working fine if there only one row...if there multiple row, its not updating data of it except last row.
My submit code is like below
if(isset($_POST['submit']) and isset($_POST['project_id']))
{
$projectId = $_GET['project_id'];
$data = array(
'unit_received' => $_POST['received']
);
$unit_edit=Update('tbl_requirements', $data, " WHERE id = '".$_POST['reqId']."'");
print_r($unit_edit);
echo $unit_edit;
if ($unit_edit > 0)
{
$_SESSION['msg']="11";
header( "Location:view_open_project.php?project_id=".$_POST['project_id']);
exit;
}
}
I am little new in PHP, Let me know if someone can help me for solve the bug.
Thanks a lot :)
As #Saji has already stated, you are replicating your name through the loop.You should rather use
echo '<input type="hidden" name="reqId[]" id= "reqId" value="'.$id.'" />';
Note the [] brackets that were appended so that you create an array of names.
To update, you need a loop like
for($i=0;$i<count($_POST['reqId']);$i++){
$unit_edit=Update('tbl_requirements', $data, " WHERE id = '".$_POST['reqId'][$i]."'");
}
What you can do is just create a hidden form where you will keep original elements. When the save button click you just find the actual value and set this to the elements inside hidden form, then trigger the submit button inside hidden form.
Note the changes I have done on your code.
1.Removed the hidden element reqId from inside loop.
2.Given a class name to the button inside loop and changed the button type from submit to button. Added data attribute data-id="'.$id.'".
3.The input received inside the loop has given unique id and name by concatenated the $id.
4.Created hidden form with hidden input in it. It has your actual input names.
5.Created a jquery function to attach click event to the button inside loop.
6.Moved the hidden element project_id to inside hidden form.
Now see the below code for more details. Hope this will help you..
<form action="" name="addeditcategory" method="post" class="form form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="project_id" value="<?php echo $_GET['project_id']; ?>"/>
<div class="section">
<div class="section-body">
<div class="form-group">
<label class="col-md-3 control-label">Project Name :-</label>
<div class="col-md-6">
<input type="text" name="name" id="name" value="<?php if (isset($_GET['project_id'])) {
echo $row['name'];
} ?> " class="form-control" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Location :-</label>
<div class="col-md-6">
<input type="text" name="location" id="location" value="<?php if (isset($_GET['project_id'])) {
echo $row['location'];
} ?> " class="form-control" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Project Status :-</label>
<div class="col-md-6">
<select name="status" id="status" class="select2" disabled>
<?php if (!isset($_GET['project_id'])) { ?>
<option value="1">--Project Status--</option>
<?php } ?>
<option value="1" <?php echo $row['status'] == '1' ? 'selected' : ''; ?> >Open</option>
<option value="2" <?php echo $row['status'] == '2' ? 'selected' : ''; ?> >Closed</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Details :-</label>
</div>
<div class="col-md-6">
<textarea name="details" id="details" rows="4" class="form-control"
disabled><?php echo stripslashes($row['details']); ?></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Requirements :-</label>
</div>
<div class="col-md-6">
<table id="t01">
<thead>
<tr>
<th>#</th>
<th>Requirements</th>
<th>Required</th>
<th>Sent</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
while ($row1 = mysqli_fetch_array($requirement_result)) {
$id = $row1['id'];
$unit_name = $row1['unit_name'];
echo '<tr>
<td>' . $no . '</td>
<td>' . $row1['name'] . '</td>
<td>' . $row1['unit_required'] . " " . $unit_name . '</td>
<td><input type="number" id = "received' . $id . '" name = "received' . $id . '" value ="' . $row1['unit_received'] . '"/></td>
<td><button type="button" name="submit" class="btn btn-primary submit-click" data-id="' . $id . '" style="padding:5px 10px;">Submit</button></td>
</tr>';
$no++;
} ?>
</tbody>
</table>
</div>
</div>
</div>
</form>
<form action="" id="addeditcategory_temp" name="addeditcategory_temp" method="post" class="form form-horizontal" enctype="multipart/form-data" style="display:none;>
<input type="hidden" name="project_id" value="<?php echo $_GET['project_id']; ?>"/>
<input type="hidden" name="reqId" id= "reqId" value="" />
<input type="number" id = "received" name = "received" value ="">
<button id="submitButton" type="submit" name="submit" class="btn btn-primary" style="padding:5px 10px;">
</form>
<script>
$(document).ready(function(e){
$('.submit-click').off('click').on('click', function(e){
var reqId = $(this).data('id');
var received = $('#received'+reqId).val();
var form = $('#addeditcategory_temp');
form.find('#reqId').val(reqId);
form.find('#received').val(received);
form.find('#submitButton').trigger('click');
})
});
</script>

HTML form with php is not working with a single submit button.

I am developing a form in html and php and there are multiple section in a page and I want a single submit button for every section. But I am unable to do it. With multiple submit buttons it works. Can anybody help me out with this problem.I need to create a form with single submit button. Here is the example code..
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Records Form</title>
</head>
<body>
<table style="width:100%">
<tr>
<th rowspan="2">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="insert1.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label">NCBI Accession number</label>
<input type="text" name="Gene_NCBI_Accession_number" placeholder="NCBI Accession number" class="form-control">
</div>
<div class="form-group">
<label class="control-label">locus tag</label>
<input type="text" name="Gene_name_locus_tag" placeholder="locus tag" class="form-control">
</div>
</th>
<td>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="insert1.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label"><b>Antibiotic</label>
<input type="text" name="Antibiotic_name" placeholder="Antibiotic name" class="form-control">
</div>
</td>
<td>
<div class="form-group">
<label class="control-label"><b>Name</label>
<input type="text" name="Name" placeholder="Name" class="form-control" required>
</div>
</td>
</tr>
<tr>
<td rowspan="2">
<h4> Please fill in information for other related data or give your comments if any</h4>
<br>
<textarea rows="7" cols="100" name="comment" form="usrform">
Enter text here...</textarea>
</td>
<td>
</div>
<div class="form-group">
<label class="control-label"><b>Protein function</label>
<input type="text" name="Protein_function" placeholder="Protein_function" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Biological process</label>
<input type="text" name="Biological_process" placeholder="Biological process" class="form-control">
</div>
</td>
</tr><br>
<br>
<br>
<br>
<br>
<br>
</form></table>
<input type="submit" value="Add Records">
<?php include 'footer.php'; ?>
</body>
</html>
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("abc", "abc", "abc", "abc");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$Gene_NCBI_Accession_number = mysqli_real_escape_string($link, $_REQUEST['Gene_NCBI_Accession_number']);
$Gene_name_locus_tag = mysqli_real_escape_string($link, $_REQUEST['Gene_name_locus_tag']);
$Antibiotic_name = mysqli_real_escape_string($link, $_REQUEST['Antibiotic_name']);
$Name = mysqli_real_escape_string($link, $_REQUEST['Name']);
$Protein_function = mysqli_real_escape_string($link, $_REQUEST['Protein_function']);
$Biological_process= mysqli_real_escape_string($link, $_REQUEST['Biological_process']);
// attempt insert query execution
$sql = "INSERT INTO contact_form_info (Gene_NCBI_Accession_number, Gene_name_locus_tag, Antibiotic_name, Name, Protein_function, Biological_process) VALUES ('$Gene_NCBI_Accession_number', '$Gene_name_locus_tag', '$Antibiotic_name', '$Name','$Protein_function', '$Biological_process' )";
if(mysqli_query($link, $sql)){
#echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
Your submit button is outside of form that's why its not working. Refer below code to resolve your issue.
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Records Form</title>
</head>
<body>
<table style="width:100%">
<tr>
<th rowspan="2">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="insert1.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label">NCBI Accession number</label>
<input type="text" name="Gene_NCBI_Accession_number" placeholder="NCBI Accession number" class="form-control">
</div>
<div class="form-group">
<label class="control-label">locus tag</label>
<input type="text" name="Gene_name_locus_tag" placeholder="locus tag" class="form-control">
</div>
</th>
<td>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="insert1.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label"><b>Antibiotic</label>
<input type="text" name="Antibiotic_name" placeholder="Antibiotic name" class="form-control">
</div>
</td>
<td>
<div class="form-group">
<label class="control-label"><b>Name</label>
<input type="text" name="Name" placeholder="Name" class="form-control" required>
</div>
</td>
</tr>
<tr>
<td rowspan="2">
<h4> Please fill in information for other related data or give your comments if any</h4>
<br>
<textarea rows="7" cols="100" name="comment" form="usrform">
Enter text here...</textarea>
</td>
<td>
</div>
<div class="form-group">
<label class="control-label"><b>Protein function</label>
<input type="text" name="Protein_function" placeholder="Protein_function" class="form-control">
</div>
<div class="form-group">
<label class="control-label">Biological process</label>
<input type="text" name="Biological_process" placeholder="Biological process" class="form-control">
</div>
</td>
</tr><br>
<br>
<br>
<br>
<br>
<br><input type="submit" value="Add Records">
</form></table>
<?php include 'footer.php'; ?>
</body>
</html>
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("abc", "abc", "abc", "abc");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$Gene_NCBI_Accession_number = mysqli_real_escape_string($link, $_REQUEST['Gene_NCBI_Accession_number']);
$Gene_name_locus_tag = mysqli_real_escape_string($link, $_REQUEST['Gene_name_locus_tag']);
$Antibiotic_name = mysqli_real_escape_string($link, $_REQUEST['Antibiotic_name']);
$Name = mysqli_real_escape_string($link, $_REQUEST['Name']);
$Protein_function = mysqli_real_escape_string($link, $_REQUEST['Protein_function']);
$Biological_process= mysqli_real_escape_string($link, $_REQUEST['Biological_process']);
// attempt insert query execution
$sql = "INSERT INTO contact_form_info (Gene_NCBI_Accession_number, Gene_name_locus_tag, Antibiotic_name, Name, Protein_function, Biological_process) VALUES ('$Gene_NCBI_Accession_number', '$Gene_name_locus_tag', '$Antibiotic_name', '$Name','$Protein_function', '$Biological_process' )";
if(mysqli_query($link, $sql)){
#echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>

how do I foreach through html input form and insert multiple rows or one based on a selected date field?

how do I foreach through html input form and insert multiple rows or one based on a selected date field? in other words when a user enters "name" "description" and "shift" and then selects either one date or more then one. PHP will then enter the same information for either one new row or multiples based on how many dates were selected.
<?php
if(isset($_REQUEST['submit']))
{
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "xxxx", "xxxx", "xxxx");
// Check connection
if($link === false){
die("| ERROR: Could not connect. " . mysqli_connect_error());
}
$name = mysqli_real_escape_string($link, $_REQUEST['name']);
$desc = mysqli_real_escape_string($link, $_REQUEST['description']);
$shift = mysqli_real_escape_string($link, $_REQUEST['shift']);
$date = mysqli_real_escape_string($link, $_REQUEST['daterange']);
$sql = "insert into db (name,description,shift,evdate) values ('$name', ' $desc','$shift','$date')";
$sql2 = "insert into db (name,description,shift,evdate) values ('$name', ' '$desc','$shift','$insert')";
if ($date=0) {
$result = mysqli_query($link, $sql);
}else{
$daterange = explode(',',$date);
foreach($daterange as $insert) {
$result = mysqli_query($link, $sql2);
}
}
if(mysqli_query($link, $sql)){
echo "";
} else{
echo "| ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
if ($link->multi_query($sql) === TRUE) {
echo "It Worked..... Maybe!!!!!!";
} else {
echo "Error: " . $sql . "<br>" . $link->error;
}
}
$link->close();
?>
<form action="test_insert.php" method="post">
<div class="col col-lg-2 col-lg-offset-0">
<div class="form-group col-lg-offset-0 col-lg-12">
<label for="Name">Employee Name:</label>
<input type="text" name="name" placeholder="First & Last Name" id="name" required>
<p class="help-block col-lg-12">First and Last Name Please.</p>
</div>
</div>
<div class="col col-lg-offset-0 col-lg-2">
<div class="form-group col-lg-12">
<label for="description">Description:</label>
<input type="text" name="description" id="description" placeholder="description..." required>
<p class="help-block">For Example: "Vacation Full Day" or "PTO 2 Hours." </p>
</div>
</div>
<div class="col col-lg-offset-0 col-lg-3">
<label for="shift">Shift:</label><br>
<input type="radio" name="shift" value="First Shift" id="shift" checked> First Shift |
<input type="radio" name="shift" value="Second Shift" id="shift"> Second Shift |
<input type="radio" name="shift" value="Third Shift" id="shift"> Third Shift
<p class="help-block">Select Correct Shift Worked.</p>
</div>
<div class="col col-lg-offset-0 col-lg-3">
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker1" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker2" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker3" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker4" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-10">
<label for="date2">Date/Dates:</label>
<input type="text" id="datepicker5" name="daterange" placeholder="Select Your Date" />
</div>
<div class="form-group col-lg-6">
<input type="submit" name="submit" class= "btn btn-primary">
</div>
</div>
</div>
</form>
Think the best way is to use AJAX,
Then with the response as a string you make a table or foreach in php as a string and then use the .html adapter to output the newly made data.
function submitForm(form){
var url = form.attr("action");
var formData = {};
$(form).find("input[name]").each(function (index, node) {
formData[node.name] = node.value;
});
$.post(url, formData).done(function (data) {
$('#showresults').html(result);
});
}

editing a record in a database with a form

first of all i would like to appolagise on the amount of code i am about to paste, i didn't want to snippet any more incase its a bit that's giving me the errors
i have a table named contacts and want to update the table by a form.
i am not sure if its the form or if its the code as the delete user isn't working
i have just started to learn this (a few days ago)so the code might be messy or not 100% secure as it should this is for a offline database so i would improve it as i learn.
<?php include("header.php");
//include database connection
include 'db_connect.php';
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){
//write query
$query = "update contacts
set
name = '".$mysqli->real_escape_string($_POST['name'])."',
surname = '".$mysqli->real_escape_string($_POST['surname'])."',
email = '".$mysqli->real_escape_string($_POST['email'])."',
pcode = '".$mysqli->real_escape_string($_POST['pcode'])."',
website = '".$mysqli->real_escape_string($_POST['website'])."',
gender = '".$mysqli->real_escape_string($_POST['gender'])."'
mobile = '".$mysqli->real_escape_string($_POST['mobile'])."'
phone = '".$mysqli->real_escape_string($_POST['phone'])."'
county = '".$mysqli->real_escape_string($_POST['county'])."'
town = '".$mysqli->real_escape_string($_POST['town'])."'
address = '".$mysqli->real_escape_string($_POST['address'])."'
notes = '".$mysqli->real_escape_string($_POST['notes'])."'
business = '".$mysqli->real_escape_string($_POST['business'])."'
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
if( $mysqli->query($query) ) {
echo "User was updated.";
}else{
echo "Database Error: Unable to update record.";
}
}
if($action=='delete'){ //if the user clicked ok, run our delete query
$query = "DELETE FROM users WHERE id = ".$mysqli->real_escape_string($_GET['id'])."";
if( $mysqli->query($query) ){
echo "User was deleted.";
}else{
echo "Database Error: Unable to delete record.";
}}
$query = "select id, name, pcode, website, email, surname, mobile, phone, business, gender, address, town, county, notes
from contacts
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'
limit 0,1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$id = $row['id'];
$name = $row['name'];
$surname = $row['surname'];
$pcode = $row['pcode'];
$email = $row['email'];
$business = $row['business'];
$phone = $row['phone'];
$mobile = $row['mobile'];
$gender = $row['gender'];
$address = $row['address'];
$county = $row['county'];
$notes = $row['notes'];
$town = $row['town'];
$website = $row['website']; ?>
<?php echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
?>
<body>
<div class="div-middle-big">
<!--we have our html form here where new user information will be entered-->
<a href='index.php'>Back to index</a>
</td>
</tr>
</table>
</form>
<div id="loader_cont"><img src="img/loaders/page_loader.gif"></div>
<?php include'topnav.php' ?>
<div class="container">
<div class="main_content row-fluid">
<div class="span3">
<?php include'menu.php' ?>
<!--/.well -->
</div>
<!--/span-->
<div class="span9">
<div class="row-fluid">
<div class="span12">
<ul class="breadcrumb br_styled no_space">
<li> Dashboard <span class="divider">/</span> </li>
<li class="active">Profile</li>
</ul>
<div class="widget profile_cont">
<header>
<h3>Profile: <span class="profile_title"><?php echo$name; ?> <?php echo$surname; ?></span></h3>
<ul class="toggle_content">
<li class="arrow">Toggle Content</li>
</ul>
</header>
<section class="group">
<div class="info"> <img src="http://api.thumbalizr.com/?url=http://<?php echo$website; ?>&width=250" alt="Profile picture">
<h4>Profile Picture</h4>
<div class="profile_picture">
<input type="file" />
<!-- <input type="submit" /> -->
visit website
<!-- UPLOAD -->
</div>
<ul>
<li><i class="sweet-user"></i> Profile</li>
<li><i class="sweet-settings"></i> Settings</li>
<li><i class="sweet-mail"></i> Email <?php echo$name; ?></li>
<li><i class="sweet-cog-4"></i> Widgets</li>
<li><i class="sweet-exit"></i> Logout</li>
</ul>
<div class="span3">
<div class="widget">
<header>
<h3>Grid 3</h3>
<ul class="toggle_content" style="display: none;">
<li class="arrow">Toggle Content</li>
</ul>
</header>
<section class="code_align"> <code>class="span3"</code> </section>
</div>
</div>
</div>
<div class="details">
<form action='#' method='post' border='0' class="well form-horizontal">
<fieldset>
<h4 class="group"> <span>Personal details</span> </h4>
<div class="control-group">
<div class="controls"> </div>
</div>
<div class="control-group">
<label class="control-label" for="name">First name</label>
<div class="controls">
<input id="name" type="text" name="name" value="<?php echo$name; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="surname">Last name</label>
<div class="controls">
<input id="surname" type="text" name="surname" value="<?php echo$surname; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="business">Company Name</label>
<div class="controls">
<input id="business" type="text" name="business" value="<?php echo$business; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="phone">Phone number</label>
<div class="controls">
<input id="phone" type="text" name="phone" value="<?php echo$phone; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="mobile">Mobile number</label>
<div class="controls">
<input id="mobile" type="text" name="mobile" value="<?php echo$mobile; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="gender">Sex</label>
<div class="controls">
<select class="gender" style="width:210px;" tabindex="2">
<option value="<?php echo$gender; ?>"><?php echo$gender; ?></option>
<option value="female">Female</option>
<option value="male">Male</option>
</select>
</div>
</div>
<h4>Contact details</h4>
<div class="control-group">
<label class="control-label" for="email">E-mail</label>
<div class="controls">
<input id="email" type="text" name="email" value="<?php echo$email; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="website">Website</label>
<div class="controls">
<input id="website" type="text" name="website" value="<?php echo$website; ?>" data-original-title="Without the http://">
</div>
</div>
<div class="control-group">
<label class="control-label" for="address">Address</label>
<div class="controls">
<textarea id="address" rows="3" name="address" ><?php echo$address; ?></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="skypeid">Town</label>
<div class="controls">
<input id="town" type="text" name="town" value="<?php echo$town; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="county">County</label>
<div class="controls">
<input id="county" type="text" name="county" value="<?php echo$county; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="pcode">Post code</label>
<div class="controls">
<input id="pcode" type="text" name="pcode" value="<?php echo$pcode; ?>">
</div>
</div>
<h4>Notes about <?php echo$name; ?> <?php echo$surname; ?></h4>
<p>
<textarea id="notes" rows="5" name="notes" ><?php echo$notes; ?></textarea>
</p>
<div class="form-actions">
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='id' value='<?php echo $id ?>' />
<!-- we will set the action to edit -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Edit' />
</div>
</fieldset>
</form>
The problem with the above code is thats its not updating my database and i am getting
Database Error: Unable to update record
UPDATE
i have gone back to my old files and now this dosent work
ok i gone right back to the basic files i had....
<meta http-equiv="refresh" content="0; url=../contacts.php"> <?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = "DELETE FROM contacts
WHERE created='$_GET[id]'";
mysql_select_db('pcrepairs');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not delete data: ' . mysql_error());
}
echo "Deleted data successfully\n";
mysql_close($conn);
?>
i am now getting this error
Could not delete data: Unknown column 'created' in 'where clause'
You seems to be using users table in your delete query.Does the users table exist?,if not please change it to contacts.Please let me know
Thanks
Forgetting PHP for a moment if you were to issue a SQL query, say in the command-line, you would need to use single quotes to signify the search string.
So it would like this:
DELETE FROM users WHERE id = '100';
The above has to remain true when you construct the query via PHP:
$query = "DELETE FROM users WHERE id='".$mysqli->real_escape_string($_GET['id'])."'";
If your code's failing, you really need to get into the mindset of debugging your code. Approach it in smaller chunks and work your way back up. So for instance, you can try executing the above query with a hard-coded id value in the console and confirm it works.
Can you try echo'ing the $query value before running it through mysqli? Get that sql statement and try manually running it through the database. You may also want to double check your data types. You can get an error if you try, for example, setting an NUMBER/INT field with a string value.
You forgot the commas in your SQL UPDATE statement:
$query = "update contacts
set
name = '".$mysqli->real_escape_string($_POST['name'])."',
surname = '".$mysqli->real_escape_string($_POST['surname'])."',
email = '".$mysqli->real_escape_string($_POST['email'])."',
pcode = '".$mysqli->real_escape_string($_POST['pcode'])."',
website = '".$mysqli->real_escape_string($_POST['website'])."',
gender = '".$mysqli->real_escape_string($_POST['gender'])."',
mobile = '".$mysqli->real_escape_string($_POST['mobile'])."',
phone = '".$mysqli->real_escape_string($_POST['phone'])."',
county = '".$mysqli->real_escape_string($_POST['county'])."',
town = '".$mysqli->real_escape_string($_POST['town'])."',
address = '".$mysqli->real_escape_string($_POST['address'])."',
notes = '".$mysqli->real_escape_string($_POST['notes'])."',
business = '".$mysqli->real_escape_string($_POST['business'])."'
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
You also need to review your HTML code.
EDIT
The SQL syntax for an update statement is:
UPDATE my_table_name SET col1='value1', col2='value2', ... WHERE conditions
And this should work for the delete query:
$query = "DELETE FROM users WHERE id='".$mysqli->real_escape_string($_GET['id'])."'";
If you are using PHP5+ I recommend you to use PDO instead of the old sqlite functions.
You also need to verify your data before saving into the DB.

inserting data into mysql database using php

I have a php order form named (order.php) and when the user clicks the (submit button "Next Step") it takes him to another page called (confirm-order.php)
The (confirm-order.php) shows the information that the user submitted from the (order.php) using the $_POST[] and by assigning each one of these to a variable.
Data showing on the (confirm-order.php) plain text like for example :
$itemName = $_POST['itemName'];
<?php echo $itemName; ?>
at the end of page there is a form contains only one element as (submit button)
How can i insert the $itemName data into mysql database only (after the submit button is clicked and the form actions take me to the confirmation page)?
I know how to insert data into mysql, but it didn't work with the isset() function
Do i have to write the isset function inside the form first? and below it the mysql database code?
order.php page:
<form class="form-horizontal well" action="confirm-order.php" method="POST">
<fieldset>
<legend>Personal Shopper Order Form</legend>
<div class="control-group">
<label class="control-label" for="select01">Choose a plan</label>
<div class="controls">
<select id="select01" name="plan">
<option>Lite Plan $0 per order</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="itemName">Item Name</label>
<div class="controls">
<input type="text" class="input-xlarge" id="itemName" name="itemName">
<p class="help-block">Item name exapmle: iPad3 White 32GB wifi & 3G.</p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="itemID">Item ID</label>
<div class="controls">
<input type="text" class="input-xlarge" id="itemID" name="itemID">
<p class="help-block">example: Ebay Item ID, Amazon Item ID.</p>
</div><br>
<div class="control-group">
<label class="control-label" for="itemURL">Item URL</label>
<div class="controls">
<input type="text" class="input-xxlarge" id="itemURL" name="itemURL">
<p class="help-block">Direct web link to the item.</p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="textarea">Item Details</label>
<div class="controls">
<textarea class="input-xlarge" id="textarea" name="itemDetails" rows="6"></textarea>
<p class="help-block">Item details (name, color, specifications etc...)</p>
</div>
</div>
<li id="li_3" data-pricefield="money_simple" data-pricevalue="0">
<div class="input-prepend input-append">
<label class="control-label" for="element_3_1">Item Price</label>
<div class="controls">
<span class="add-on">$</span>
<input id="element_3_1" data-price-value="10.00" name="element_3" type="text" class="element text large">
<p class="help-block">Item exact price on the US online store.</p>
</div>
</div>
</li>
<li id="li_7" data-pricefield="money_simple" data-pricevalue="0">
<div class="input-prepend input-append">
<label class="control-label" for="element_7_1">Local Shipping Cost</label>
<div class="controls">
<span class="add-on">$</span>
<input id="element_7_1" data-price-value="10.00" name="element_7" type="text" class="element text large">
</div>
<p class="help-block">Local shipping fee from the US Store to Sky2ship (if applicable).</p>
</div>
</li>
<li id="li_8" data-pricefield="radio" data-pricevalue="0">
<div class="control-group">
<div class="controls">
<p class="help-block">Order Processing Service Fee.</p>
<label class="radio">($0) Standard 2-3 days
<input id="element_8_1" data-pricedef="00.00" name="element_8" class="element radio" type="radio" value="$0 Standard 2-3 Day">
</label>
<label class="radio">($10) Express 1 day
<input id="element_8_2" data-pricedef="10.00" name="element_8" class="element radio" type="radio" value="$10 Express Same Day">
</label>
</div>
</div>
</li>
<legend>Personal Information & Shipping Address</legend>
<div class="control-group">
<label class="control-label" for="input04">Full Name</label>
<div class="controls">
<input type="text" class="input-medium" id="fullName" name="fullName">
<p class="help-block">First & last name.</p>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="radio">Male
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
</label>
<label class="radio">
<input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">Female
</label>
</div>
</div>
<div class="input-prepend">
<label class="control-label" for="prependedInput">Email Address</label>
<div class="controls">
<span class="add-on">#</span>
<input type="text" class="span2" id="prependedInput" name="Email">
<p class="help-block">Your email address.</p>
</div>
</div>
<div class="control-group">
<label class="control-label" for="input06">Address</label>
<div class="controls">
<input type="text" class="input-xxlarge" id="input06" name="streetAddress" placeholder="Street Address">
<p class="help-block">Your shipping address.</p>
</div>
</div>
<div class="control-group">
<div class="controls controls-row">
<input type="text" class="span2" id="City" name="City" placeholder="City">
<input type="text" class="span3" id="State" name="State" placeholder="State / Province">
</div>
</div>
<div class="control-group">
<div class="controls controls-row">
<input type="text" class="span2" id="PostalCode" name="PostalCode" placeholder="Postal Code">
<input type="text" class="span3" id="Phone" name="Phone" placeholder="Phone Number">
</div>
</div>
<div class="control-group">
<label class="control-label" for="select01">Country</label>
<div class="controls">
<select id="select02" name="Country">
<option>IRAQ</option>
<option>JORDON</option>
</select>
</div>
</div>
<li class="total_payment" align="right" data-basetotal="0">
<span>
<h3 class="alert-success">$<var>0</var></h3>
<h5>Total</h5>
</span>
</li>
<div class="control-group">
<label class="control-label" for="optionsCheckbox">Read & Agree</label>
<div class="controls">
<label class="checkbox">
<input type="checkbox" id="optionsCheckbox" value="option1">
I agree to the site's Terms of Service & Privacy Policy.
</label>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Confirm Order</button>
<button type="reset" class="btn">Cancel Order</button>
</div>
</fieldset>
</form>
confirm-order.php page:
<?php
$itemName = $_POST['itemName'];
$plan = $_POST['plan'];
$itemID = $_POST['itemID'];
$itemPrice = $_POST['element_3'];
$processService = $_POST['element_8'];
$itemDetails = $_POST['itemDetails'];
$streetAddress = $_POST['streetAddress'];
$City = $_POST['City'];
$State = $_POST['State'];
$PostalCode = $_POST['PostalCode'];
$Phone = $_POST['Phone'];
$Country = $_POST['Country'];
$fullName = $_POST['fullName'];
$Email = $_POST['Email'];
$itemURL = $_POST['itemURL'];
$itemLocalShipCost = $_POST['element_7'];
?>
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "000000";
$db_name = "dbname";
if (isset($_POST['submit'])) {
$db_connect = mysqli_connect($db_host,$db_user,$db_pass,$db_name);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql ="INSERT INTO lite_order (lite_plan, lite_item_name)
VALUES
('$plan','$item')";
if (!mysqli_query($db_connect,$sql))
{
die('Error: ' . mysqli_error($db_connect));
}
echo "1 record added";
}
?>
<address>
<strong>Shipping Address.</strong><br>
<?php echo $streetAddress; ?><br>
<?php echo $City; ?>, <?php echo $State; ?>, <?php echo $PostalCode; ?><br><?php echo $Country; ?><br>
<abbr title="Phone">P:</abbr><?php echo $Phone; ?>
</address>
<address>
<strong><?php echo $fullName; ?></strong><br>
<?php echo $Email; ?>
</address>
<table class="table">
<thead>
<tr>
<th>Plan</th>
<th>Item Name</th>
<th>Item ID</th>
<th>Local Shipping Cost</th>
<th>Item Price</th>
<th>Order Processing Fee</th>
</tr>
</thead>
<tbody>
<tr class="success">
<td><?php echo $plan; ?></td>
<td><?php echo $itemName; ?></td>
<td><?php echo $itemID; ?></td>
<td><?php echo "$" . $itemLocalShipCost; ?></td>
<td><?php echo "$" . $itemPrice; ?></td>
<td><?php echo $processService; ?></td
></tr>
</tbody>
</table>
<strong>Item URL</strong><p class="alert alert-info"><?php echo $itemURL; ?></p>
<pre class="pre-scrollable"><?php echo $itemDetails; ?></pre>
<p>Your Total <h3 class="question"><?php echo "$" . $orderTotal; ?></h3></p>
<div class="form-actions"><form action="pending-order.php" method="post" name="confirmed-order">
<button type="submit" name="submit" class="btn btn-primary">Submit Order</button>
<button type="button" class="btn">Previous</button></form>
</div>
</div>
</div>
</div>
</div>
</div>
Whereto insert the mysql database code to insert all the variables into database after the submit button is clicked? where to place the isset() function? i tried it, it didn't insert any data into my table.
EDIT: a simple example
do <form>, validation and inserting in one file, say form.php:
<? // check if FORM has been posted
$posted = isset($_POST['submit']);
if ($posted) { // form has been posted...
// validate input
if (!isset($_POST['item']) || strlen(trim($_POST['item'])) == 0)
$error['item'] = "please insert an item-name!";
if (!isset($_POST['price']) || !is_numeric($_POST['price']))
$error['price'] = "please enter a valid price!";
// ready for input?
if (!isset($error)) { // no $error --> go insert!
// I'll do the db-operation with PDO and a prepared statement.
// this is cool, easy and safe. LEARN IT!
$sql = "INSERT INTO table (item,price) VALUES (:item,:price)";
$insert = $db->prepare($sql);
$insert->execute(array(
':item' => $_POST['item'],
':price' => $_POST['price']
));
} // $error
} // submit
?>
Now, in the <body> of the same page...
<? // check whether to display confirmation or form...
if ($posted && !isset($error)) {
// form was sent AND no error --> confirm
?>
<h1>Confirmed!</h1>
<p>Your data has been sent, thank you very much!</p>
go to somepage
<?
} else {
// form not sent or errors --> display form
?>
<h1>Please enter data</h1>
<? // display error-message, if there's one:
if (isset($error)) {
$output = "";
foreach ($error as $field => $msg)
$output .= (strlen($output) > 0?', ':'') . "[$field]: $msg";
echo "<p>There were errors: $output</p>";
} // $error
?>
<form method="post">
<!-- if the form has been sent, bring back the field's value from $_POST -->
<p>item-name: <input type="text" name="item"
value="<?=($posted?$_POST['item']:'')?>" /></p>
<p>price: <input type="text" name="price"
value="<?=($posted?$_POST['price']:'')?>" /></p>
<p><input type="submit" name="submit" value="submit" /></p>
</form>
<?
} // submit & $error
?>
See the use of a ternary-operator for setting the value-attribute of the <input>-elements:
(<condition>?<what to do if true>:<what to do if false>)
There are two specific things I can contribute.
First, isset tests for null... which is different than empty. If you have a form field that is submitted empty, then set a local variable to that posted value, then test it with isset; isset will return true because the value exists which is different than the variable not having been registered in the page load at all.
Second... ANYTHING can post to your form (think evil autonomous Korean hacker bots). Also, there are many ways a form can get submitted without having activated the submit button itself so there is no guarantee you will even see a submit key in your $_POST vars. What you need to define in your processing script is a "default action". What I mean by that is a very basic and SAFE behavior (like redirecting to a 'something is wrong' page) that kicks off by default such that the only way around it is to submit a correct form with all anticipated values correctly set.
If you do this, you can ignore the value of the submit button itself and instead focus on the contents of the POST. Did I receive everything I expected to receive? Was it all in the correct format? Was the user authenticated correctly? Only after all these questions have been tested to your satisfaction would you switch from the default behavior to a form processing behavior in which the posted data can be inserted into your database.
Example using your 3 page structure:
reference: filter vars
Page 1:
<form action=./page2 method=POST>
<input type=text value=1234 name=numericValue />
<input type=text value="dummytext" name=stringValue />
<input type=submit value=submit name=submit />
</form>
Page 2:
<?php
$args = array('numericValue' => FILTER_VALIDATE_INT
,'stringValue' => FILTER_SANITIZE_STRING);
$clean_data = filter_input_array(INPUT_POST,$args);
if (is_array($clean_data))
{
$_SESSION["saved_clean_data"] = $clean_data;
}
else
{
Header(<something wrong page>);
die();
}
?>
<form action=./page3 method=POST>
<input type=submit name=submit value=No />
<input type=submit name=submit value=Yes />
</form>
Page 3:
<?php
if ($_POST["submit"] === "Yes")
{
$cleanNum = $_SESSION["saved_clean_data"]["numericValue"];
$cleanStr = $_SESSION["saved_clean_data"]["stringValue"];
// DB insert Query, use advice from michi about PDO
// parameterize your queries to help prevent sql injection
}
else
{
Header(<somewhere for declined submits>);
die();
}
?>
Well we can do this in the following ways
You store all the data in session and use it in confirmation page and then on data insertion page. Do remember to update or delete it if user updates or cancel the order.
You can dynamically create the confirm order page using javascript and HTML and when user clicks confirm button then only we post it to the PHP page. This will also reduce a server call.
One other ways is to again send the collected posted values and keep it as hidden fields in the confirmation page and post it when clicked confirm.
create a form and store variables in hidden fields , then create this submit button in the form
So clicking this form will store the info. See the exmple here
<form class="form-horizontal well" action="confirm-order.php" method="POST">
<input type="hidden" value="<?php echo $itemName; ?>" />
<input type="submit" value="Confirm Order" />
</form>
Well there are couple of ways about doing this:
Store all the data from the previous page i.e. from order.php in the $SESSION[] variables:
Explaination: Setting it in Session will enable you to access the same variable from anywhere in the site until the session of the user. Means that after you store it in session you can access it in pending-order.php page.
How to do it: In this page at the top, instead of setting the variables at top write the following:
$SESSION['itemName'] = $_POST['itemName']
then echo it using:
echo $SESSION['itemName']
and then in the pending-order.php you can assign a value to a variable like so:
$itemName = $SESSION['itemName']
and now you can store the variable in the db.
Put hidden fields inside the form of confirm-order.php page:
Explaination: Create hidden input fields in confirm-order.php form and set the values that are in the variables. This way when you click the submit button you can access them in pending-order.php in the same way you are doing on confirm-order.php.
How to do it: Simply put the variables in value attribute of the hidden input like so:
<form action="pending-order.php" method="post" name="confirmed-order">
<input type="hidden" value="<?php $itemID ?>" id="someID">
</form>
Try
<button type="submit" class="btn btn-primary" NAME="submit">Confirm Order</button>
And use
IF (isset($_POST['submit]) {
$itemName = $_POST['itemName'];
$plan = $_POST['plan'];
$itemID = $_POST['itemID'];
$itemPrice = $_POST['element_3'];
$processService = $_POST['element_8'];
$itemDetails = $_POST['itemDetails'];
$streetAddress = $_POST['streetAddress'];
$City = $_POST['City'];
$State = $_POST['State'];
$PostalCode = $_POST['PostalCode'];
$Phone = $_POST['Phone'];
$Country = $_POST['Country'];
$fullName = $_POST['fullName'];
$Email = $_POST['Email'];
$itemURL = $_POST['itemURL'];
$itemLocalShipCost = $_POST['element_7'];
// your mysql INSERT codes here
}
EDIT 1:
change <button type="submit" class="btn btn-primary">Confirm Order</button>
TO <input type="submit" class="btn btn-primary" value="Confirm Order">
isset() function work when the input field type is submit.like
<input type="submit" value="Confirm Order" />
so update the code form
<div class="form-actions">
<button type="submit" class="btn btn-primary">Confirm Order</button>
<button type="reset" class="btn">Cancel Order</button>
</div>
to
<div class="form-actions">
<input type="submit" class="btn btn-primary" value="Confirm Order" />
<input class="btn" type="reset" value="Cancel Order" />
</div>

Categories