Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm a PHP beginner, I'm trying to design a website using php and phpmyadmin. This website is supposed to view all records in a database, delete and add records. The following code is to add a new record, I have errors on lines 46,53,57.

and when I try to register a user I get this message:
ERROR
Fatal error: Uncaught Error: Call to undefined function eregi() in C:\xampp\htdocs\page\register.php:10 Stack trace: #0 C:\xampp\htdocs\index.php(295): include() #1 {main} thrown in C:\xampp\htdocs\page\register.php on line 10


What is causing these issues and what do I need to do to resolve them?

here the code i have:

PHP
<h1>Регистрация</h1>
<?php
if(isset($_POST["regsub"])){
	$err = NULL;
	db_connect();
	if(db_rows(db_query("SELECT `id` FROM `".DBpref."users` WHERE `login` = '".db_string($_POST["login"])."' OR `email` = '".db_string($_POST["email"])."'")) != 0)
		$err .= "<li>Логин или E-mail уже используются</li>";
	if(strlen($_POST["login"]) < 4)
		$err .= "<li>Минимальная длина логина 4 символа</li>";
	if ( !@eregi("^[a-zA-Z0-9_]+$", $_POST["login"]))
		$err .= "<li>В логине допускается только кирилица и символ _</li>";
	if(!checkEmail($_POST["email"]))
		$err .= "<li>E-mail введен не корректно</li>";
	if(strlen($_POST["pass"]) < 6)
		$err .= "<li>Минимальная длина пароля 6 символов</li>";
	if($_POST["pass"] != $_POST["pass2"])
		$err .= "<li>Подтверждение пароля не совпадает</li>";
		
	if($err == NULL){
		
		$ref = NULL;
		if(isset($_SESSION["ref"])){
			if(db_rows(db_query("SELECT `id` FROM `".DBpref."users` WHERE `login` = '".db_string($_SESSION["ref"])."'")) == 1)
				$ref = $_SESSION["ref"];
		}
	
		db_query("INSERT INTO `".DBpref."users` (`balance`, `login`, `email`, `pass`, `ref`, `wmid`, `wmr`, `genus`, `regdat`, `ip`, `logdat`) VALUES (1, '".db_string($_POST["login"])."', '".db_string($_POST["email"])."', '".heshPass($_POST["pass"])."', '".db_string($ref)."', '', '', '".intval($_POST["genus"])."', '".date("Y-m-d H:i:s")."', '".getIP()."', '".date("Y-m-d H:i:s")."')");
		
		$_SESSION["login"] = $_POST["login"];
		
		echo "<ul class='good'><li>Регистрация успешно завершинна</li></ul>
			<script type='text/javascript'>
				location.replace('/?p=profile');
			</script>";
		
		$regok = true;
	}else{
		echo "<ul class='error'>".$err."</ul>";
	}
	
	db_close();
}
?>

<?php
if($regok == false){
?>
<form action="/?p=register" method="post">
<input name="ref" type="hidden" value="<?php echo htmlspecialchars($_SESSION["ref"]); ?>">
<table class="table2">
  <tr>
    <td>Логин</td>
    <td><input name="login" type="text" size="30" maxlength="30" value="<?php echo htmlspecialchars($_POST["login"]); ?>" /></td>
  </tr>
  <tr>
    <td>E-mail</td>
    <td><input name="email" type="text" size="30" maxlength="60" value="<?php echo htmlspecialchars($_POST["email"]); ?>" /></td>
  </tr>
  <tr>
    <td>Пароль</td>
    <td><input name="pass" type="password" size="30" maxlength="25" /></td>
  </tr>
  <tr>
    <td>Подтверждение пароля</td>
    <td><input name="pass2" type="password" size="30" maxlength="25" /></td>
  </tr>
  <tr>
    <td> </td>
    <td><input name="regsub" type="submit" value="Регистрация" /></td>
  </tr>
</table>

</form>
<?php
}
?>


What I have tried:

PHP books, Online websites, youtube and consulting fellow colleagues.
Posted
Updated 9-Jul-23 3:14am
v2
Comments
Member 15627495 9-Jul-23 5:42am    
you forget ton init '$regok' at start of your code.

the faulty code happens when you use $regok , but you create $regok inner the if/else, while you quit the first if/else , $regok is delete ( because of the place you init it. )

Look at toue code
PHP
<?php
if(isset($_POST["regsub"])){
		
	if($err == NULL){ // What is the value of regok when condition is not met ?
		
		
		$regok = true;
	}else{
		echo "<ul class='error'>".$err."</ul>";
	}
	
	db_close();
}

if($regok == false){
...
 
Share this answer
 
I am not sure which version of PHP you are using but the error gives you the hint -
Quote:
Call to undefined function eregi()


The erigi function has been removed since version 7.0.0 of PHP, if you are on a higher version, it will not recognise 'eregi' function - eregi PHP[^]
Quote:
Warning
This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

Alternatives to this function include:

preg_match() (with the i (PCRE_CASELESS) modifier)


You can read up more on regular expressions in PHP at - PHP Regular Expressions[^]

You need to change the line to -
PHP
if ( !@eregi("^[a-zA-Z0-9_]+$", $_POST["login"])) 
//eregi is removed... will error
if (!preg_match("/^[a-zA-Z0-9_]+$/", $_POST["login"]))


Your code will thus read -
PHP
if (isset($_POST["regsub"])) {
    $err = NULL;
    db_connect();
    if (db_rows(db_query("SELECT `id` FROM `".DBpref."users` WHERE `login` = '".db_string($_POST["login"])."' OR `email` = '".db_string($_POST["email"])."'")) != 0)
        $err .= "<li>Логин или E-mail уже используются</li>";
    if (strlen($_POST["login"]) < 4)
        $err .= "<li>Минимальная длина логина 4 символа</li>";
    if (!preg_match("/^[a-zA-Z0-9_]+$/", $_POST["login"]))
        $err .= "<li>В логине допускается только кирилица и символ _</li>";
    if (!checkEmail($_POST["email"]))
        $err .= "<li>E-mail введен не корректно</li>";
    if (strlen($_POST["pass"]) < 6)
        $err .= "<li>Минимальная длина пароля 6 символов</li>";
    if ($_POST["pass"] != $_POST["pass2"])
        $err .= "<li>Подтверждение пароля не совпадает</li>";

    if ($err == NULL) {

        $ref = NULL;
        if (isset($_SESSION["ref"])) {
            if (db_rows(db_query("SELECT `id` FROM `".DBpref."users` WHERE `login` = '".db_string($_SESSION["ref"])."'")) == 1)
                $ref = $_SESSION["ref"];
        }

        db_query("INSERT INTO `".DBpref."users` (`balance`, `login`, `email`, `pass`, `ref`, `wmid`, `wmr`, `genus`, `regdat`, `ip`, `logdat`) VALUES (1, '".db_string($_POST["login"])."', '".db_string($_POST["email"])."', '".heshPass($_POST["pass"])."', '".db_string($ref)."', '', '', '".intval($_POST["genus"])."', '".date("Y-m-d H:i:s")."', '".getIP()."', '".date("Y-m-d H:i:s")."')");

        $_SESSION["login"] = $_POST["login"];

        echo "<ul class='good'><li>Регистрация успешно завершена</li></ul>
            <script type='text/javascript'>
                location.replace('/?p=profile');
            </script>";

        $regok = true;
    } else {
        echo "<ul class='error'>".$err."</ul>";
    }

    db_close();
}
 
Share this answer
 
You need to understand scope rules: PHP Variables Scope[^]
When you first create a variable inside a code block (delimited by "{" and "}" it exists only while that code block is executing - when the code reaches the "}" it is deleted.

So when you meet is again in the if test, there is no such variable in existence, and you get an error. Declare it at global scope and you will be fine.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900