简体   繁体   English

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

[英]MYSQL and PHP database update wont update database

I am having an issue trying update the database with a form. 我在尝试使用表单更新数据库时遇到问题。

So, my issue is when I click on update nothing happens. 所以,我的问题是当我单击更新时什么也没发生。 The database doesn't update. 数据库不会更新。

Another issue is when i use POST in my form and SQL query. 另一个问题是当我在表单和SQL查询中使用POST时。 It doesn't pull the information through to the edit page, it is blank. 它不会将信息拉到编辑页面,而是空白。

I'm sorry if this hard to read etc. but this is my first time posting. 很抱歉,如果这很难读等,但这是我第一次发布。 Also, I know there is security flaws in my GET/POST queries I'm just trying to get the thing to work before I start using the prepared statements or whatever they are called. 另外,我知道我的GET / POST查询中存在安全漏洞,我只是想让事情开始起作用,然后再开始使用准备好的语句或任何被调用的语句。

FYI, if I echo the query and define a if/else statement, I can see it doesn't work but I just don't know why. 仅供参考,如果我回显查询并定义if / else语句,我可以看到它不起作用,但是我不知道为什么。 I have spent 3 days on this and change the code so many times using examples I have found on the internet. 我花了三天的时间,并使用我在互联网上找到的示例对代码进行了很多次更改。

Index.php 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 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 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");
 }
 ?>

Ok your edit.php form has a GET method, yet you are using POST variables in you processarticle.php and you have a GET variable in there. 好的,您的edit.php表单具有GET方法,但是您正在processarticle.php中使用POST变量,并且在那里具有GET变量。

Lets just say a form can only do one thing either GET or POST 可以说表单只能执行GET或POST一件事

The URL you specified in your form then will access either GET or POST variables based on form method 然后,您在表单中指定的URL将根据表单方法访问GET或POST变量

So if you want to update your article based off your form first lets look at the id = this should be $POST['id'] the hidden field in your form, not that hidden though 因此,如果您要根据表单更新文章,请先查看id =这应该是$ POST ['id']表单中的隐藏字段,尽管不是

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

The more I look at this the more this is going to turn in to a 3 part mini series 我越看越看,这将成为3部分迷你系列

Ok on your processarticle.php for starters I would use a prepared statement for the update http://php.net/manual/en/mysqli.prepare.php 好的,对于初学者来说,您在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();  
}
}

Start here http://www.w3schools.com/php/default.asp and go from crawling to walking Honest to god work through that from top to bottom then go here https://symfony.com/ and hope in a F1 Car 从这里开始http://www.w3schools.com/php/default.asp ,从爬行到步行,从诚实到上帝,从上到下依次完成,然后到这里https://symfony.com/并希望乘坐F1

Then try http://www.w3schools.com/bootstrap/default.asp because you are going to want your page to look cool 然后尝试http://www.w3schools.com/bootstrap/default.asp,因为您希望页面看起来很酷

Use form method post or change all $_POST in $_GET in processarticle.php . 使用表单方法post或更改processarticle.php$_GET中的所有$_POST
And try changing $_GET['edit_id'] into $_GET['id'] in processarticle.php . 和尝试改变$_GET['edit_id']$_GET['id']processarticle.php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM