根据登录用户修改MySQL条目

时间:2013-04-08 00:50:55

标签: php mysql

晚上再一次堆过鲜花,

我已经获得了@ sman591使用的代码:

$current_user_id = get_current_user_id(); // current user logged into WordPress

mysql_query("UPDATE `table` SET `user_id`='$current_user_id' WHERE `id`='$entry_id'") or die(mysql_error());

$query = mysql_query("SELECT * FROM `table` WHERE `id`='$entry_id' LIMIT 1") or die(mysql_error());
$data = mysql_fetch_array($query);

$allowed_user_edit_id = $data['id']; // user id who is allowed to edit

$current_user_id = get_current_user_id(); // current user logged into WordPress

if ($current_user_id == $allowed_user_edit_id) {
    // Your code for editing the entry
}

此代码用于补充我的modifyame.php(包装在Wordpress模板中)

<?php
/**
 * The template for displaying all pages.
 * Template Name: modifyame.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


mysql_connect("localhost", "herp", "derp") or die (mysql_error());
echo "Connected to MYSQL ";
mysql_select_db("diddly") or die (mysql_error());
echo "Connected to Data Base";


if(!isset($_POST['submit'])) {
    $q = "SELECT * FROM addserverame WHERE ID = $_GET[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]";
    mysql_query($u) or die (mysql_error());
    echo "Server Info Has Been Modified";
}
?>

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

<?php get_footer(); ?>

问题

我有一个问题合并这两个以便sman的条件在之前检查并验证之前的modifyame.php的条件,我的主要问题是我不是很熟悉PHP的语法规则,我放置了登录验证代码高于另一个if语句并尝试将语句包装在另一个语句中。

我在问什么

有人可以正确合并吗?

注意我认为将它们分开然后我的尝试会更容易(更少 混乱)

1 个答案:

答案 0 :(得分:0)

我猜你想要这样的东西,只需根据你现有的代码判断:

PS:您可能希望将业务与显示逻辑分开。

<?php
/**
 * The template for displaying all pages.
 * Template Name: modifyame.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

mysql_connect("localhost", "herp", "derp") or die (mysql_error());
echo "Connected to MYSQL ";
mysql_select_db("diddly") or die (mysql_error());
echo "Connected to Data Base";

$current_user_id = get_current_user_id(); // current user logged into WordPress

mysql_query("UPDATE `table` SET `user_id`='$current_user_id' WHERE `id`='$entry_id'") or die(mysql_error());

$query = mysql_query("SELECT * FROM `table` WHERE `id`='$entry_id' LIMIT 1") or die(mysql_error());
$data = mysql_fetch_array($query);

$allowed_user_edit_id = $data['id']; // user id who is allowed to edit

$current_user_id = get_current_user_id(); // current user logged into WordPress

if ($current_user_id == $allowed_user_edit_id):

    if(!isset($_POST['submit'])) {
    $q = "SELECT * FROM addserverame WHERE ID = $_GET[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]";
    mysql_query($u) or die (mysql_error());
    echo "Server Info Has Been Modified";
}
?>

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

<?php endif; ?>

<?php get_footer(); ?>