无法在XAMPP中使用php更新MYSQL数据库表字段

用户名

因此,我正在尝试设计一个php网站,到目前为止,在将条目添加到列表中方面,它都能很好地工作。

问题是,它无法使用来更新edit.php单击编辑链接后,它会显示一条消息:

“没有要编辑的数据。”

但是,如果我尝试手动将localhost/edit.php**?id=1**其显示为ID编号列表,则效果很好。请帮忙。

home.php

<html>
    <head>
        <title>My first PHP Website</title>
    </head>
   <?php
   session_start(); //starts the session
   if($_SESSION['user']){ // checks if the user is logged in  
   }
   else{
      header("location: index.php"); // redirects if user is not logged in
   }
   $user = $_SESSION['user']; //assigns user value
   ?>
    <body>
        <h2>Home Page</h2>

<hello>! 
 <!--Display's user name-->
        <a href="logout.php">Click here to go logout</a><br/><br/>
        <form action="add.php" method="POST">
           Add more to list: <input type="text" name="details" /> <br/>
           Public post? <input type="checkbox" name="public[]" value="yes" /> <br/>
           <input type="submit" value="Add to list"/>
        </form>
    <h2 align="center">My list</h2>
    <table border="1px" width="100%">
    <tr>
      <th>Id</th>
      <th>Details</th>
      <th>Post Time</th>
      <th>Edit Time</th>
      <th>Edit</th>
      <th>Delete</th>
      <th>Public Post</th>
    </tr>
    <?php
      mysql_connect("localhost","root","") or die(mysql_error());
      mysql_select_db("first_db") or die("Cannot connect to database");
      $query = mysql_query("select * from list");
      while($row = mysql_fetch_array($query))
      {
        print "<tr>";
          print '<td align="center">'. $row['id'] . "</td>";
          print '<td align="center">'. $row['details'] . "</td>";
          print '<td align="center">'. $row['date_posted'] . " - " . $row['time_posted'] . "</td>";
          print '<td align="center">'. $row['date_edited'] . " - " . $row['time_edited'] . "</td>";
          print '<td align="center"><a href="edit.php">edit</a></td>';
          print '<td align="center"><a href="delete.php">delete</a></td>';
          print '<td align="center">'. $row['public'] . "</td>";
        print "</tr>";
      }
    ?>
    </table>
  </body>
</html>

图像

edit.php

<html>
    <head>
        <title>My first PHP website</title>
    </head>
    <?php
    session_start(); //starts the session
    if($_SESSION['user']){ //checks if user is logged in
    }
    else{
        header("location:index.php"); // redirects if user is not logged in
    }
    $user = $_SESSION['user']; //assigns user value
    $id_exists = false;
    ?>
    <body>
        <h2>Home Page</h2>
        <p>Hello <?php Print "$user"?>!</p> <!--Displays user's name-->
        <a href="logout.php">Click here to logout</a><br/><br/>
        <a href="home.php">Return to Home page</a>
        <h2 align="center">Currently Selected</h2>
        <table border="1px" width="100%">
            <tr>
                <th>Id</th>
                <th>Details</th>
                <th>Post Time</th>
                <th>Edit Time</th>
                <th>Public Post</th>
            </tr>
            <?php
                if(!empty($_GET['id']))
                {
                    $id = $_GET['id'];
                    $_SESSION['id'] = $id;
                    $id_exists = true;
                    mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
                    mysql_select_db("first_db") or die("Cannot connect to database"); //connect to database
                    $query = mysql_query("Select * from list Where id='$id'"); // SQL Query
                    $count = mysql_num_rows($query);
                    if($count > 0)
                    {
                        while($row = mysql_fetch_array($query))
                        {
                            Print "<tr>";
                                Print '<td align="center">'. $row['id'] . "</td>";
                                Print '<td align="center">'. $row['details'] . "</td>";
                                Print '<td align="center">'. $row['date_posted']. " - ". $row['time_posted']."</td>";
                                Print '<td align="center">'. $row['date_edited']. " - ". $row['time_edited']. "</td>";
                                Print '<td align="center">'. $row['public']. "</td>";
                            Print "</tr>";
                        }
                    }
                    else
                    {
                        $id_exists = false;
                    }
                }
            ?>
        </table>
        <br/>
        <?php
        if($id_exists)
        {
        Print '
        <form action="edit.php" method="POST">
            Enter new detail: <input type="text" name="details"/><br/>
            public post? <input type="checkbox" name="public[]" value="yes"/><br/>
            <input type="submit" value="Update List"/>
        </form>
        ';
        }
        else
        {
            Print '<h2 align="center">There is no data to be edited.</h2>';
        }
        ?>
    </body>
</html>

<?php
    if($_SERVER['REQUEST_METHOD'] == "POST")
    {
        mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
        mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
        $details = mysql_real_escape_string($_POST['details']);
        $public = "no";
        $id = $_SESSION['id'];
        $time = strftime("%X");//time
        $date = strftime("%B %d, %Y");//date
        foreach($_POST['public'] as $list)
        {
            if($list != null)
            {
                $public = "yes";
            }
        }
        mysql_query("UPDATE list SET details='$details', public='$public', date_edited='$date', time_edited='$time' WHERE id='$id'") ;
        header("location: home.php");
    }
?>

http://s15.postimg.org/m8dloz7d7/screenshot_20.png

这是?id=1网址为http; // s15,postimg,org / yoabiq0p7 / screenshot_21,png的网址(用句号更改逗号)。

布鲁诺

您仅打印edit.php,需要打印整个编辑链接。

print '<td align="center"><a href="edit.php">edit</a></td>';

将此行替换为:

print '<td align="center"><a href="edit.php?id=' . $row["id"] . '">edit</a></td>';

这样可以解决问题。

PS:请注意,您的代码已开放用于SQL注入!确保在此位置使用mysql_real_escape_string():

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

如果id仅是数字,则也可以执行以下操作以避免SQL注入:

$id = intval($_GET["id"]);

SQL注入非常重要,您需要过滤来自外部的内容。我建议也使用预准备的语句PDO。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法使用php更新mysql数据库中的值

来自分类Dev

PHP-如何更新数据库中正确行中的表字段?

来自分类Dev

更新查询只使数据库表字段为空

来自分类Dev

使用php连接到特定的数据库(xampp / mysql)

来自分类Dev

无法使用SWIFT更新数据库FMDB中的字段

来自分类Dev

无法使用SWIFT更新数据库FMDB中的字段

来自分类Dev

在MySQL数据库中使用JDBC更新数据

来自分类Dev

如何使数据库表字段动态

来自分类Dev

使用PHP将多个HTML字段更新到MySQL数据库

来自分类Dev

无法在Codeigniter中使用Ajax更新数据库

来自分类Dev

无法更新数据库中的字段

来自分类Dev

PHP&MySql:无法更新数据库

来自分类Dev

无法更新数据库php / mysql上的库存数量

来自分类Dev

在Meteor中使用反应性MySQL数据库(更新?)

来自分类Dev

无法在mysql中使用数据库(访问被拒绝)

来自分类Dev

无法使用PHP使用引号更新SQL数据库

来自分类Dev

无法从MySQL数据库更新数据

来自分类Dev

使用PHP和AJAX的MySQL UPDATE,无法更新数据库

来自分类Dev

用户无法使用 PHP 表单更新 MySQL 数据库中的信息

来自分类Dev

无法使用PHP在MySQL数据库中插入数据

来自分类Dev

无法使用 PHP 更新 postgres 数据库

来自分类Dev

无法在mysql中使用数据库(未知数据库)

来自分类Dev

将json字段与数据库表字段映射

来自分类Dev

使用PHP和AJAX更新MySQL数据库

来自分类Dev

不使用按钮更新数据库?PHP MySQL

来自分类Dev

使用php语句更新MySQL数据库

来自分类Dev

使用 MySQL 和 PHP 更新数据库

来自分类Dev

PHP MySql数据库阵列更新

来自分类Dev

PHP-更新MySQL数据库