php/mysql form validation - mysql_num_rows error

Hello all, this is my first post on this forum :slight_smile:

I have a form that posts data to a mysql table, and part of the validation is to make sure that the entered value does not already exist. I’m using mysql_num_rows to find a match, but receiving this error:

//this is me echoing the query
SELECT * FROM `mwm7d_CowURL` WHERE `cowurl`='test'

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mysite/public_html/cowurl.php on line 67
SELECT * FROM `mwm7d_CowURL` WHERE `cowurl`='test' 

I tried the above query in phpMyAdmin, and it works fine… I get 31 results. So I’m not sure what could be failing here…

Here’s the full page. Please take a look around lines 64-68 to see what I’m doing, and if possible…show me the error of my ways :slight_smile: Thanks in advance!!

[php]

<?php error_reporting(E_ALL); ini_set('display_errors', '1'); // creates the new record form function renderForm($cowurl, $error) { // if there are any errors, display them if ($error != '') { echo '
'.$error.'
'; } ?>
Cow Marvel URL: *

* required

<?php }

// connect to the database
include(‘connectdb.php’);

// check if the form has been submitted. If it has, start to process the form and save it to the database
if (
isset($_POST[‘submit’]) &&
isset($_POST[‘cowurl’]) &&
!empty($_POST[‘cowurl’])
)
{
/*

  • Specify the field names that are in the form. This is meant
  • for security so that someone can’t send whatever they want
  • to the form.
    */
    $allowedFields = array(
    ‘cowurl’
    );

// Specify the field names that you want to require…
$requiredFields = array(
‘cowurl’
);

// Loop through the $_POST array, which comes from the form…
$errors = array();
foreach($_POST AS $key => $value)
{
// first need to make sure this is an allowed field
if(in_array($key, $allowedFields))
{
$$key = $value;

    // is this a required field?
    if(in_array($key, $requiredFields) && $value == '')
    {
        $errors[] = "The field $key is required.";
    }
}
$query = "SELECT * FROM `mwm7d_CowURL` WHERE `cowurl`='$cowurl'";
echo $query."\n<br>";
$findmatch = mysql_query($query) or die("Error: ". mysql_error());
if (mysql_num_rows(mysql_query($findmatch)) > 0 ) {
        $errors[] = "This URL already exists";
}

}

// were there any errors?
if(count($errors) > 0)
{
$errorString = ‘

There was an error processing the form.

’;
$errorString .= ‘
    ’;
    foreach($errors as $error)
    {
    $errorString .= “
  • $error
  • ”;
    }
    $errorString .= ‘
’;
}
else {
// save the data to the database
$ipaddress = $_SERVER[‘REMOTE_ADDR’];
$cowurl = mysql_real_escape_string($_POST[‘cowurl’]);
$userid = ‘42’;
$timestamp = date(“Y-m-d H:i:s”,time());
mysql_query(“INSERT mwm7d_CowURL SET cowurl=’$cowurl’, ipaddress=’$ipaddress’,userid=’$userid’,datestamp=’$timestamp’”)
or die(mysql_error());
header(“Location: cowurlistlist.php”);
}
}else
// if the form hasn’t been submitted, display the form
{
renderForm(’’,’’);
}
?>
[/php]

Nevermind, solved it…once I’d found the main problem, a bunch more popped up! :wink:

Thanks anyway!

Sponsor our Newsletter | Privacy Policy | Terms of Service