第二行无法在php中更新

时间:2018-07-25 05:38:02

标签: javascript php html mysql

我创建了一个页面,它是从寄存器页面中获取数据的。人们可以向该ID发送请求。为此,我把请求发送按钮。如果用户单击该按钮,将出现提示框,并且必须输入主题。

现在我的问题是输入的主题仅保存到第一行。它不支持第二行。仅更新第二行ID,用户ID和朋友ID。

我希望你们能帮助我确定我的错。

再次感谢

这是我的代码:

char const*

1 个答案:

答案 0 :(得分:0)

您的PHP代码生成几种形式,每种形式都有一个ID为“ subject”的隐藏输入字段。当您的JavaScript尝试使用document.getElementById("subject")查找元素时,它将始终找到第一个。

要解决此问题,您可以将正确的 form DOM元素作为参数传递给myFunction(),并使用它来设置主题字段的值。

要传递表格,只需使用this.form

<input type="button" onclick="myFunction(this.form)" name="addfriend" data-type='addfriend' id="addfriend" value="xx" />

然后像这样使用它:

function myFunction(form){
    var subject = prompt("Please enter Subject that want to study");
    if (subject != null){
        form['subject'].value= subject;
        form.submit();
    }
}

这是最终代码:

<?php
session_start();
$_SESSION['myid'];
$mysqli=new MySQLi('127.0.0.1','root','','learning_malaysia');
$sql = "SELECT * FROM tutor_register INNER JOIN tutorskill ON tutor_register.register_ID = tutorskill.register_ID ORDER BY 
tutor_register.register_ID='".$_SESSION['myid']."'desc";
$result= mysqli_query($mysqli,$sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Resuming</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
</head>
<script>
function myFunction(form){
    var subject = prompt("Please enter Subject that want to study");
    if (subject != null){
        form['subject'].value= subject;
        form.submit();
    }
}
</script>
<body>
<div class="container">
<?php
if(mysqli_num_rows($result)>0)
{

    while($row = mysqli_fetch_array($result))
{      
        $register_ID=$row["register_ID"];
        $username = $row['username'];
        $profile = $row['profile'];
        $email = $row['email'];
        $address=$row['address'];
        $gender=$row['gender'];
        $main_subject=$row["main_subject"];
        $subject_add=$row["subject_add"];
        $rate_main=$row["rate_main"];
        $rate_add=$row["rate_add"];
        $qualification=$row["qualification"];
        ?>
        <table><form method="post">
        <tr class="border_bottom">
        <td height="230"><img src='<?php echo $profile;?>'width="200" height="200"/>&nbsp;</td><td><td></td></td>
        <td class="data" width="800"><strong>Username:</strong>  <?php echo $username;?></br>
        <strong>Address:</strong>  <?php echo $address;?></br>
        <strong>Gender:</strong><?php echo $gender;?></br>
        <strong>Main Subject:</strong><?php echo $main_subject  ;?></br>
        <strong>Subject Added: </strong><?php echo $subject_add;?></br>
        <strong>Main Subject Rate:</strong> <?php echo $rate_main;?></br>
        <strong> Added Subject Rate:</strong><?php echo $rate_add; ?></br>
        <strong>Qualification:</strong> <?php echo $qualification; ?></td>
          <?php 
        if($register_ID == $_SESSION['myid']){
                ?>
                <td><label>Your Profile</label></td>
                <?php
            } else {
                ?>

                <form method="post" id="form" enctype="multipart/form-data" autocomplete="off"> 
                <td><button class='friendBtn unfriend'  name="" data-type="unfriend">Unfriend</button>
                  <input type="hidden" name="id" value="<?php echo $row['register_ID'];?>" />
                <input type="hidden" id="subject" name="subject" data-uid=<?php echo $_SESSION['id'] ;?>/>
                <input type="submit" onclick="myFunction(this.form)" name="addfriend" data-type='addfriend' id="addfriend" value="<?php 
                    if($register_ID == $_SESSION['friend_id'] && $_SESSION['status'] == "yes" ) {     
                    echo "Request Sent";
                    }
                    else { 
                    echo "Add Friend";
                    }
                    ?>"  /></td>     </form>

</tr>
</div>
</table></form> 
<?php
            }
}
}?>


</body>
</html>

<?php


if(isset($_POST['subject']) and $_POST['id']) {
$user_id = $_SESSION['myid'];
$friend_id = $_POST['id'];
$status = "yes";
$subject=$_POST['subject'];
$sql="INSERT INTO friends(user_id,status,subject,friend_id)" ."VALUES('$user_id','yes','$subject','$friend_id') ";

            if($mysqli->query($sql)=== true) {
                          $_SESSION['status']="yes";
                          $_SESSION['id']=$row['id'];
                        $_SESSION['friend_id']=$friend_id;
                        $_SESSION['user_id'] = $user_id;
            } else {}
                              }





?>

作为旁注,今后,您应该真正保护自己免受SQL injection的侵害(通常使用prepared statements)。