简体   繁体   English

管理面板:PHP表单不会将数据发送到MySQL

[英]Admin Panel: PHP form doesn't send data to MySQL

I have a simple code to add banners from admin panel to the index of the site. 我有一个简单的代码,可将管理面板中的横幅添加到网站的索引中。 But the add function doesnt work correctly here is the form to add banner 但是添加功能不能正常工作,这里是添加横幅的形式

                            <h2>Add Banner</h2>                            
<?php include ("../engine/config/config.php"); ?>
                                <form method="post" action="">
        Clicks
        <input type="text" name="click" value="0" style="width: 200px;" /> <div class="hr"></div>        
        Impressions
        <input type="text" name="imp" value="0" style="width: 200px;" /> <div class="hr"></div>                     
        LINK
        <input type="text" name="url" value="http://" style="width: 200px;" /> <div class="hr"></div>
        Size
      <select name="razmer">
<option value='468x60'>468x60</option>
<option value='88x31'>88x31</option>
</select>
<div class="hr"></div>
        Banner<br />
        <input type="text" name="picurl" value="http://" style="width: 200px;" /><div class="hr"></div>
        <input type="submit" name="submit" value="Submit"> <br  />
        </form>

<?
if($_POST['submit']) {
$click = $_POST['click'];
$imp = $_POST['imp'];
$url = $_POST['url'];
$razmer = $_POST['razmer'];
$picurl = $_POST['picurl'];
    $sql = "INSERT INTO `banneradd` (click, imp, url, razmer, picurl, username) VALUES ('$click', '$imp', '$url', '$razmer', '$picurl', '')";
    $result = mysql_query($sql);
    echo "<div class='hr'>The Banner has been added, please go back to the index: <a href='view_reklama.php'> Index </a></div>";
    }
    ?>

So it say it was added but when I go back ITS NOT. 所以它说它被添加了,但是当我回去时它没有。 There is no error or anything, can someone help? 没有错误或任何东西,有人可以帮忙吗? Thanks in advance :) 提前致谢 :)

Okay, there are way too many things wrong with your code, so if you're learning from a particular site or person... find a different source. 好的,代码中有太多错误,因此,如果您正在从特定站点或个人那里学习,请查找其他来源。

  1. Don't open PHP with <? 不要用<?打开PHP <? . This is the shorthand style. 这是速记样式。 It is disabled on many if not most web servers, and for good reason -- because XML introduces its encoding using the same opening <? 在很多(如果不是大多数)Web服务器上,它是禁用的,这是有原因的-因为XML使用相同的开头<?引入了其编码<? and it causes conflict. 并导致冲突。 Always open your PHP with <?php . 始终使用<?php打开您的PHP。 http://www.php.net/manual/en/ini.core.php#ini.short-open-tag http://www.php.net/manual/zh/ini.core.php#ini.short-open-tag

  2. Don't use if($_POST['submit']) , use if (isset($_POST['submit'])) . 不要使用if($_POST['submit']) ,而要使用if (isset($_POST['submit'])) Your current script should generate an error, but it's probably being masked because PHP defaults to not showing very many errors. 您当前的脚本应该会产生一个错误,但是由于PHP默认不显示很多错误,因此它可能被掩盖了。 It does trigger a warning, though, because you're checking if the variable (or rather array value) $_POST['submit'] is equal to true . 但是,它确实会触发警告,因为您正在检查变量(或更确切地说是数组值) $_POST['submit']是否等于true In fact, that variable is undefined. 实际上,该变量是未定义的。 Use isset() to check if a variable exists. 使用isset()检查变量是否存在。 http://php.net/manual/en/function.isset.php http://php.net/manual/zh/function.isset.php

  3. Sanitize your user's input. 清理用户的输入。 If somebody typed a ' into any of your fields, your query would break. 如果有人在您的任何字段中输入' ,则查询会中断。 Why? 为什么? Because in your query, you're placing your stringed values in single quotes, and any instance of another single quotation mark would break out of that. 因为在查询中,您将字符串值放在单引号中,而另一个单引号的任何实例都将超出该范围。 There is such a thing as magic quotes in PHP (which automatically escapes POST values), but it's absolutely awful, so please disable it. PHP中有一个神奇的引号(它会自动转义POST值),但是绝对糟糕,因此请禁用它。 http://php.net/manual/en/security.magicquotes.php The best way to escape user input is with real escape functions (more on that later). http://php.net/manual/zh-cn/security.magicquotes.php逃避用户输入的最佳方法是使用真正的逃逸功能(稍后会详细介绍)。

  4. mysql_ functions are deprecated. 不建议使用mysql_函数。 Use PDO or MySQLi. 使用PDO或MySQLi。 If you're getting used to the mysql_ functions, it is easier to transition to MySQLi. 如果您习惯了mysql_函数,则过渡到MySQLi会更容易。 For simplicity, I'll use the procedural style, but it's much better to go with the OOP style.... 为了简单起见,我将使用过程样式,但是与OOP样式一起使用会更好。

  5. If you want to debug MySQL commands with PHP, you should format your queries carefully, print the error, and also print the computed query, because sometimes you need to look at the actual resulted query in order to see what is wrong with it. 如果要使用PHP调试MySQL命令,则应仔细设置查询的格式,打印错误并打印计算出的查询,因为有时您需要查看实际的查询结果才能查看问题所在。

That said, here's what I suggest: 也就是说,这是我的建议:

<?php
error_reporting(E_ALL);
    // Turn on all error reporting. Honestly, do this every time you write a script,
    // or, better yet, change the PHP configuration.

$connection = mysqli_connect('host', 'username', 'password', 'database');
    // Somewhere in your config file, I assume you're calling mysql_connect.
    // This is a pretty similar syntax, although you won't need mysql_select_db.

if (isset($_POST['submit'])) {
    $click = mysqli_real_escape_string($connection, $_POST['click']);
        // This will escape the contents of $_POST['click'], e.g.
        // if the user inputted: Hello, 'world'! then this will produce:
        // Hello, \'world\'!
    $imp = mysqli_real_escape_string($connection, $_POST['imp']);
    $url = mysqli_real_escape_string($connection, $_POST['url']);
    $razmer = mysqli_real_escape_string($connection, $_POST['razmer']);
    $picurl = mysqli_real_escape_string($connection, $_POST['picurl']);

    $query = "
INSERT INTO `banneradd` (
    `click`,
    `imp`,
    `url`,
    `razmer`,
    `picurl`,
    `username`
)
VALUES
    (
        '$click',
        '$imp',
        '$url',
        '$razmer',
        '$picurl',
        ''
    );
";
    // Format your query nicely on multiple lines. MySQL will tell you what line
    // the error occurred on, but it's not helpful if everything's on the same line.
    $result = mysqli_query($connection, $query);
    $error = mysqli_error($connection);
    if ($error) {
        echo "A MySQL error occurred: $error<br>";
        echo "<pre>$query</pre>";
            // If an error occurred, print the error and the original query
            // so you can have a good look at it.
        die;
            // Stop executing the PHP.
    }

    echo '<div class="hr">The Banner has been added, please go back to the index: <a href="view_reklama.php"> Index </a></div>';
}
?>

See if that helps. 看看是否有帮助。 Chances are, the MySQL error will be helpful with diagnosing the problem. 可能的情况是,MySQL错误将有助于诊断问题。 You might have just misspelled a column name or table name. 您可能只是拼错了列名或表名。

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

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