dongyan1548 2018-04-14 01:22
浏览 57
已采纳

php更新查询不更新数据库

I am trying to update my database using a php file that has an html form in it. When I hit 'update' in the url bar, the updated information is showing. But when I go back to my HTML page that shows me everything in my database, it still has the old information.

What am I not doing?

I know there are security issues, like not using a session, or sanitizing the data, or using my_sql in general. This is just for a school project. After the semester I will be closing the hosting account.

EDIT: I moved the "$id = $_GET['id'];" line above the update query so that "$id" would be defined before query. Updated code.

EDIT2: After following the comments about turning on errors and displaying them after the update query. It showed that the ID was not in fact being read back in. So I added a hidden input value for the ID to give back to the file after the submit button was it.

<?php
error_reporting(E_ALL & ~E_DEPRECATED); 
ini_set('display_errors', 1); 

$host = 'hose';
$user = 'user';
$pass = 'pass';
$database = 'database';
$table = 'table';

//connecting to server
$conn = mysql_pconnect($host,$user,$pass); 

//opening to database
if (!($db = mysql_select_db($database))) {
    echo "Could NOT connect to database.";
}

//gathering new data from update form
if (isset($_GET['submit'])) {
    $title= $_GET['title'];
    $year = $_GET['year'];
    $director = $_GET['director'];
    $genre = $_GET['genre'];
    $runtime = $_GET['runtime'];
    $id = $_GET['id'];
    $query = mysql_query("UPDATE `collection`
                  SET `title`='$title', `year`='$year', `director`='$director', `genre`='$genre', `runtime`='$runtime' 
                  WHERE `ID`='$id'");
}

//passing in ID number and running query
$id = $_GET['id'];
$query = "SELECT * FROM '$table' WHERE ID = '$id'";
$result = mysql_query($query);
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}

//getting row data for ID number
$row = mysql_fetch_array( $result );

?>

<!DOCTYPE html>
<html>
   <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <title>title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="webpage.css">
    <style type = "text/css">
        table, th, td {
                border: 0px solid black;
                border-collapse: collapse;
            }
        table {
            margin: auto;
            width: 50%;
            }
        td {
                padding: 5px;
            }
        img {
            text-decoration:  none;
        }
    </style>
   </head>
   <body class="subStyle">
   
    <div class="topnav">
        <a href="#">Home</a>
        <a href="#">Database</a>
        <a href="#">Insert</a>
    </div>
    
    <form class='form' method='get'>
    <table border=0>
        <tr>
        <th>Movie Title</th>
        <th>Year Made</th>
        <th>Director</th>
        <th>Genre</th>
        <th>Runtime(Minutes)</th>
        </tr>
        
        <tr>
        <td><input type=text name="title"    id="title"    maxlength=100 size=50 value="<?php echo $row['title']; ?>"></td>
        <td><input type=text name="year"     id="year"     maxlength=4   size=10 value="<?php echo $row['year']; ?>"></td>
        <td><input type=text name="director" id="director" maxlength=100 size=30 value="<?php echo $row['director']; ?>"></td>
        <td><input type=text name="genre"    id="genre"    maxlength=20  size=20 value="<?php echo $row['genre']; ?>"></td>
        <td><input type=text name="runtime"  id="runtime"  maxlength=4   size=20 value="<?php echo $row['runtime']; ?>"></td>
        <td><input type=hidden name="id"  id="id"  value="<?php echo $row['ID']; ?>"></td>
        </tr>
        
        <tr><td></td><td></td><td>
        <button class='submit' type='submit' name='submit' value='update'>Update Movie</button></td></tr>
    </table>
    </form>
   </body>
</html>

<?php

//check if update worked
if (isset($_GET['submit'])) {
    echo '<div class="form" id="form3"><br><br><br><br><br><br>
    <Span>Data Updated Successfuly</span></div>';
}

//close connection
mysql_close($conn); 
?>

</div>
  • 写回答

1条回答 默认 最新

  • dtcu5885 2018-04-14 01:30
    关注

    Your code is insecure - Why shouldn't I use mysql_* functions in PHP?

    I think the issue is down to variables not being defined - in your query:

    UPDATE '$table' SET `title`='$title', `year`='$year', 
    `director`='$director', `genre`='$genre', `runtime`='$runtime' 
    WHERE `ID`='$id'
    

    $id is not defined

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗