Hey, i have recievied Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /u3.bristol/s20/ad999/public_html/register.php on line 63, i was wondering what the problem could be, i really need help as my dissertation deadline is coming soon.


<?php # Script 12.6 - register.php
// This is the registration page for the site.

// Set the page title and include the HTML header.
$page_title = 'Register';
include ('includes/headerlogin.html');

if (isset($_POST['submit'])) { // Handle the form.

    require_once ('../mysql_connect1.php'); // Connect to the database.

    // Check for a first name.
    if (eregi ("^[[:alpha:].' -]{2,15}$", stripslashes(trim($_POST['first_name'])))) {
        $fn = escape_data($_POST['first_name']);
    } else {
        $fn = FALSE;
        echo '<p><font color="red" size="+1">Please enter your first name!</font></p>';
    }

    // Check for a last name.
    if (eregi ("^[[:alpha:].' -]{2,30}$", stripslashes(trim($_POST['last_name'])))) {
        $ln = escape_data($_POST['last_name']);
    } else {
        $ln = FALSE;
        echo '<p><font color="red" size="+1">Please enter your last name!</font></p>';
    }

    // Check for an email address.
    if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", stripslashes(trim($_POST['email'])))) {
        $e = escape_data($_POST['email']);
    } else {
        $e = FALSE;
        echo '<p><font color="red" size="+1">Please enter a valid email address!</font></p>';
    }

    // Check for a username.
    if (eregi ("^[[:alnum:]_]{4,20}$", stripslashes(trim($_POST['username'])))) {
        $u = escape_data($_POST['username']);
    } else {
        $u = FALSE;
        echo '<p><font color="red" size="+1">Please enter a valid username!</font></p>';
    }

    // Check for a password and match against the confirmed password.
    if (eregi ("^[[:alnum:]]{4,20}$", stripslashes(trim($_POST['password1'])))) {
        if ($_POST['password1'] == $_POST['password2']) {
            $p = escape_data($_POST['password1']);
        } else {
            $p = FALSE;
            echo '<p><font color="red" size="+1">Your password did not match the confirmed password!</font></p>';
        }
    } else {
        $p = FALSE;
        echo '<p><font color="red" size="+1">Please enter a valid password!</font></p>';
    }

    if ($fn && $ln && $e && $u && $p) { // If everything's OK.

        // Make sure the username is available.
        $query = "SELECT user_id FROM users WHERE username='$u'";       
        $result = @mysql_query ($query);

[COLOR=Red]     if (mysql_num_rows($result) == 0) { // Available.[/COLOR]

            // Add the user.
            $query = "INSERT INTO users (username, first_name, last_name, email, password, registration_date) VALUES ('$u', '$fn', '$ln', '$e', PASSWORD('$p'), NOW() )";       
            $result = @mysql_query ($query); // Run the query.

            if ($result) { // If it ran OK.

                // Send an email, if desired.
                echo '<h3>Thank you for registering!</h3>';
                include ('includes/footer.html'); // Include the HTML footer.
                exit();             

            } else { // If it did not run OK.
                // Send a message to the error log, if desired.
                echo '<p><font color="red" size="+1">You could not be registered due to a system error. We apologize for any inconvenience.</font></p>'; 
            }       

        } else { // The username is not available.
            echo '<p><font color="red" size="+1">That username is already taken.</font></p>'; 
        }

        mysql_close(); // Close the database connection.

    } else { // If one of the data tests failed.
        echo '<p><font color="red" size="+1">Please try again.</font></p>';     
    }

}// End of the main Submit conditional.
?>

    <h1>Register</h1>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <fieldset>

    <p><b>First Name:</b> <input type="text" name="first_name" size="15" maxlength="15" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>

    <p><b>Last Name:</b> <input type="text" name="last_name" size="30" maxlength="30" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>

    <p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p>

    <p><b>User Name:</b> <input type="text" name="username" size="10" maxlength="20" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" /> <small>Use only letters, numbers, and the underscore. Must be between 4 and 20 characters long.</small></p>

    <p><b>Password:</b> <input type="password" name="password1" size="20" maxlength="20" /> <small>Use only letters and numbers. Must be between 4 and 20 characters long.</small></p>

    <p><b>Confirm Password:</b> <input type="password" name="password2" size="20" maxlength="20" /></p>
    </fieldset>

    <div align="center"><input type="submit" name="submit" value="Register" /></div>

    </form><!-- End of Form -->

<?php // Include the HTML footer.
include ('includes/footerlogin.html');
?>

Edited 3 Years Ago by pyTony: fixed formating

I'd change the line above from

$result = @mysql_query ($query);

to

$result = @mysql_query ($query) or die(mysql_error() . '<br>'.$query);

and see if there is a problem with the query - like no database connection or something.

the result i got from one of the other forms after copying and pasted what u wrote was this:

"Unknown column 'upload_date' in 'field list'
SELECT upload_id, file_name, ROUND(file_size/1024) AS fs, description, DATE_FORMAT(upload_date, '%M %e, %Y') AS d FROM uploads ORDER BY upload_date DESC"

Just wanted to confirm if the database connection was fine, but the fields in the column are wrong

Many thanks again

you have been of great help

Hey everyone

i keep getting this error, everything else seems to be fine, but i can't find out what the solution is to this problem( i also keep getting this error when i want to add_file or add_url):-

Fatal error: Cannot redeclare escape_data() (previously declared in ../mysql_connect.php:36) in /u3.bristol/s20/ad999/public_html/add_file.php on line 13

this is the mysql_connect statement i wrote


<?php # Script 12.4 - mysql_connect.php

// This file contains the database access information. This file also establishes a connection to MySQL and selects the database.

// Set the database access information as constants.
DEFINE ('DB_USER', 'ad999');
DEFINE ('DB_PASSWORD', '040045022');
DEFINE ('DB_HOST', 'vega.soi.city.ac.uk');
DEFINE ('DB_NAME', 'ad999');

if ($dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) { // Make the connnection.

if (!mysql_select_db (DB_NAME)) { // If it can't select the database.

// Handle the error.
my_error_handler (mysql_errno(), 'Could not select the database: ' . mysql_error());

// Print a message to the user, include the footer, and kill the script.
echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>';
include_once ('includes/footer.html');
exit();

} // End of mysql_select_db IF.

} else { // If it couldn't connect to MySQL.

// Print a message to the user, include the footer, and kill the script.
my_error_handler (mysql_errno(), 'Could not connect to the database: ' . mysql_error());
echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>';
include_once ('includes/footer.html');
exit();

} // End of $dbc IF.

// Function for escaping and trimming form data.
function escape_data ($data) { //<---THIS IS THE PROBLEM
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string (trim ($data), $dbc);
} // End of escape_data() function.
?>

and heres the add_file.php.

<?php # Script 11.7 - add_file.php
// This page allows users to upload files to the server.

// Set the page title and include the HTML header.
$page_title = 'Upload a File';
include ('includes/header.html');

if (isset($_POST)) { // Handle the form.

require_once ('../mysql_connect.php'); // Connect to the database.

// Function for escaping and trimming form data.
function escape_data ($data) {
global $dbc;
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
return mysql_real_escape_string (trim ($data), $dbc);
} // End of escape_data() function.

// Check for a description (not required).
if (!empty($_POST)) {
$d = escape_data($_POST);
} else {
$d = '';
}

// Add the record to the database.
$query = "INSERT INTO uploads (file_name, file_size, file_type, description, upload_date) VALUES ('{$_FILES}', {$_FILES}, '{$_FILES}', '$d', NOW())";
$result = @mysql_query ($query);

if ($result) {

// Create the file name.
$extension = explode ('.', $_FILES);
$uid = mysql_insert_id(); // Upload ID
$filename = $uid . '.' . $extension[1];

// Move the file over.
if (move_uploaded_file($_FILES, "../uploads/$filename")) {
echo '<p>The file has been uploaded!</p>';
} else {
echo '<p><font color="red">The file could not be moved.</font></p>';

// Remove the record from the database.
$query = "DELETE FROM uploads WHERE upload_id = $uid";
$result = @mysql_query ($query);
}

} else { // If the query did not run OK.
echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>';
}

mysql_close(); // Close the database connection.

} // End of the main Submit conditional.
?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER; ?>" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="524288">

<fieldset><legend>Fill out the form to upload a file:</legend>

<p><b>File:</b> <input type="file" name="upload" /></p>

<p><b>Description:</b> <textarea name="description" cols="40" rows="5"></textarea></p>

</fieldset>

<div align="center"><input type="submit" name="submit" value="Submit" /></div>

</form><!-- End of Form -->

<?php
include ('includes/footer.html'); // Include the HTML footer.
?>

Many Thanks again

Mandip Panesar

You are including or requiring add_file.php more than once

use include_once() or require_once() to prevent the file from redeclaring the functions.

This is giving me a headache and i am sure it giving you guys it aswell, i keep recieving

Warning: move_uploaded_file(../uploads/11): failed to open stream: Permission denied in /u3.bristol/s20/ad999/public_html/add_file.php on line 48

I am working from home and transferring my .php scripts to the university server, i don't know if this could be the case!!

<?php # Script 12.8 - add_file.php
// This page allows users to upload files to the server.


// Set the page title and include the HTML header.
$page_title = 'Upload a File';
include ('./includes/header.html');


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


$counter = 3; // Number of files to allow for.


if (isset($_POST)) { // Handle the form.


require_once ('../mysql_connect.php'); // Connect to the database.


for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file.


// Create index names to refer to the proper upload and description.
$filename = 'upload' . $i;
$description = 'description' . $i;


// Check for a file.
if (isset($_FILES[$filename]) && ($_FILES[$filename] != 4)) {


// Check for a description (not required).
if (!empty($_POST[$description])) {
$d = "'" . escape_data($_POST[$description]) . "'";
} else {
$d = 'NULL';
}


// Add the record to the database.
$query = "INSERT INTO uploads (file_name, file_size, file_type, description) VALUES ('{$_FILES[$filename]}', {$_FILES[$filename]}, '{$_FILES[$filename]}', $d)";
$result = mysql_query ($query);


if ($result) {


// Return the upload_id from the database.
$upload_id = mysql_insert_id();


// Move the file over.
if (move_uploaded_file($_FILES[$filename], "../uploads/$upload_id")) { <----THIS IS THE PROBLEM


echo '<p>File number ' . ($i + 1) . ' has been uploaded!</p>';


} else { // File could not be moved.


echo '<p><font color="red">File number ' . ($i + 1) . ' could not be moved.</font></p>';


// Remove the record from the database.
$query = "DELETE FROM uploads WHERE upload_id = $upload_id";
$result = mysql_query ($query);


// Add more detailed error reporting, if desired.


}


} else { // If the query did not run OK.
echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>';
// Print the query and invoke the mysql_error() function to debug.
}


} // End of if (isset($the_file)...


} // End of FOR loop.


mysql_close(); // Close the database connection.


} // End of the main Submit conditional.
?>
<form enctype="multipart/form-data" action="add_file.php" method="post">


<fieldset><legend>Fill out the form to upload a file:</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="524288">


<?php // Create the inputs.
for ($i = 0; $i < $counter; $i++) {
echo '<p><b>File:</b> <input type="file" name="upload' . $i . '" /></p>
<p><b>Description:</b> <textarea name="description' . $i . '" cols="40" rows="5"></textarea></p><br />
';
}
?>


</fieldset>
<input type="hidden" name="submitted" value="TRUE" />
<div align="center"><input type="submit" name="submit" value="Submit" /></div>


</form>
<?php
include ('./includes/footer.html');
?>

many thanks again

i owe you one!

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.