php - 你能看到逻辑错误吗

标签 php mysql wordpress

我有这个脚本:

<?php
/**
 * The template for displaying all pages.
 * Template Name: modify.php
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */

get_header(); ?>


    <div id="primary" class="site-content">
        <div id="content" role="main">


            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', 'page' ); ?>
                <?php comments_template( '', true ); ?>

            <?php endwhile; // end of the loop. ?>



<?php

$user_id = get_current_user_id();
if($user_id==0)
{
    echo "Login please";
    exit;
}

mysql_connect("localhost", "", "") or die (mysql_error());
#echo "Connected to MYSQL ";
mysql_select_db("derp") or die (mysql_error());
#echo "Connected to Data Base";
/*$query = "SELECT * FROM addserverame WHERE userId='{$user_id}'";
$result = mysql_query ($query) or die (mysql_error());*/

    if(!isset($_POST['submit'])) {
    $q = "SELECT * FROM addserverame WHERE ID = $_GET[id] AND userId='{$user_id}'";
    $result = mysql_query ($q);
    $person = mysql_fetch_array ($result);
    }

    ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add Your Server</title>
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>

</head>
<body id="main_body" >

    <img id="top" src="top.png" alt="">
    <div id="form_container">

        <h1><a>Modify Server</a></h1>
        <form id="form_606591" class="appnitro"  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                    <div class="form_description">
            <h2>Add Your Server</h2>
            <p></p>
        </div>                      
            <ul >

                    <li id="li_1" >
        <label class="description" for="servername">Server Name </label>
        <div>
            <input id="element_1" name="Servername" class="element text medium" type="text" maxlength="255" value="<?php echo $person['servername']; ?>"/> 
        </div><p class="guidelines" id="guide_1"><small>Enter your server name here NOT your server address.</small></p> 
        </li>       <li id="li_2" >
        <label class="description" for="Serveraddress">Server Address </label>
        <div>
            <input id="element_2" name="Serveraddress" class="element text medium" type="text" maxlength="255" value="<?php echo $person['serveraddress']; ?>"/> 
        </div><p class="guidelines" id="guide_2"><small>This will the DNS name of your server</small></p> 
        </li>       <li id="li_3" >
        <label class="description" for="Portnumber">PortNumber </label>
        <div>
            <input id="element_3" name="Portnumber" class="element text medium" type="text" maxlength="255" value="<?php echo $person['portnumber']; ?>"/> 
        </div><p class="guidelines" id="guide_3"><small>This will be the port your server is using.</small></p> 
        </li>       <li id="li_4" >
        <label class="description" for="Description">Server Description </label>
        <div>
            <textarea id="element_4" name="Description" class="element textarea medium" value ="<?php echo $person['description']; ?>"></textarea> 
        </div><p class="guidelines" id="guide_4"><small>Enter server description/rules here.</small></p> 
        </li>

                    <li class="buttons">
                <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />

                <input id="saveForm" class="button_text" type="submit" name="submit" value="Modify" />
        </li>
            </ul>
        </form> 
        <div id="footer">
        </div>
    </div>

    </body>-
</html>

<?

if(isset($_POST['submit'])) {
    $u = "UPDATE addserverame SET `servername`='$_POST[Servername]', `serveraddress`='$_POST[Serveraddress]', `portnumber`='$_POST[Portnumber]', `description`='$_POST[Description]' WHERE ID = $_POST[id] AND userId='{$user_id}'";
    mysql_query($u) or die (mysql_error());
    echo "Server Info Has Been Modified";
}
?>

    </div><!-- #primary -->
        </div><!-- Content -->

<?php //endif; ?>

<?php get_footer(); ?>

我想不出逻辑错误是什么,如果有的话。

意味着什么 用户选择更新表格,如果表格是他们的,他们应该能够编辑它并更新表格。

发生了什么 用户将尝试编辑它,但它不会更新,没有成功的修改回显,他们将被重定向到首页。

有谁能给我解释一下为什么会发生这种情况,而不是我想要发生的情况。

谢谢

最佳答案

您的表单失败的可能原因是该表单未在您的默认页面中运行,该默认页面路由到索引页面中的某个位置,因为它为您提供了正在执行的 php 文件和 index.php,所以主要原因可能是您的表单操作,即您在表单中使用操作,例如

action="<?php echo $_SERVER['PHP_SELF']; ?>"

所以它可能会重定向到您的主页,即 index.php,所以将您的表单操作留空可能对您有帮助

关于php - 你能看到逻辑错误吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15941788/

相关文章:

php - 用于 Excel 的 HTML 表格 - PHP

php - 求行的平均值

php - 使用 !password_verify 时是否需要转义密码输入?

php - 日期之间的MySQL日期

php - the_field 不输出为 HTML

javascript - HTML 表单提交 : open PHP in invisible browser tab

php - 如何优化选择 300.000 行的 SQl 代码 +

php - 如何使用php将文件路径存储在数据库中?

wordpress - 允许特定的 Twitter 用户查看 Wordpress 博客

wordpress - 如何在docker-entrypoint.sh文件中添加wordpress插件