douzepao0281 2017-11-19 04:15
浏览 36
已采纳

更新sql不适用于PHP的GET变量

I have the following code to pass a variable id (primary key in meeting table) from viewMeeting.php to modifyMeeting.php

Under viewMeeting.php:

<a href="modifyfinalmeeting.php?id=<?php echo $id ?>"><?php echo $nameMeeting; ?></a>

Eg of URL: http://localhost/MyProject/modifyMeeting.php?id=1

Under modifyMeeting.php:

//get ID from URL 
if (isset($_GET['id'])) {
    $id =$_GET['id'];
}

if (isset($_POST['updatebtn'])) {
        $sql = 'UPDATE meeting SET nameMeeting = ?, venue = ? WHERE id = ?';
        if ($stmt = mysqli_prepare($link, $sql)) {
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, 'sss', $nameMeeting, $venue, $id);

            // Attempt to execute the prepared statement
            if (mysqli_stmt_execute($stmt)) {

                //Alert to redirect
                 echo ("<SCRIPT LANGUAGE='JavaScript'>
                 window.alert('Succesfully Updated')
                 window.location.href='viewfinalmeeting.php';
                 </SCRIPT>");
             } else {
                 echo "Something went wrong. Please try again later.";
             }
        }
}

<button type="submit" name="updatebtn" class="btn btn-primary">Update</button>

If i hardcode the variable for $id (eg: $id = 2), it will update the database.

However, currently it will show "Successful Updated" but not update the database to the corresponding $id from the GET variable.

I also tried the following method but it does not work as it shows undefined function:

$id = mysql_real_escape_string($_GET['id'])

Please, any help as to how to update the database will be greatly appreciated!

  • 写回答

2条回答 默认 最新

  • doudu5498 2017-11-19 04:29
    关注

    Problem

    You are submitting the form from modifyMeeting.php page, so when it will go to the target page as mentioned in the tag it will not retain the $_GET['id'] variable as it will not be in the URL anymore. So your database will show value "undefined" for that id.

    Solution

    When you create the form on modifyMeeting.page, set the URL to contain the GET variable

    Example

    <form action="sample.php?id=<?php echo $_GET['id']; ?>" method="post">
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂