MYSQL和PHP数据库更新不会更新数据库

时间:2016-04-24 09:08:07

标签: php html mysql sql-update

我在尝试使用表单更新数据库时遇到问题。

所以,我的问题是当我点击更新时没有任何反应。数据库不会更新。

另一个问题是我在表单和SQL查询中使用POST。它不会将信息提取到编辑页面,它是空白的。

如果这很难读,我很抱歉,但这是我第一次发帖。另外,我知道我的GET / POST查询中存在安全漏洞我只是想在开始使用预准备语句或其他任何调用语句之前让事情起作用。

仅供参考,如果我回应查询并定义一个if / else语句,我可以看到它不起作用,但我只是不知道为什么。我花了3天时间使用我在互联网上找到的示例多次更改代码。

  

的index.php

<?php

$servername = "localhost";
$username = "***";
$password = "****";
$dbname = "****";

$link = new mysqli("$servername", "$username", "$password", "$dbname");

if ($link->connect_error) {
    die("Connection failed: " . $link->connect_error);
}
echo "Connected successfully";


mysqli_select_db($link,"jamesrph_myphp");


$sql = "SELECT * FROM article";

$result = mysqli_query($link,$sql);

$id = 'id';
$title = 'title';


?>
<html>
<head>

<link rel="stylesheet" type="text/css" href="style.css"/>
<title>PHP </title>

</head>

<body>

<h1> My title </h1> 

<table>

<tr> 
<?php
while($row=mysqli_fetch_array($result)){

?>
<td><?php echo $row["title"]; ?> </td> 

<td><a href="article_detail.php?id=<?php echo $row["id"]; ?>">Read More</a></td>

<td><a href="edit.php?id=<?php echo $row["id"]; ?>">Edit</a></td>

</tr>
<?php 
}
?>
</table>       
</body>
</html>
  

edit.php

<?php

$link = mysqli_connect("localhost","******","******", "*****");

$query = "SELECT * FROM article WHERE id=".mysqli_real_escape_string($link, $_GET['id'])." LIMIT 1";

$result = mysqli_query($link,$query);

$row = mysqli_fetch_array($result);


$title = $row['title']; 

$content = $row['content'];

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>      


<p> Edit Article </p>

<form method="get" action="processarticle.php">

<input type="hidden" name="id" value="<?php echo $row["id"]; ?>" />

<input id="titlearea" type="text" name="title" value="<?php echo $row["title"]; ?>"/>    

<textarea id="contentarea" name="content" rows="10" cols="40"><?php echo $row["content"];?></textarea>    

<input type="submit" name="submit" id="update_article"/>

</form>
</body>

</html>
  

processarticle.php

 <<?php
 //Database Connection
 include 'connection.php';
 //Get ID from Database
 if(isset($_GET['edit_id'])){
 $sql = "SELECT * FROM article WHERE id =" .$_GET['edit_id'];
 $result = mysqli_query($link, $sql);
 $row = mysqli_fetch_array($result);
  }
 //Update Information
 if(isset($_POST['btn-update'])){
 $title = $_POST['title'];
 $content = $_POST['content'];
 $id = $_POST['id'];
 $update = "UPDATE article SET title=?, content=? WHERE id=?";
 $up = mysqli_query($link, $update);

 if($stmt = $mysqli->prepare($update)){
    $stmt->bind_param("ssi" ,$title ,$content ,$id);
    $stmt->excute();
 }
 header("location: disp.php");
 }
 ?>

2 个答案:

答案 0 :(得分:0)

好的你的edit.php表单有一个GET方法,但是你在processarticle.php中使用了POST变量,那里有一个GET变量。

让我们说一个表格只能做一件事GET或POST

您在表单中指定的URL将根据表单方法

访问GET或POST变量

因此,如果您想根据表单更新文章,请先查看id =这应该是$ POST [&#39; id&#39;]表单中的隐藏字段,而不是隐藏的字段

$update = "UPDATE article SET title='$title', content='$content' WHERE id=". $_POST['id'];

我看的越多,这将会变成3部分迷你系列

确定你的processarticle.php为初学者我会使用准备好的声明进行更新http://php.net/manual/en/mysqli.prepare.php

process.php

//Update Information
if(isset($_POST['Update_Article'])){
$title = $_POST['title'];
$content = $_POST['content'];
$id = $_POST['id'];

$SQL = "UPDATE
        article 
        SET title=?, content=?    
        WHERE id=?";

if ($stmt = $mysqli->prepare($SQL)) { 
     $stmt->bind_param("sss", $title ,$content ,$id );
     $stmt->execute();  
}
}

从这里http://www.w3schools.com/php/default.asp开始,从爬行到走路 通过从上到下的上帝工作,然后去这里 https://symfony.com/并希望参加F1赛车

然后尝试http://www.w3schools.com/bootstrap/default.asp,因为您希望自己的网页看起来很酷

答案 1 :(得分:-1)

使用表单方法发布或更改 processarticle.php $_POST的所有$_GET
并尝试将$_GET['edit_id']更改为 processarticle.php 中的$_GET['id']