简体   繁体   English

基本的PHP语法或配置? 脚本不在WAMP服务器上执行

[英]Basic PHP syntax or config?; script doesn't execute on WAMP server

I am trying to write a simple PHP script: 我正在尝试编写一个简单的PHP脚本:

  1. A user visits a webpage and registers their email address and a password via a form (no other iinputs required). 用户访问网页并通过表格注册其电子邮件地址和密码(无需其他输入)。
  2. Their email and password are stored in a database and a random unique 6 digit pin is generated and stored against the email. 他们的电子邮件和密码存储在数据库中,并生成随机的唯一6位数密码,并根据电子邮件进行存储。 The pin is checked against existing pins in the database to ensure it is unique. 对照数据库中现有的引脚检查该引脚,以确保其唯一性。
  3. The pin is then displayed on the webpage and shown to the user following confirmation of their email and password submission. 然后,该图钉将显示在网页上,并在确认其电子邮件和密码提交后向用户显示。

TL;DR users sign up and get a unique pin. TL; DR用户注册并获得唯一的图钉。

I have the following PHP code, but it doesn'nt seem to execute on my WAMP server. 我有以下PHP代码,但似乎未在WAMP服务器上执行。 It just returns the .php pages as raw text. 它只是将.php页面作为原始文本返回。 Am I missing some obvious syntax or configuration? 我是否缺少一些明显的语法或配置?

<?
if($_POST['srSubmit'] && $_POST['srEmail'] && $_POST['srPass']) {
    $conn = mysql_connect('localhost','','');
    mysql_select_db("db_test",$conn);
    while(1) {
        $pin = rand(111111,999999);
        $sel = mysql_query("SELECT * FROM formusers WHERE pin = '$pin'");
        if(mysql_num_rows($sel) != 0) { continue; }

        mysql_query("INSERT INTO formusers(email,password,pin) VALUES('".$_POST['srEmail']."','".$_POST['srPass']."','".$pin."')");
        if(mysql_affected_rows()!=-1)  {
            echo "Pin:" . $pin;
            exit;
        } else {
            echo "Existing email, try again<br />";
        }
        break;
    }

}
?>
<form method="POST" action="">
<input type="email" name="srEmail" value="" placeholder="Email" /><br />
<input type="password" name="srPass" value="" placeholder="Password" /><br />
<input type="submit" name="srSubmit" value="Register" />
</form>

The opening tag for php does not look right: php的开始标记看起来不正确:

<?
if($_POST['srSubmit'] && $_POST['srEmail'] && $_POST['srPass']) {

should be: 应该:

<?php
if($_POST['srSubmit'] && $_POST['srEmail'] && $_POST['srPass']) {

Short PHP tags ( <? ?> ) are not enabled by default and that's why your code isn't working. 短PHP标记( <? ?> )默认未启用,这就是为什么您的代码无法正常工作的原因。

Change your code to use the full PHP tags () for consistency over different development environments. 更改您的代码以使用完整的PHP标记(),以确保在不同开发环境中的一致性。

<?php // <-- notice the difference

if($_POST['srSubmit'] && $_POST['srEmail'] && $_POST['srPass']) {

But, if you still want to enable short tags, you can do so by modifying the short_open_tag directive in your php.ini file, like so: 但是,如果您仍想启用短标签,则可以通过修改php.ini文件中的short_open_tag指令来启用,例如:

short_open_tag = on

For reasons of compatibility, but it's recommended to avoid using it anyway. 出于兼容性考虑,但是建议无论如何都避免使用它。 Read this answer to understand the differences. 阅读答案以了解差异。

Hope this helps! 希望这可以帮助!

1) Place 'php' after the "<" and "?" 1)将“ php”放在“ <”和“?”之后 at the beginnning 在开始时

problem persists? 问题仍然存在?

2) Sounds like a configuration thing, as if the server doesnt see your php as a php it has to execute, but as text it has to display. 2)听起来像是配置的事情,好像服务器没有将您的php视为必须执行的php一样,而是将其显示为文本。 Before I ask if youre sure the php module is loaded, is it a new installation of WAMP? 在我问您是否确定php模块已加载之前,它是WAMP的新安装吗? It happened to me a long time ago, and i remember it was a little configuration thing. 很久以前,这件事发生在我身上,我记得那是一件小事。 I also remembering finding the answer somewhere around the web ;) 我也记得在网上某处找到答案;)

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

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