Php 根据我单击的编号从数据库加载数据
您好,我有一个表格,如下图所示 。。。以何种形式从数据库表加载第一个数据 我想要的是,当我单击列表顶部的数字时,加载相应的数据 我的代码如下(不包括html表单代码)Php 根据我单击的编号从数据库加载数据,php,jquery,forms,Php,Jquery,Forms,您好,我有一个表格,如下图所示 。。。以何种形式从数据库表加载第一个数据 我想要的是,当我单击列表顶部的数字时,加载相应的数据 我的代码如下(不包括html表单代码) $(文档).ready(函数(){ $(“#更新工作”)。单击(函数(){ $(“#idW”).css(“显示”、“无”); var r=parseInt($(“#idW”).val(),10)+1; $(“#idW”).val(r); }); }); 您可以通过ajax请求来实现这一点。Jquery为我们提供了简单的应用
$(文档).ready(函数(){
$(“#更新工作”)。单击(函数(){
$(“#idW”).css(“显示”、“无”);
var r=parseInt($(“#idW”).val(),10)+1;
$(“#idW”).val(r);
});
});
您可以通过ajax请求来实现这一点。Jquery为我们提供了简单的应用程序界面
可能有无穷无尽的方法,但为了说明目的,这里是我将如何做到这一点
- 让每个锚元素都有一个代表性的属性,可以是类选择器:
。etc
或者您可以让jQuery根据元素的值来决定发布什么信息。。你的喜好
- 请参阅,用户单击的是哪个数字?
$('a')。在('click',function(){var clicked=$(this).attr('change\u form\u to'))上
因此,这里的变量保存这个自定义属性值$(this).text()
或类似的值,以获取文本文本文本jQuery
- 然后调用php脚本
$.ajax({type:'POST',url:'dbh.php',data:{idWork:clicked},success:function(res){populate(res)}})
,当调用success函数时,响应为您调用定制的populate函数
致geather:
$('a').on('click',function(){
var clicked = $(this).attr('change_form_to');
$.ajax({
type:'POST',
url:'dbh.php',
data:{idWork:clicked},
success: function(res){
console.log(res);
//populate(res); could look like this if your php script dies with just outputing json_encode on some assoc array
var j = JSON.parse(res);
$('form input.name').value(j.name) ...etc
}
})
});
您的问题表明您希望从数据库中获取数据,但在sql语法中,您有更新逻辑
<?php
$username = $_SESSION["username"];
if(isset($_POST['idWork'])){
$id = $_POST['idWork'];
$job_title = mysql_real_escape_string($_POST["job_title"]);
$company = mysql_real_escape_string($_POST["company"]);
$website = mysql_real_escape_string($_POST["website"]);
$start_date = mysql_real_escape_string($_POST["start_date"]);
$end_date = mysql_real_escape_string($_POST["end_date"]);
$start_year = mysql_real_escape_string($_POST["start_year"]);
$end_year = mysql_real_escape_string($_POST["end_year"]);
$work_history = mysql_real_escape_string($_POST["work_history"]);
if($start_year > $end_year)
{
echo '<script>ErrorMessage()</script>';
$id=$id-1;
$good = false;
}
else
{
$good = true;
}
if($good == true){
$query="UPDATE work
SET job_title = '$job_title', company = '$company', website = '$website', start_date='$start_date', end_date='$end_date', start_year='$start_year', end_year='$end_year', work_history='$work_history'
WHERE id='$id' AND username='$username'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=0){
echo "<p>Record Updated<p>";
echo "<script type='text/javascript'>;
window.location='addCV.php';
</script>";
}else{
echo "<p>Error Updating Record<p>";
echo "<script type='text/javascript'>;
window.location='addCV.php';
</script>";
}
}
}
// add the below check
else if(isset($_GET['idWork'])){
// user clicked on one of the id links to get here
// set $id to the value of the GET parameter for key "idWork"
$id = $_GET['idWork'];
}
else{
// first time, initialize as you wish. Probably need to get the first id for this user, using another query
// I went ahead and added this for you...
if($query = mysql_query("SELECT job_title,company,website,start_date,end_date,start_year,end_year,work_history FROM work WHERE username='$username' order by id asc limit 1") or die(mysql_error())){
if(mysql_num_rows($query)>0){
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
}
}
else{
echo '<script>ErrorMessage()</script>';
}
}
}
if($query = mysql_query("SELECT job_title,company,website,start_date,end_date,start_year,end_year,work_history FROM work WHERE id>'$id' AND username='$username' order by id asc limit 1") or die(mysql_error()))
{
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$job_title = $row['job_title'];
$company = $row['company'];
$website = $row['website'];
$start_date = $row['start_date'];
$end_date = $row['end_date'];
$start_year = $row['start_year'];
$end_year = $row['end_year'];
$work_history = $row['work_history'];
}
}
}
?>
<script>
$(document).ready(function(){
$("#updateWork").click(function(){
$("#idW").css("display","none");
var r = parseInt($("#idW").val(),10)+1;
$("#idW").val(r);
});
// listen for clicks on the id links
$('[data-row-id]').click(function(e){
e.preventDefault();
var fileName = 'thisFile.php?idWork='; //change "thisFile.php" to the name of this file in your project, the "?" starts the GET parameters, idWork= sets the key for the GET parameter
var id = $(this).data('row-id'); // this gets the id that we stored in the link's data attribute
var url = fileName+id; // then we add that id as the value for the "idWork" key
window.location= url; // esentially refresh this page with the id set as a GET parameter and make use of the logic we already have to load the info
});
});
</script>
<p><input type="button" value="Work History" id="SlideMeWorkHistoryButton" style="color: #F87F25; background: black; font: bold 22px Tahoma; width: 16em; height: 2em; border-radius:7px; padding:4px;"/></p>
<div id="SlideMeWorkHistoryForm">
<?php
$sql = "SELECT id FROM work WHERE username='$username' order by id asc limit 10;";
$result = mysql_query($sql);
if ($result != 0) {
$num_results = mysql_num_rows($result);
for ($i=0;$i<$num_results;$i++) {
$row = mysql_fetch_array($result);
$id = $row['id'];
echo '<a href="#" data-row-id="'.$id.'">' .$id. '</a>';
}
}
?>
最后一个脚本是我们用于GETlol的脚本,我没有注意到粘贴的代码区域是可滚动的。这是答案,请检查!你能不能把你的代码整合到我的代码中,以便理解它是如何工作的?谢谢你的时间,请插一点。一旦你掌握了我提供的基本知识,你就可以把它们放在一起。你应该使用数据属性。您没有提到OP需要如何设置dbh.php
来返回查询行的信息Hi niklakis,您的项目是否一定要使用php
和mySQL
?我之所以这么问,是因为如果您愿意使用parse.com的免费服务,只需使用jquery就可以非常轻松地创建所需的内容。查看我在1.5小时内制作的这个演示,它完全满足您的需要,并且只使用jquery来完成:(注意,我在编号的链接上使用了一个“下一步”按钮)@delighteddod我们做了类似的事情,但教授希望根据我们数据库中的数据数量显示随机的数字。他想显示特定表格中可能包含的所有数据的编号,然后当我点击编号2时,例如在文本字段中加载该特定编号的信息。这很容易添加,你必须使用php和MySQL吗?@bleeted0d是的,我们使用php和MySQL来实现这一点project@DelightedD0D你有什么建议吗?感谢您的时间,我收到了这个错误通知:未定义的索引:id在第472行的E:\xampp\htdocs\CVToolGismo\addCV.php中,这可能是它不起作用的原因。linr 472中的代码是$id=$row['id']。。。。。我还替换了var文件名='thisFile.php?idWork=';用这个。。。。var fileName='thisFile.php?idWork='。。。正如您在评论中提到的,while循环或for循环中的一个?同样,如果我单击一个数字,错误会消失,但它不会将数据加载到formI中。我尝试了您的代码,我检测到当我单击任何数字时,我浏览器上的url变为addCV.php?idWork=undefined,所以我可以理解它没有得到id值,因为如果我自己写这个数字,例如addCV.php?idWork=4,它会加载数据
<?php
$username = $_SESSION["username"];
if(isset($_POST['idWork'])){
$id = $_POST['idWork'];
$job_title = mysql_real_escape_string($_POST["job_title"]);
$company = mysql_real_escape_string($_POST["company"]);
$website = mysql_real_escape_string($_POST["website"]);
$start_date = mysql_real_escape_string($_POST["start_date"]);
$end_date = mysql_real_escape_string($_POST["end_date"]);
$start_year = mysql_real_escape_string($_POST["start_year"]);
$end_year = mysql_real_escape_string($_POST["end_year"]);
$work_history = mysql_real_escape_string($_POST["work_history"]);
if($start_year > $end_year)
{
echo '<script>ErrorMessage()</script>';
$id=$id-1;
$good = false;
}
else
{
$good = true;
}
if($good == true){
$query="UPDATE work
SET job_title = '$job_title', company = '$company', website = '$website', start_date='$start_date', end_date='$end_date', start_year='$start_year', end_year='$end_year', work_history='$work_history'
WHERE id='$id' AND username='$username'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=0){
echo "<p>Record Updated<p>";
echo "<script type='text/javascript'>;
window.location='addCV.php';
</script>";
}else{
echo "<p>Error Updating Record<p>";
echo "<script type='text/javascript'>;
window.location='addCV.php';
</script>";
}
}
}
// add the below check
else if(isset($_GET['idWork'])){
// user clicked on one of the id links to get here
// set $id to the value of the GET parameter for key "idWork"
$id = $_GET['idWork'];
}
else{
// first time, initialize as you wish. Probably need to get the first id for this user, using another query
// I went ahead and added this for you...
if($query = mysql_query("SELECT job_title,company,website,start_date,end_date,start_year,end_year,work_history FROM work WHERE username='$username' order by id asc limit 1") or die(mysql_error())){
if(mysql_num_rows($query)>0){
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
}
}
else{
echo '<script>ErrorMessage()</script>';
}
}
}
if($query = mysql_query("SELECT job_title,company,website,start_date,end_date,start_year,end_year,work_history FROM work WHERE id>'$id' AND username='$username' order by id asc limit 1") or die(mysql_error()))
{
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$job_title = $row['job_title'];
$company = $row['company'];
$website = $row['website'];
$start_date = $row['start_date'];
$end_date = $row['end_date'];
$start_year = $row['start_year'];
$end_year = $row['end_year'];
$work_history = $row['work_history'];
}
}
}
?>
<script>
$(document).ready(function(){
$("#updateWork").click(function(){
$("#idW").css("display","none");
var r = parseInt($("#idW").val(),10)+1;
$("#idW").val(r);
});
// listen for clicks on the id links
$('[data-row-id]').click(function(e){
e.preventDefault();
var fileName = 'thisFile.php?idWork='; //change "thisFile.php" to the name of this file in your project, the "?" starts the GET parameters, idWork= sets the key for the GET parameter
var id = $(this).data('row-id'); // this gets the id that we stored in the link's data attribute
var url = fileName+id; // then we add that id as the value for the "idWork" key
window.location= url; // esentially refresh this page with the id set as a GET parameter and make use of the logic we already have to load the info
});
});
</script>
<p><input type="button" value="Work History" id="SlideMeWorkHistoryButton" style="color: #F87F25; background: black; font: bold 22px Tahoma; width: 16em; height: 2em; border-radius:7px; padding:4px;"/></p>
<div id="SlideMeWorkHistoryForm">
<?php
$sql = "SELECT id FROM work WHERE username='$username' order by id asc limit 10;";
$result = mysql_query($sql);
if ($result != 0) {
$num_results = mysql_num_rows($result);
for ($i=0;$i<$num_results;$i++) {
$row = mysql_fetch_array($result);
$id = $row['id'];
echo '<a href="#" data-row-id="'.$id.'">' .$id. '</a>';
}
}
?>