表没有使用Ajax和Mysql更新

时间:2016-05-07 13:21:43

标签: php html mysql ajax

我一直在搜索其他主题以找到解决方案,但没有。 我有表单,我想在单击更新按钮时更新表单输入值。我在Ajax中使用的代码是

$("#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);                        
                        }
        });
    });

用于更新值的PHP文件是:

<?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>';

?>

任何帮助表示赞赏

2 个答案:

答案 0 :(得分:2)

通过ajax发送的数据未正确形成。它应该是:

var data = {'surname':surname,'name':name,'company_name':company_name .....}

而不是姓:'姓'

答案 1 :(得分:1)

问题是由于您的AJAX请求中的以下行,

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'}

您实际上正在发送surnamecompany_name等字符串,而不是变量的值。所以删除那些单引号。它应该是,

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}

旁注:不要使用mysql_数据库扩展,它们在PHP 5.5.0中已弃用,在PHP 7.0.0中完全删除。请改用mysqliPDO扩展程序。这是why you shouldn't use mysql_ functions