gpt4 book ai didi

php - 如何使用PHP和mysql提交后返回数据

转载 作者:行者123 更新时间:2023-11-29 11:58:06 24 4
gpt4 key购买 nike

我需要一个帮助,即在数据库内提交后立即返回数据。我在下面解释我的代码。

addCourse.php:

<?php
$course_name=stripslashes($_POST['course_name']);
$course_short_name=stripslashes($_POST['course_short_name']);
$semester=stripslashes($_POST['semester']);
$con = mysql_connect('localhost', 'root', '******');
mysql_select_db('go_fasto', $con);
$qry ='INSERT INTO db_course (course_name,short_name,semester) values ("' . $course_name . '","' . $course_short_name . '","' . $semester . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
echo "Course has added successfully";
} else {
echo "course could not added ";
}
?>

该文件是使用ajax调用的,所有返回值都会出现在ajax的success函数中。这里我需要将提交的数据和成功消息返回到ajax的success函数中。请帮我解决这个问题。

最佳答案

您可以使用json_encode来实现预期的输出。

    $result['course_name'] = $course_name;
$result['course_short_name'] = $course_short_name;
$result['semester'] = $semester;
if ($qry_res) {
$result['message'] = 'Course has added successfully';
} else {
$result['message'] = 'course could not added';
}

echo json_encode($result);

您可以检索所有提交的数据以及消息。

关于php - 如何使用PHP和mysql提交后返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32835947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com