Jump to content

[SOLVED] redeclaring function?


ultratek

Recommended Posts

my server runs php version 4.4.7

i am learning from a book on php version 5

 

the book taught me a script in register.php that creates the function escape_data

the book also told me to create the function in the connect script for connecting to the sql data base

 

but the error i get is:

Fatal error: Cannot redeclare escape_data() (previously declared in \\boswinfs02\home\users\web\b1623\ez.ultratek\practice\mysql_connect.php:11) in \\boswinfs02\home\users\web\b1623\ez.ultratek\practice\register.php on line 7

 

code for register.php:

<?php
$page_title = 'Register';
include ('./includes/header.html');

if (isset($_POST['submitted'])) {
require_once ('mysql_connect.php');
function escape_data ($data) { 
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string(trim($data), $dbc);
}
$errors = array();
if (empty($_POST['first_name'])) {
$errors[] = 'You forgot to enter your first name.';
} else {
$fn = escape_data($_POST['first_name']);
}
if (empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name.';
} else {
$ln = escape_data($_POST['last_name']);
}
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';
} else {
$e = escape_data($_POST['email']);
}
if (!empty($_POST['password1'])) {
if ($_POST['password1'] != $_POST['password2']) {
$errors[] = 'Your password did not match the confirmed password.';
} else {
$p = escape_data($_POST['password1']);
}
} else {
$errors[] = 'You forgot to enter your password.';
}
if (empty($errors)) {
$query = "SELECT user_id FROM users WHERE email='$e'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
$query = "INSERT INTO users (first_name, last_name, email, password, registration_date)
VALUES ('$fn', '$ln', '$e', SHA('$p'), NOW() )";
$result = @mysql_query ($query);
if ($result) {
echo '<h1 id="mainhead">Thank You!</h1>
<p>You are now registered. In chapter 9 you will actually be able to log in!</p><p>
<br /></p>';
include ('./includes/footer.html');
exit();
} else {
echo '<h1 id="mainhead">System Error</h1>
<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>';
include ('./includes/footer.html');
exit();
}
} else {
echo '<h1 id="mainhead">Error!</h1><p class="error">The email address has already been registered.</p>';
}
} else {
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) {
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';
}
mysql_close();
}
?>
<h2>Register</h2>
<form action="register.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" maxlength="30" value="<?php if (isset($_POST['first_name'])) 
echo $_POST['first_name'];
?>" /></p>
<p>Last Name: <input type="text" name="last_name" size="20" maxlength="30" value="<?php if (isset($_POST['last_name'])) 
echo $_POST['last_name'];
?>" /></p>
<p>Email: <input type="text" name="email" size="30" maxlength="40" value="<?php if (isset($_POST['email'])) 
echo $_POST['email'];
?>" /> </p>
<p>Password: <input type="password" name="password1" size="20" maxlength="20" /></p>
<p>Confirm Password: <input type="password" name="password2" size="20" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('./includes/footer.html');
?>

 

and mysql_connect.php:

<?php
DEFINE ('DB_USER', 'ultratek');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'ultratek.easycgimysql.com');
DEFINE ('DB_NAME', 'sitename');

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );

function escape_data($data) {
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
if (function_exists('mysql_real_escape_string')) {
global $dbc;
$data = mysql_real_escape_string
(trim($data), $dbc);
} else {
$data = mysql_escape_string
(trim($data));
}
return $data;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/133794-solved-redeclaring-function/
Share on other sites

the function was recreated in the connect script (which is a require_once in the register.php)

to utilize function_exists(); ..does this mean i have to take out the function in the register.php since it is now in the connect script?...so my other scripts will work?

You can only declare a function 1 time.  You need to either remove one of the definitions, or wrap them both in a function_exists() if tree.  The problem with that though is that since the function is conditionally defined, it cannot be used anywhere above the function definition.

i took this out of the register script:

function escape_data ($data) { 
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string(trim($data), $dbc);
}

and now everything is fine...i am wondering why the book had me do it twice

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.