简体   繁体   English

找不到持续错误 - PHP

[英]Couldn't find constant error - PHP

Hi can some one please point out my mistake here.嗨,有人可以在这里指出我的错误。 I've got two PHP files.我有两个 PHP 文件。 First view.php goes as follows;第一个view.php如下; and the second as processor.php I see the if(constant('SELECTitem') == $i) line doesnt capture the desired input to be compared with $i hence straight goes to 'else' validation.第二个作为 processor.php 我看到 if(constant('SELECTitem') == $i) 行没有捕获要与 $i 进行比较的所需输入,因此直接进入'else'验证。 Please correct me.请纠正我。 Thanks.谢谢。

view.php查看.php

<?php
$con=mysql_connect('localhost','root') or die ("Server connection failure!");
$db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the database");
$SQL="SELECT sbstart, sbend FROM newchk";
$run=mysql_query($SQL,$con) or die ("SQL Error");
$nor=mysql_num_rows($run);

define("SELECTitem", form.options.value);
?>
<html>
<head><title></title>
<script type="text/javascript">
function ValidateData(form) 
{
var TextIn = document.getElementById('txtN');
if(form.txtN.value == "")
{
alert("Text field is empty"); 
return false;
}else{
alert ((form.options[form.options.selectedIndex].value) + (form.txtN.value));
}
}
</script>
</head>
<body>
<form onsubmit="return ValidateData(this)" method="POST" action="processor.php">
<select STYLE='width:90px' name="options"><?php
while ($rec = mysql_fetch_array($run))
{
    for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
    {
    echo "<option id='options' value='$i'>$i<br></option>";
    }
}
?>
</select>
<input type="text" id="txtN">
<input type="submit" name="subN" value="Save">
</form>

</body>
</html> 

processor.php处理器.php

<?php
$con=mysql_connect('localhost','root') or die ("Server connection failure!");
$db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the database");
$SQL="SELECT * FROM newchk";
$run=mysql_query($SQL,$con) or die ("SQL Error");
$nor=mysql_num_rows($run);

while ($rec = mysql_fetch_array($run))
{
for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
    {
    if(constant('SELECTitem') == $i)
     {
        if($rec['totsb'] <= "0")
        {
        echo "You have already entred this cheque number."; 
        return false;
        } else {
        echo "You can proceed withi this entry"; 
        return false;
        }
     }
     else 
     { echo "Error: Cant find choosen in the databse"; 
      return false;
     }
    }
}
?>

When you submit to processor.php, the constant you created in the previous page will not be carried over. 当您提交到processor.php时,您在上一页中创建的常量将不会被转移。 Also, when you are defining the constant, you cannot access JavaScript in PHP. 此外,在定义常量时,无法在PHP中访问JavaScript。 So the constant you created... 所以你创造的常数......

define("SELECTitem", form.options.value);
echo SELECTitem; // returns "formoptionsvalue" (form concatenated with options and value)

What you should do is submit form.options.value and access it via $_POST. 你应该做的是提交form.options.value并通过$ _POST访问它。

 define("SELECTitem", form.options.value);

What does "form.options.value" mean? “form.options.value”是什么意思? If i understand it right, it's your JS code. 如果我理解正确,那就是你的JS代码。 But it doesn't work like that. 但它并不像那样。

Also u can not access constant from other process. 你也无法从其他进程访问常量。

You need to read some PHP books first. 你需要先阅读一些PHP书籍。

Title of question is good and generic: Couldn't find constant error - PHP but the body of it is very particular问题的标题很好而且很笼统: Could not find constant error - PHP but the body is very special

For all of you who come here for the general answer please note that when you get here for a not working class constant then please check if you are using the fully qualified name of the class otherwise it will not work.对于所有来到这里寻求一般答案的人,请注意,当您来到这里是因为一个非工作类常量,请检查您是否使用了类的完全限定名称,否则它将无法工作。

NOK: constant('MyClass::MY_CONST'); NOK: constant('MyClass::MY_CONST');

OK: constant(MyClass::class . '::MY_CONST'); OK: constant(MyClass::class . '::MY_CONST');

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

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