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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度