Aristain Aristain - 6 months ago 18
SQL Question

Table not updated with Ajax and Mysql

I ve been searching through other topics to find a solution for that but nothing .
I have form and i want to update the form input values when update button is clicked . The code that i using in Ajax is

$("#updateit").click(function() {

var surname = $("#search_text").val();
var name = $("#name").val();
var company_name = $("#company_name").val();
var firm = $("#firm").val();
var address = $("#address").val();
var town = $("#town").val();
var tk = $("#tk").val();
var country = $("#country").val();
var telephone = $("#telephone").val();
var fax = $("#fax").val();
var mobile = $("#mobile").val();
var web_site = $("#web_site").val();
var visitors = $("#visitors").val();
var id = $("#id").val();
$.ajax({
url: 'update1.php',
type: 'POST',
data: {surname:'surname',name:'name',company_name:'company_name',firm:'firm',address:'address',town:'town',tk:'tk',country:'country',telephone:'telephone',fax:'fax',mobile:'mobile',mail:'mail',web_site:'web_site',visitors:'visitors',id:'id'} ,
dataType:'html',
success: function(data)
{
alert(data);
}
});
});


The PHP file for updating values is this :

<?php
require('db.php');
include("auth.php");
date_default_timezone_set('Europe/Athens');
$id=$_POST['id'];
$surname =$_POST['surname'];
$name= $_POST['name'];
$company_name=$_POST['company_name'];
$firm= $_POST['firm'];
$address= $_POST['address'];
$town= $_POST['town'];
$tk= $_POST['tk'];
$country= $_POST['country'];
$telephone= $_POST['telephone'];
$fax= $_POST['fax'];
$mobile= $_POST['mobile'];
$mail= $_POST['mail'];
$web_site= $_POST['web_site'];
$visitors= $_POST['visitors'];

$update="update base set surname='".$surname."', name='".$name."',company_name='".$company_name."',firm='".$firm."',address='".$address."',town='".$town."',tk='".$tk."',country='".$country."',telephone='".$telephone."',fax='".$fax."',mobile='".$mobile."',web_site='".$web_site."',visitors='".$visitors."' where id='".$id."'";
mysql_query($update) or die(mysql_error());
$status = "Record Updated Successfully. </br></br><a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';

?>


Any Help appreciated

Answer

The problem is because of the following line in your AJAX request,

data: {surname:'surname',name:'name',company_name:'company_name',firm:'firm',address:'address',town:'town',tk:'tk',country:'country',telephone:'telephone',fax:'fax',mobile:'mobile',mail:'mail',web_site:'web_site',visitors:'visitors',id:'id'}

you're actually sending strings like surname, company_name etc. rather than the value of the variables. So remove those single quotes. It should be,

data: {surname:surname,name:name,company_name:company_name,firm:firm,address:address,town:town,tk:tk,country:country,telephone:telephone,fax:fax,mobile:mobile,mail:mail,web_site:web_site,visitors:visitors,id:id}

Sidenote: Don't use mysql_ database extensions, they were deprecated in PHP 5.5.0 and are removed altogether in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_ functions.

Comments