AngularJS and PHP entering blank rows in database - php

I'm trying to figure out why my data is not being collected by my PHP files.
This is my index.html
<!DOCTYPE html>
<html lang="en" ng-app="gemStore">
<head>
<meta http-equiv='Content-Type':'application/x-www-form-urlencoded; charset=UTF-8' />
<title>Angular</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div ng-controller="custController">
<form>
First Name: <input type="text" ng-model="firstname"><br>
Last Name: <input type="text" ng-model="lastname"><br>
Choose Username: <input type="text" ng-model="username"><br>
Choose Password: <input type="text" ng-model="password"><br>
Address 1: <input type="text" ng-model="address1"><br>
Address 2: <input type="text" ng-model="address2"><br>
City: <input type="text" ng-model="city"><br>
State: <input type="text" ng-model="state"><br>
Zip: <input type="number" ng-model="zip"><br>
<input type="button" value="Submit" ng-click="insertdata()"><br>
</form>
</div>
<script src="js/angular.min.js"></script>
<script src="js/script.js"></script>
<script src="js/products.js"></script>
</body>
</html>
This is my script.js
(function(){
var app = angular.module('gemStore', ['store-products']);
app.controller('StoreController', ['$http', function($http){
var store = this;
store.products = [];
$http.get('/store-products.json').success(function(data){
store.products = data;
});
}]);
app.controller('custController', function($scope,$http){
$scope.insertdata = function(){
$http.post('insert.php',{'firstname':$scope.firstname,'lastname':$scope.lastname,'address1':$scope.address1,'address2':$scope.address2, 'city':$scope.city,'state':$scope.state,'zip':$scope.zip,'username':$scope.username,'password':$scope.password})
.success(function(data,status,headers,config){
console.log('data inserted successfully. First Name: ' + $scope.firstname);
});
}
});
})();
And finally my PHP file
<?php
$data = json_decode(file_get_contents("php://input"));
$firstname = mysql_real_escape_string($data->firstname);
$lastname = mysql_real_escape_string($data->lastname);
$username = mysql_real_escape_string($data->username);
$address1 = mysql_real_escape_string($data->address1);
$address2 = mysql_real_escape_string($data->address2);
$city = mysql_real_escape_string($data->city);
$state = mysql_real_escape_string($data->state);
$zip = mysql_real_escape_string($data->zip);
$password = mysql_real_escape_string($data->password);
mysql_connect("localhost","root","");
mysql_select_db("mydatabase");
mysql_query("INSERT INTO users (`firstname`, `lastname`, `username`, `password`, `address1`, `address2`, `city`, `state`, `zip`)VALUES('" . $firstname . "','" . $lastname . "','" . $username . "','" . $password . "','" . $address1 . "','" . $address2 . "','" . $city . "','" . $state . "','" . $zip . "')");
?>
I am thinking it has something to do with headers, but I'm not sure where I should add it or what needs to be changed. Any help would be appreciated. Thanks.
Also, I am using angular 1.3.16, but I doubt that makes a difference.

try this in your insertdata function.
$scope.insertdata = function(){
var data = {'firstname':$scope.firstname,'lastname':$scope.lastname,'address1':$scope.address1,'address2':$scope.address2, 'city':$scope.city,'state':$scope.state,'zip':$scope.zip,'username':$scope.username,'password':$scope.password};
$http({
url:'insert.php',
method: 'post',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data :data
}).success(function(data,status,headers,config){
console.log('data inserted successfully. First Name: ' + $scope.firstname);
});
}

Related

it create a record but it is empty

well I am trying to insert user data and I made these codes , the record is being created but there is no data in it , just to clarify I have include the files and I am connected to the database and I think I declared all the necessary objects , if you know what is wrong I would appreciate a bit more explaining I am a beginner
addUser.php
<div id="message" class="flex-row align-center flex-nowrap">
<h4 class="heading flex-1"></h4>
</div>
<div class="flex-row form-row">
<div class="flex-column create-session-sidebar__column">
<form class="form" method="post">
<div class="Faseeh-form-input outlined"><label class="label">الاسم الاول</label>
<input id="fname" placeholder="" type="text" value=""></div>
<div class="Faseeh-form-input outlined"><label class="label">الاسم الثاني</label>
<input id="lname" placeholder="" type="text" value=""></div>
<div class="Faseeh-form-input outlined"><label class="label">اسم المستخدم</label>
<input id="username" placeholder="" type="text" value=""></div>
<div class="Faseeh-form-input outlined"><label class="label">البريد الالكتروني</label>
<input id="email" placeholder="" type="email" value=""></div>
<div class="Faseeh-form-input outlined"><label class="label"> الالكتروني</label>
<input id="password" placeholder="" type="password" value=""></div>
<div class="flex-row form-row">
<button id="addUser" class="Faseeh-btn Faseeh-btn Faseeh-btn-primary" type="submit">تسجيل</button>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function() {
NewUser();
});
function NewUser() {
$(document).on('click', '#addUser', function() {
var fname = $('#fname').val();
var lname = $('#lname').val();
var username = $('#username').val();
var email = $('#email').val();
var password = $('#password').val();
var data = "fname=" + fname + "&lname=" + lname + "&username=" + username + "&email=" + email + "&password=" + password;
if(fname == "" || lname == "" || username == "" || email == "" || password == ""){
$('#message').html('Please fill in the blanks');
}else{
$.ajax ({
url: 'core/register.php?',
type:'post',
data: data,
success: function(data) {
$("#message").html(data);
}
});
}
});
}
</script>
and this is register.php
<?php
include_once "util.php";
session_start();
$util = new util();
$post = $_POST['data'];
$data = json_decode($post);
$result = $util->newUser($data);
if($result) {
echo 1;
}else {
echo 0;
}
and finally util.php
public function newUser($data){
// Our database object
$db = new dbhandler();
// Quote and escape form submitted values
$fname = $db -> quote($fname->fname);
$lname = $db -> quote($lname->lname);
$username = $db -> quote($username->username);
$email = $db -> quote($email->email);
$password = $db -> quote($password->password);
// Insert the values into the database
$result = $db -> query("INSERT INTO `users` (`fname`,`lname`,`username`,`email`,`password`) VALUES (" . $fname . "," . $lname . "," . $username . "," . $email . "," . $password . ");");
}
Almost! 2 problems though.
You are sending in the data as a url format, but you are reading it as a JSON format. Instead of
var data = "fname=" + fname + "&lname=" + lname + "&username=" + username + "&email=" + email + "&password=" + password;
You need to create an object with:
var data = array(
"fname": fname,
"lname": lname,
"username": username,
"email": email,
"password": password
}
You need to get the variables from inside the $data object you passed in.
public function newUser($data){
// Our database object
$db = new dbhandler();
// Quote and escape form submitted values
$fname = $db -> quote($data->fname);
$lname = $db -> quote($data->lname);
$username = $db -> quote($data->username);
$email = $db -> quote($data->email);
$password = $db -> quote($data->password);
// Insert the values into the database
$result = $db -> query("INSERT INTO `users` (`fname`,`lname`,`username`,`email`,`password`) VALUES (" . $fname . "," . $lname . "," . $username . "," . $email . "," . $password . ");");
}

How to open link after the webpage receive POST call in PHP?

I have an application to POST data to my webpage. My webpage should use POSTed data to generate a link with parameters and open it.
My webpage has these PHP codes
if(isset($_POST['name']) && isset($_POST['surname']))
{
$name = $_POST['name'];
$surname = $_POST['surname'];
header('Location: http://localhost/smcreader/tktest.php?name=' . $name . '&surname=' . $surname);
}
My problem is it won't open generated URL. I replace header with echo and add MessageBox.Show(response_stuff) to my app to show response message. It response normally, STATUS is OK, and the response message is a generated link.
I think that it isn't the right approach for redirect a page dynamically builded. Have you tried an AJAX function?
index.php
<html>
<head>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#send").click(function(){
var name = $("#name").val();
var surname = $("#surname").val();
$.ajax({
type: "POST",
url: "GenUrl.php",
data: "name=" + name + "&surname=" + surname,
dataType: "text",
success: function(url)
{
if (url != "KO") {
window.location = url
}
else
{
$("#result").html("Check Values");
}
},
error: function()
{
alert("Error......");
}
});
});
});
</script>
<body>
<form name="modulo">
<p>Name</p>
<p><input type="text" name="name" id="name"></p>
<p>Surname</p>
<p><input type="text" name="surname" id="surname"></p>
<input type="button" id="send" value="GO">
</form>
<div id="result"></div>
</body>
</html>
GenUrl.php
<?php
if(!empty($_POST['name']) && !empty($_POST['surname']))
{
$name = $_POST['name'];
$surname = $_POST['surname'];
echo "http://localhost/smcreader/tktest.php?name=" . $name . "&surname=" . $surname;
}
else
{
echo "KO";
}
?>

Error in adding details by Ajax

Hello frndz i need a help i am trying to add detail by my form but no getting any value..and error is reflecting as "add request fails"..can anyone solve my error i am not getting what to do for this.. ther is my code
webapp.js
// Add company button
$(document).on('click', '#add_employee', function(e){
e.preventDefault();
$('.lightbox_content h2').text('Add Employee');
$('#form_employee button').text('Add');
$('#form_employee').attr('class', 'form add');
$('#form_employee').attr('data-id', '');
$('#form_employee .field_container label.error').hide();
$('#form_employee .field_container').removeClass('valid').removeClass('error');
$('#form_employee #ID').val('');
$('#form_employee #Name').val('');
$('#form_employee #Lastname').val('');
$('#form_employee #Email').val('');
$('#form_employee #Username').val('');
$('#form_employee #Password').val('');
$('#form_employee #Mobile').val('');
$('#form_employee #Website').val('');
show_lightbox();
});
// Add company submit form
$(document).on('submit', '#form_employee.add', function(e){
e.preventDefault();
// Validate form
if (form_employee.valid() == true){
// Send company information to database
hide_ipad_keyboard();
hide_lightbox();
show_loading_message();
var form_data = $('#form_employee').serialize();
var request =
$.ajax({
url: 'data.php',
cache: false,
data: {job:"add_employee",form_data},
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'get'
});
request.done(function(output){
if (output.result == 'success'){
// Reload datable
table_employee.api().ajax.reload(function(){
hide_loading_message();
var Name = $('#Name').val();
show_message("Employee Name '" + Name + "' added successfully.", 'success');
}, true);
} else {
hide_loading_message();
show_message('Add request failed', 'error');
}
});
request.fail(function(jqXHR, textStatus){
hide_loading_message();
show_message('Add request failed: ' + textStatus, 'error');
});
}
});
data.php
<?php
// Database details
$db_server = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'example1';
// Get job (and id)
$job = '';
$id = '';
if (isset($_GET['job'])){
$job = $_GET['job'];
if ($job == 'get_employee' ||
$job == 'get_employee_detail' ||
$job == 'add_employee' ||
$job == 'edit_employee' ||
$job == 'delete_employee'){
if (isset($_GET['id'])){
$id = $_GET['id'];
if (!is_numeric($id)){
$id = '';
}
}
} else {
$job = '';
}
}
// Prepare array
$mysql_data = array();
// Valid job found
if ($job != ''){
// Connect to database
$db_connection = mysqli_connect($db_server, $db_username, $db_password, $db_name);
if (mysqli_connect_errno()){
$result = 'error';
$message = 'Failed to connect to database: ' . mysqli_connect_error();
$job = '';
}
if ($job == 'add_employee'){
// Add company
$query = "INSERT INTO employees SET ";
if (isset($_GET['ID'])) { $query .= "ID = '" . mysqli_real_escape_string($db_connection, $_GET['ID']) . "', "; }
if (isset($_GET['Name'])) { $query .= "Name = '" . mysqli_real_escape_string($db_connection, $_GET['Name']) . "', "; }
if (isset($_GET['Lastname'])) { $query .= "Lastname = '" . mysqli_real_escape_string($db_connection, $_GET['Lastname']). "', "; }
if (isset($_GET['Email'])) { $query .= "Email = '" . mysqli_real_escape_string($db_connection, $_GET['Email']) . "', "; }
if (isset($_GET['Username'])) { $query .= "Username = '" . mysqli_real_escape_string($db_connection, $_GET['Username']). "', "; }
if (isset($_GET['Password'])) { $query .= "Password = '" . mysqli_real_escape_string($db_connection, $_GET['Password']). "', "; }
if (isset($_GET['Mobile'])) { $query .= "Mobile = '" . mysqli_real_escape_string($db_connection, $_GET['Mobile']) . "', "; }
if (isset($_GET['Website'])) { $query .= "Website = '" . mysqli_real_escape_string($db_connection, $_GET['Website']) . "'"; }
$query = mysqli_query($db_connection, $query);
if (!$query){
$result = 'error';
$message = 'add Employee error';
} else {
$result = 'success';
$message = 'Employees added success';
}
// Close database connection
mysqli_close($db_connection);
}
// Prepare data
$data = array(
"result" => $result,
"message" => $message,
"data" => $mysql_data
);
// Convert PHP array to JSON array
$json_data = json_encode($data);
print $json_data;
?>
**index.html**
<!doctype html>
<html lang="en" dir="ltr">
<head>
<title>Table</title>
<meta charset="utf-8">
<meta name="viewport" content="width=1000, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Oxygen:400,700">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="design.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script charset="utf-8" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script charset="utf-8" src="//cdn.datatables.net/1.10.0/js/jquery.dataTables.js"></script>
<script charset="utf-8" src="http://cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.min.js"></script>
<script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"> </script>
<script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/additional-methods.min.js"></script>
<script charset="utf-8" src="webapp.js"></script>
</head>
<body>
<div id="page_container">
<h1>Details of Employees</h1>
<button type="button" class="button" id="add_employee">Add Employees</button>
<table class="datatable" id="table_employee">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Lastname</th>
<th>Email</th>
<th>Username</th>
<th>Password</th>
<th>Mobile No</th>
<th>Website</th>
<th>Functions</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div class="lightbox_bg"></div>
<div class="lightbox_container">
<div class="lightbox_close"></div>
<div class="lightbox_content">
<h2>Add Employees</h2>
<form class="form add" id="form_employee" data-id="" novalidate>
<div class="input_container">
<label for="Name">Name: <span class="required">*</span></label>
<div class="field_container">
<input type="text" class="text" name="Name" id="Name" value="" required>
</div>
</div>
<div class="input_container">
<label for="Lastname">Lastname: <span class="required">*</span></label>
<div class="field_container">
<input type="text" class="text" name="Lastname" id="Lastname" value="" required>
</div>
</div>
<div class="input_container">
<label for="Email">Email: <span class="required">*</span></label>
<div class="field_container">
<input type="text" class="text" name="Email" id="Email" value="" required>
</div>
</div>
<div class="input_container">
<label for="Username">Username: <span class="required">*</span></label>
<div class="field_container">
<input type="text" class="text" name="Username" id="Username" value="" required>
</div>
</div>
<div class="input_container">
<label for="Password">Password: <span class="required">*</span></label>
<div class="field_container">
<input type="password" class="text" name="Password" id="Password" value="" placeholder="eg. X8df90EO" required>
</div>
</div>
<div class="input_container">
<label for="Mobile">Mobile: <span class="required">*</span></label>
<div class="field_container">
<input type="text" class="text" name="Mobile" id="Mobile" maxlength="10" pattern="[7-9]{1}[0-9]{9}" placeholder="Only 10 digit Mobile no"required>
</div>
</div>
<div class="input_container">
<label for="Website">Website: <span class="required">*</span> </label>
<div class="field_container">
<input type="text" class="text" name="Website" id="Website" value="" placeholder="https://www.domain.com" required>
</div>
</div>
<div class="button_container">
<button type="submit">Add Employees</button>
</div>
</form>
</div>
</div>
<div id="message_container">
<div id="message" class="success">
<p>This is a success message.</p>
</div>
</div>
<div id="loading_container">
<div id="loading_container2">
<div id="loading_container3">
<div id="loading_container4">
Loading, please wait...
</div>
</div>
</div>
</div>
</body>
</html>
Change your submit HTML code to <input type="submit" value="Add Employees"></input>
And use my javascript source
<script type="text/javascript">
$( "#form_employee" ).submit(function( event ) {
var data = $(this).serializeArray();
data.push(
{name: "job", value: "add_employee"}
);
data = JSON.stringify(data);
$.ajax({
type: "POST",
url: "jsOnChange.php", //Set-Your-URL-Here
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
error: function(e)
{
alert(JSON.stringify(e, null, 4));
},
success: function(strDrivers){
alert(JSON.stringify(strDrivers, null, 4));
}
});
});
</script>
In .php listener
<?php
ini_set("allow_url_fopen", true);
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
echo $jsonStr;
?>
You will get
Hope this help!!!
I think you code have some mistake at this place
var form_data = $('#form_employee').serialize();
var request =
$.ajax({
url: 'data.php',
cache: false,
data: {job:"add_employee",form_data},
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'get'
});
Replace it with
var form_data = $('#form_employee').serialize();
form_data.job='add_employee';
var request =
$.ajax({
url: 'data.php',
cache: false,
data: form_data,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'get'
});
Also in PHP side before print json_encode string add ob_clean() because you have mentioned dataType: json in ajax request.
ob_clean();
// Convert PHP array to JSON array
$json_data = json_encode($data);
print $json_data;

data is not inserting into database

So I'm thinking there is an issue with the serialization part of the jquery ajax. It's not putting any information into the database and I have no idea why! Obviously the variables from the input controls on the webpage are not being passed into the php processing page. What am I doing wrong here?? Need some help here with this folks! This is all very new to me.
Webpage:
<form name="main_form" id="main_form" method="post">
<div id="ChangeAddressDialog" title="Change of Address">
<p>Mailing Address: <input type="text" id="Address1" name="Address1" /></p>
<p>Mailing Address 2: <input type="text" id="Address2" name="Address2" /></p>
<p>City: <input type="text" id="City" name="City" /></p>
<p>State: <input type="text" id="State" name="State" maxlength="2" /></p>
<p>Zip Code: <input type="text" id="Zip" id="Zip" maxlength="10" /></p>
<p>Country: <input type="text" id="County" name="Country" /></p>
<input type="hidden" id="change_of_address_form" name="change_of_address_form" />
</div>
</form>
$('#ChangeOfAddress').click(function() {
//change of address dialog
$( "#ChangeAddressDialog" ).dialog({
width:500,
modal:true,
closeOnEscape:true,
buttons: [
{ text: "Ok", type: "submit", click: function() {
$.ajax({
url: "classes/add-address.php",
type: "POST",
data: $("#main_form").serialize(),
dataType: 'json',
error: function(SMLHttpRequest, textStatus, errorThrown){
alert("An error has occurred making the request: " + errorThrown)
},
success: function(result){
//do stuff here on success such as modal info
//$("#main_form").submit();
$(this).dialog("close");
}
})
}
},
{ text: "Close", click: function() { $(this).dialog( "close" ); } } ]
});//end dialog
});
PHP processing page:
<?php
require_once('../config.php');
//$sqlCheck = '';
$parcel_id = isset($_POST['ParcelId']) ? $_POST['ParcelId'] : null;
$address1 = isset($_POST['Address1']) ? $_POST['Address1'] : null;
$address2 = isset($_POST['Address2']) ? $_POST['Address2'] : null;
$city = isset($_POST['City']) ? $_POST['City'] : null;
$state = isset($_POST['State']) ? $_POST['State'] : null;
$zip = isset($_POST['Zip']) ? $_POST['Zip'] : null;
$country = isset($_POST['Country']) ? $_POST['Country'] : null;
$db = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$result = $db->query("INSERT INTO change_of_address (parcel_id, address_1, address_2, City, State, Zip, Country) VALUES ('" . $parcel_id . "','" . $address1 . "','" . $address2 . "','" . $city . "','" . $state . "','" . $zip . "','" . $country . "')");
if ($result == 1) {
echo '{"success":true}';
} else {
echo '{"success":false}';
}
//$sqlCheck = "INSERT INTO change_of_address (parcel_id, address_1, address_2, City, State, Zip, Country) VALUES ('" . $parcel_id . "','" . $address1 . "','" . $address2 . "','" . $city . "','" . $state . "','" . $zip . "','" . $country . "')";
//echo json_encode($sqlCheck);
?>
data: $("#main_form").serialize(),
The form element is named main_form. Your jquery selector is looking for an id. You can just change name="main_form" to id="main_form" on your form tag and it should fix it.

cannot save date in mysql datatase with php

I have a problem with date in php
even if I fill the textbox of the date , in the database I find it empty
here is my php page :
<?php
session_start();
if (!array_key_exists("user", $_SESSION)) {
header('Location: index.php');
exit;
}
require_once("Includes/db.php");
$wisherID = WishDB::getInstance()->get_wisher_id_by_name($_SESSION['user']);
$wishDescriptionIsEmpty = false;
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (array_key_exists("back", $_POST)) {
header('Location: editWishList.php');
exit;
} else
if ($_POST['wish'] == "") {
$wishDescriptionIsEmpty = true;
} else if ($_POST["wishID"] == "") {
WishDB::getInstance()->insert_wish($wisherID, $_POST["wish"], $_POST["dueDate"]);
header('Location: editWishList.php');
exit;
} else if ($_POST["wishID"] != "") {
WishDB::getInstance()->update_wish($_POST["wishID"], $_POST["wish"], $_POST["dueDate"]);
header('Location: editWishList.php');
exit;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" href="jquery-ui-1.8.24.custom/css/smoothness/jquery-ui-1.8.24.custom.css" rel="stylesheet" />
<script type="text/javascript" src="jquery-ui-1.8.24.custom/js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.24.custom/js/jquery-ui-1.8.24.custom.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.24.custom\development-bundle\ui\i18n\jquery.ui.datepicker-fr.js"></script>
<script type="text/javascript">
$(function() {
$.datepicker.setDefaults( $.datepicker.regional[ "" ] );
$( "#datepicker" ).datepicker( $.datepicker.regional[ "fr" ] );
});
</script>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
$wish = array("id" => $_POST["wishID"], "description" => $_POST["wish"], "due_date" => $_POST["dueDate"]);
else
if (array_key_exists("wishID", $_GET))
$wish = mysqli_fetch_array(WishDB::getInstance()->get_wish_by_wish_id($_GET["wishID"]));
else
$wish = array("id" => "", "description" => "", "due_date" => "");
?>
<form name="editWish" action="editWish.php" method="POST">
<input type="hidden" name="wishID" value="<?php echo $wish["id"]; ?>" />
<table>
<tr>
<td>Describe your wish:</td>
<td><input type="text" name="wish" value="<?php echo $wish['description']; ?>" /></td>
<td><?php if ($wishDescriptionIsEmpty) echo "Please enter description"; ?></td>
</tr>
<tr>
<td>When do you want to get it?</td>
<td><input type="text" name="due_date" id="datepicker" value="<?php echo $wish['due_date']; ?>" /></td>
</tr>
</table>
<input type="submit" name="saveWish" value="Save Changes"/>
<input type="submit" name="back" value="Back to the List"/>
</form>
je suis
</br>
</body>
</html>
and here is the coresponding method in db.php :
function insert_wish($wisherID, $description, $dueDate) {
$description = $this->real_escape_string($description);
if ($duedate == '') {
$this->query("INSERT INTO wishes (wisher_id, description)" .
" VALUES (" . $wisherID . ", '" . $description . "')");
} else
$this->query("INSERT INTO wishes (wisher_id, description, due_date)" .
" VALUES (" . $wisherID . ", '" . $description . "', '" . $dueDate . "')");
}
public function update_wish($wishID, $description, $duedate) {
$description = $this->real_escape_string($description);
if ($duedate == null) {
$this->query("UPDATE wishes SET description = '" . $description . "', due_date = NULL WHERE id = " . $wishID);
} else
$this->query("UPDATE wishes SET description = '" . $description . "', due_date = '" . $duedate . "' WHERE id = " . $wishID);
}
I use the datepicker query component for date
can you detect me the location of the error
thanks
I think you have given a wrong name to input element. Replace below
<input type="text" name="due_date" id="datepicker"
value="<?php echo $wish['due_date']; ?>" />
With
<input type="text" name="dueDate" id="datepicker"
value="<?php echo $wish['due_date']; ?>" />
You are using $_POST["dueDate"] to get date value and the name is incorrect in your markup.
Edit ::
As #simonTifo said in comment "it return me the exact date, bit in the datatabase it saves like 00-00-0000", there might be some format related issue to overcome this problem just use the date function in php. So the code suppose to be :
WishDB::getInstance()->insert_wish($wisherID, $_POST["wish"],
date('Y-m-d H:i:s', $_POST["dueDate"]));
Check that function manual and set whatever format according to your need.
Hope this will help !!

Categories