您好我有一张表格,如下图所示......
...我从哪种形式加载数据库表中的第一个数据......
我想要的是当我点击列表顶部的数字时,加载适当的数据。
我的代码如下(不包括html表单代码)...
<?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>";
}
}
}
else{
//first time, initialize as you wish. Probably need to get the first id for this user, using another query
$id = 0;
}
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);
});
});
</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="' .$id. '">' .$id. '</a>';
}
}
答案 0 :(得分:0)
试试这个,评论中解释的变化:
<?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>';
}
}
?>
答案 1 :(得分:-1)
您可以使用ajax请求执行此操作。 Jquery为我们提供了简单的app界面。
可能会有无穷无尽的方法,但为了说明目的,以下是我将如何做到这一点。
<a href="#" class="num" change_form_to="1"> 1 </a><a href="#" class="num" change_form_to="3"> 2 </a> ..etc
或者你可以让jQuery根据元素的值决定POST什么信息..你的偏好
查看用户点击了哪个号码$('a').on('click',function(){var clicked = $(this).attr('change_form_to')})
所以此变量包含此自定义属性值 $(this).text()
或类似这样以获取文字文字 jQuery
然后调用php脚本$.ajax({type:'POST',url:'dbh.php',data:{idWork:clicked},success: function(res){populate(res)}})
,当调用成功函数时,响应已经来调用你自定义的填充函数
全部收集:
$('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语法中,您有UPDATE逻辑