[英]Can't insert and update at the same time
I have the code below and i can't figure why it's not working.我有下面的代码,我不知道为什么它不起作用。 The problem it's that i can insert a post with it, but when i try to update a post it's create a new page instead to update.
问题是我可以用它插入帖子,但是当我尝试更新帖子时,它会创建一个新页面来更新。
I have tried to remove isset
from if(isset($_POST['id']) != 'null')
and the update work, but then the insert doesn't work anymore.我试图从
if(isset($_POST['id']) != 'null')
和更新工作中删除isset
,但随后插入不再起作用。
Any idea what it's wrong with my code?知道我的代码有什么问题吗? Thanks.
谢谢。
<?php
if(isset($_POST['submitted']) == 1)
{
$title = mysqli_real_escape_string($dbc, $_POST['title']);
$header = mysqli_real_escape_string($dbc, $_POST['header']);
$body = mysqli_real_escape_string($dbc, $_POST['body']);
if(isset($_POST['id']) != 'null')
{
$q = "UPDATE pages SET user = $_POST[user], title = '$title', header = '$header', body = '$body' WHERE id = $_GET[id]";
}
else
{
$q = "INSERT INTO pages (user, title, header, body) VALUES ($_POST[user], '$title', '$header', '$body')";
}
$r = mysqli_query($dbc, $q);
if($r)
{
$message = '<p>Page was added!</p>';
}
else
{
$message = '<p>Page could not be added because:</p>'.mysqli_error($dbc);
$message .= '<p>'.$q.'</p>';
}
}
?>
You are using post and get at the same time.您同时使用 post 和 get。 first check whethet it is post or get.
首先检查它是发布还是获取。 then just simply do isset() check
然后只需简单地做 isset() 检查
<?php
if(isset($_POST['submitted']) == 1)
{
$title = mysqli_real_escape_string($dbc, $_POST['title']);
$header = mysqli_real_escape_string($dbc, $_POST['header']);
$body = mysqli_real_escape_string($dbc, $_POST['body']);
if(isset($_GET['id']) && $_GET['id']!="")
{
$q = "UPDATE pages SET user = $_POST[user], title = '$title', header = '$header', body = '$body' WHERE id = $_GET[id]";
}
else
{
$q = "INSERT INTO pages (user, title, header, body) VALUES ($_POST[user], '$title', '$header', '$body')";
}
$r = mysqli_query($dbc, $q);
if($r)
{
$message = '<p>Page was added!</p>';
}
else
{
$message = '<p>Page could not be added because:</p>'.mysqli_error($dbc);
$message .= '<p>'.$q.'</p>';
}
}
?>
尝试这个 :
if(isset($_POST['id']) AND $_POST['id'] != 'null')
use this code: if(isset($_POST['id'] && $_POST['id']!= '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.