Php 使用Update CONCAT将数据添加到数据库中已有的数据

Php 使用Update CONCAT将数据添加到数据库中已有的数据,php,mysql,Php,Mysql,我试图在数据库内进行程序更新,但我有一个担心。 当我向mysql服务器发出update语句时,我希望它向mysql数据库添加另一个数据,它只是覆盖那里的第一个数据,并将新数据保存到数据库中。我希望它做的是向数据库中添加另一个数据,再加上已经存在的数据 我的代码如下所示 <?php session_start(); require_once('inc/config.php'); if(!isset($_SESSION['username'])){ header('Location:

我试图在数据库内进行程序更新,但我有一个担心。 当我向mysql服务器发出update语句时,我希望它向mysql数据库添加另一个数据,它只是覆盖那里的第一个数据,并将新数据保存到数据库中。我希望它做的是向数据库中添加另一个数据,再加上已经存在的数据

我的代码如下所示

<?php
session_start();
require_once('inc/config.php');
if(!isset($_SESSION['username'])){
    header('Location: signon.php');
}
?>

<?php
require_once('inc/config.php');

$con = mysqli_connect($host, $user,$pass, $db) or die ('Cannot Connect: '.mysqli_error());
//get parameters

$inst_name = mysqli_real_escape_string($con,$_POST['inst_name']);
$inst_name2 = mysqli_real_escape_string($con,$_POST['inst_name2']);
$grade = mysqli_real_escape_string($con,$_POST['grade']);
$study_course = mysqli_real_escape_string($con,$_POST['study_course']);
$qualification = mysqli_real_escape_string($con,$_POST['qualification']);
$other_qualification = mysqli_real_escape_string($con,$_POST['other_qualification']);
$completion_year = mysqli_real_escape_string($con,$_POST['completion_year']);


$data ="inst_name = '".$inst_name."',inst_name2 = '".$inst_name2."',grade = '".$grade."', study_course = '".$study_course."',qualification = '".$qualification."',other_qualification = '".$other_qualification."',completion_year = '".$completion_year."'";


//$sql = "UPDATE user_info SET inst_name = '".$inst_name."',inst_name2 = '".$inst_name2."',grade = '".$grade."', study_course = '".$study_course."',qualification = '".$qualification."',other_qualification = '".$other_qualification."',completion_year = '".$completion_year."' WHERE username = '".$_SESSION['username']."'";

$sql = "UPDATE user_info SET $data=CONCAT($data,inst_name = '".$inst_name."',inst_name2 = '".$inst_name2."',grade = '".$grade."', study_course = '".$study_course."',qualification = '".$qualification."',other_qualification = '".$other_qualification."',completion_year = '".$completion_year."') WHERE username = '".$_SESSION['username']."'";
mysqli_query($con,$sql) or die ('Failed Query: '.mysqli_error($con));
mysqli_close($con);
    header("Refresh:0; url=EDWE.php");
    ?> 


显然我做得不对。

要创建新记录,而不是更新现有记录,请尝试SQL
INSERT
语句,而不是使用
UPDATE
<代码>插入创建记录并
更新
编辑现有记录。

首先从表中为该用户选择现有数据

Select * from user_info WHERE username = '".$_SESSION['username']."'
然后将新数据转换为现有数据 比如说分数

$grade_posted_data = $_POST['grade']);
$grade_existing data = $fetched_data['grade'];
$grade=$grade_existing_data.','.$grade_posted_data;////// using whatever separator you require
然后更新您的查询

$sql = "UPDATE user_info SET $data=CONCAT($data,inst_name = '".$inst_name."',inst_name2 = '".$inst_name2."',grade = '".$grade."'..etc etc....) WHERE username = '".$_SESSION['username']."'";

意味着我要做的是创建一个新表并将记录保存在数据库中,它可以包含一个列,比如名称或其他与另一个表匹配的内容,不是吗?不要更新数据,使用重复的键插入它,ref documentation@KundanPrasad你是指这样的$sql=“insert into user\u info(inst_name、inst_name 2、年级、学习课程、资格、其他_资格、毕业年份)值(“$inst_name.”、“$inst_name 2.”、“$grade.”、“$study_course.”、“$qualification.”、“$other_qualification.”、“$completion_year.”)重复键更新inst_name=values(inst_name)(inst_name)、inst_2=values(inst_name)、grade=values=values(等级),学习课程=价值观(学习课程),资格=价值观(资格),其他资格=价值观(其他资格),“你太接近了$sql=”插入用户信息(仪器名称,仪器名称2,等级,学习课程,资格,其他资格‌​离子,完成年份)值(“$inst_名称。”,“$inst_名称2。”,“$grade。”,“$study_课程。”,“$quali”‌​",","其他‌​r_资格。“,”‌​".$完成一年。”‌​') 在重复密钥更新时,inst_name='“$inst_name.”,inst_name2='”.inst_name2.”,field=value,…;因此没有