gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:52:04 26 4
gpt4 key购买 nike

所以,我正在尝试设计一个 php 网站,到目前为止,它在向列表添加条目方面效果良好。

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

"There is no data to be edited."

但是如果我尝试手动放置 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>

image )

编辑.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>';

这将解决问题。

P.S:小心,你的代码是开放的 SQL 注入(inject)!确保在这个地方使用mysql_real_escape_string():

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

如果id只是数字,您也可以执行以下操作以避免SQL注入(inject):

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

SQL 注入(inject)问题非常严重,您需要过滤来自外部的内容。我也建议使用准备好的语句 PDO。

关于php - 无法在XAMPP中使用php更新MYSQL数据库表字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40794996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com