Validator For Syntax Error

Hi,

I have a syntax error on the following code which I am struggling to fix. Is there any form of validator or techniqie I can use to resolve it?

syntax error, unexpected ';'
<?php
include "access.php";

if(isset($_POST['category'])){
	
	$_POST['category'] = trim($_POST['category']);
	
	if(!empty($_POST['category'])) {
		$error = "Please select a category.";
	}
	
	if(!isset($error)) {
		$category = mysql_real_escape_string($categories[$_POST['category']]);
		$linkcategory = str_replace(' ', '-',strtolower($_POST['category']);
		$linkcategory = mysql_real_escape_string($linkcategory);
		$sql = "INSERT INTO organiserdbase (category, linkcategory) VALUES ('$category','$linkcategory')";
		echo "$sql";
		/*
		$query = mysql_query($sql);
		if($query) {
		} else {
			$error = "There was a problem with the submission. Please try again.";
		}
		*/		
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
	<title></title>
</head>
<body>
	<div id="registerpagecell">
		<div id="registerpageheaderorganiser">
		Create Your Organiser Profile
		</div>
		<div id="registerform">
			<form id="form_id" name="form_id" class="appnitro" method="post" action="">
				<?php if(isset($error)){ echo "<span style=\\"color:#ff0000;\\">".$error."</span><br /><br />";}?>
				<div id="forminputcell">
					<div id="forminputleft">
						<label class="description" for="element_3">Your Job Role:</label>
					</div>
					<div id="forminputright">
						<select class="element select medium" name="category" id="category" >
							<option value="" selected="selected">Venue:</option>
							<option value="Event Manager" >Event Manager</option>
							<option value="Event Managent Courses" >Event Managent Courses</option>
							<option value="Event Management Software" >Event Management Software</option>
							<option value="Entertainment Staff" >Entertainment Staff</option>
						</select> 			
					</div> 				
				</div> 	
				<input type="submit" value="Submit" />
			</form>				
		</div> 				
	</div>
</body>
</html>

Change this…

$linkcategory = str_replace(' ', '-',strtolower($_POST['category']);

…to…

$linkcategory = str_replace(' ', '-',strtolower($_POST['category']));

Brilliant thanks,

The drop down menu now appears on the screen but whenever I choose a value and click submit it returns the error “Please select a category.”

Do I also need some form of input into the database for the linkcategory such as the following:

$linkcategory = mysql_real_escape_string(trim($_POST['linkcategory']));

You’re getting that error because of this line…

if(!empty($_POST['category'])) {

Perhaps you should change it to…

if(empty($_POST['category'])) {

Brilliant thanks, its now saying a value has entered into only the linkcategory. However nothing is currently entering the DB.

INSERT INTO organiserdbase (category, linkcategory) VALUES (‘’,‘event-management-software’)

What I dont understand is that there is no code for the value to enter the linkcategory. But there is code for the category to enter the DB.

Should I have something like this:


if(isset($_POST['form_id'])){
$category = mysql_real_escape_string(trim($_POST['category']));
	$linkcategory = mysql_real_escape_string(trim($_POST['linkcategory']));

Your original code was close, try this…

        $category = mysql_real_escape_string($_POST['category']);
        $linkcategory = str_replace(' ', '-',strtolower($_POST['category']));
        $linkcategory = mysql_real_escape_string($linkcategory);
        $sql = "INSERT INTO organiserdbase (category, linkcategory) VALUES ('$category','$linkcategory')";
        echo "$sql";

Brilliant thanks,

You know your stuff. I now get this message but nothing enters the DB. Is there anyway I can confirm it has entered the DB?

INSERT INTO organiserdbase (category, linkcategory) VALUES (‘Event Manager’,‘event-manager’)

A classic way of updating a database, and one I like goes like this…


$sql = "
    INSERT INTO
        organiserdbase
    SET
        category = '".$category."',
        linkcategory = '".$linkcategory."'";

$result = mysql_query($sql) or die("An error occurred ",mysql_error());

if($result)
{
	echo "success";
}

Something to consider for the long term is what API you want to use…

http://www.php.net/manual/en/mysqlinfo.api.choosing.php

Hi,

It came with an error for the comma in this part ccurred ",mysql_

I tried a few changes but couldn’t get it work. Can anyone advise what this error is down to please?

if(isset($_POST['category'])){
	
	$_POST['category'] = trim($_POST['category']);

	
	if(empty($_POST['category'])) {
		$error = "Please select a category.";
	}
	
	if(!isset($error)) {
	
	        $category = mysql_real_escape_string($_POST['category']);
        $linkcategory = str_replace(' ', '-',strtolower($_POST['category']));
        $linkcategory = mysql_real_escape_string($linkcategory);
      $sql = "
    INSERT INTO
        organiserdbase
    SET
        category = '".$category."',
        linkcategory = '".$linkcategory."'";

$result = mysql_query($sql) or die("An error occurred ",mysql_error());

if($result)
{
	echo "success";
}

	}
}

That’s what’s called a typo - sorry about that.

Change the line to this…

$result = mysql_query($sql) or die("An error occurred ".mysql_error());

Superb thanks, you definately know your stuff.

I have fitted this code back into my existing code so that the whole form works. However when I submit an input it creates 2 lines.

1 line has just “Event Manager” and “event-manager” in.

The other line has only “Event Manager” and all the other inputs which I am also collecting. Do you know who how I can arrange all code that it creates just one line which features “Event Manager” and “event-manager” and all the other information I am collecting.


if(isset($_POST['form_id'])){

	
	$_POST['category'] = trim($_POST['category']);

	
	if(empty($_POST['category'])) {
		$error = "Please select a category.";
	}
	
	if(!isset($error)) {
	
	        $category = mysql_real_escape_string($_POST['category']);
        $linkcategory = str_replace(' ', '-',strtolower($_POST['category']));
        $linkcategory = mysql_real_escape_string($linkcategory);
      $sql = "
    INSERT INTO
        organiserdbase
    SET
        category = '".$category."',
        linkcategory = '".$linkcategory."'";

$result = mysql_query($sql) or die("An error occurred ".mysql_error());

if($result)
{
	echo "success";
}

	}

    $firstname = mysql_real_escape_string(trim($_POST['firstname']));
    $surname = mysql_real_escape_string(trim($_POST['surname']));
    $email = mysql_real_escape_string(trim($_POST['email']));
    $website = mysql_real_escape_string(trim($_POST['website']));
    $company = mysql_real_escape_string(trim($_POST['company']));
    $building = mysql_real_escape_string(trim($_POST['building']));
    $streetname = mysql_real_escape_string(trim($_POST['streetname']));
    $state = mysql_real_escape_string(trim($_POST['state']));
    $postcode = mysql_real_escape_string(trim($_POST['postcode']));
    $country = mysql_real_escape_string(trim($_POST['country']));
    $aboutcompany = mysql_real_escape_string(trim($_POST['aboutcompany']));
    $error = false;

       if(!isset($category) || empty($category)) {
  $error = "Please select a category.";
    }

    if(!isset($firstname) || empty($firstname)) {
  $error = "Please enter a First Name.";
    }

    if(!isset($surname) || empty($surname)) {
  $error = "Please enter a Surname.";
    }

    if(!isset($email) || empty($email)) {
  $error = "Please enter an email.";
    }

    if(!isset($website) || empty($website)) {
  $error = "Please enter a Website Domain.";
    }

    if(!isset($company) || empty($company)) {
  $error = "Please enter a Company Name.";
    }

    if(!isset($building) || empty($building)) {
  $error = "Please enter a Building Name or Number.";
    }

    if(!isset($streetname) || empty($streetname)) {
  $error = "Please enter a Street Name.";
    }

    if(!isset($state) || empty($state)) {
  $error = "Please enter a State.";
    }

    if(!isset($postcode) || empty($postcode)) {
  $error = "Please enter a Zip Code/Post Code.";
    }

      if(!isset($country) || empty($country)) {
  $error = "Please select your country.";
    }

    if(!isset($aboutcompany) || empty($aboutcompany)) {
  $error = "Please enter details about your company.";
    }




    if(!$error) {
		
        $query = mysql_query("INSERT INTO organiserdbase (category,firstname, surname, email, website, company, building, streetname, state, postcode, country, aboutcompany) VALUES ('".$category."', '".$firstname."', '".$surname."', '".$email."', '".$website."', '".$company."', '".$building."', '".$streetname."', '".$state."', '".$postcode."', '".$country."', '".$aboutcompany."')");
        if($query) {
        } else {
 $error = "There was a problem with the submission. Please try again.";
        }
    }
}

?>

There’s a bit of mix of styles there, so I’ll show you one method…

<?php
function mysql_real_escape_array($t)
{
    return array_map("mysql_real_escape_string",$t);
}

function trim_array($ar)
{
    return array_map("trim",$ar);
}

if(isset($_POST['form_id']))
{
    $_POST = mysql_real_escape_array($_POST);
    $_POST = trim_array($_POST);
    $error = "";

    if(!isset($_POST['category']) || empty($_POST['category'])) {
        $error = "Please select a category.";
    }

    if(!isset($_POST['firstname']) || empty($_POST['firstname'])) {
        $error.= " Please enter a First Name.";
    }

    if(!isset($_POST['surname']) || empty($_POST['surname'])) {
        $error.= " Please enter a Surname.";
    }

    if(!isset($_POST['email']) || empty($_POST['email'])) {
        $error.= " Please enter an email.";
    }

    if(!isset($_POST['website']) || empty($_POST['website'])) {
        $error.= " Please enter a Website Domain.";
    }

    if(!isset($_POST['company']) || empty($_POST['company'])) {
        $error.= " Please enter a Company Name.";
    }

    if(!isset($_POST['building']) || empty($_POST['building'])) {
        $error.= " Please enter a Building Name or Number.";
    }

    if(!isset($_POST['streetname']) || empty($_POST['streetname'])) {
        $error.= " Please enter a Street Name.";
    }

    if(!isset($_POST['state']) || empty($_POST['state'])) {
        $error.= " Please enter a State.";
    }

    if(!isset($_POST['postcode']) || empty($_POST['postcode'])) {
        $error.= " Please enter a Zip Code/Post Code.";
    }

    if(!isset($_POST['country']) || empty($_POST['country'])) {
        $error.= " Please select your country.";
    }

    if(!isset($_POST['aboutcompany']) || empty($_POST['aboutcompany'])) {
        $error.= " Please enter details about your company.";
    }

    if($error != "")
    {
        $sql = "
        INSERT INTO
            organiserdbase
        SET
            category = '".str_replace(' ', '-',strtolower($_POST['category']))."',
            linkcategory = '".$_POST['linkcategory']."',
            firstname = '".$_POST['firstname']."',
            surname = '".$_POST['surname']."',
            email = '".$_POST['email']."',
            website = '".$_POST['website']."',
            company = '".$_POST['company']."',
            building = '".$_POST['building']."',
            streetname = '".$_POST['streetname']."',
            state = '".$_POST['state']."',
            postcode = '".$_POST['postcode']."',
            country = '".$_POST['country']."',
            aboutcompany = '".$_POST['aboutcompany']."'";

        $result = mysql_query($sql) or die("An error occurred ".mysql_error());

        if($result)
        {
            echo "success";
        }

    }
    else
    {
        echo "You have the following errors: ".$error;
    }
}

For info, the 2 functions at the top clean and trim the $_POST array.

Hi,

The code wasn’t entering either the category or linkcategory so I had a play about with it.

The code now doesn’t enter the linkcategory and it will enter information whilst some boxes are blank but will raise the error messages.

So if someone does not enter an email address it will raise the error message “Please enter an email.” but it will submit a line to the DV with the email box empty.

Any thoughts on how to fix these two?

<?php
function mysql_real_escape_array($t)
{
    return array_map("mysql_real_escape_string",$t);
}

function trim_array($ar)
{
    return array_map("trim",$ar);
}

if(isset($_POST['form_id']))
{
    $_POST = mysql_real_escape_array($_POST);
    $_POST = trim_array($_POST);
    $error = "";

    if(!isset($_POST['category']) || empty($_POST['category'])) {
        $error = "Please select a category.";
    }

    if(!isset($_POST['firstname']) || empty($_POST['firstname'])) {
        $error.= " Please enter a First Name.";
    }

    if(!isset($_POST['surname']) || empty($_POST['surname'])) {
        $error.= " Please enter a Surname.";
    }

    if(!isset($_POST['email']) || empty($_POST['email'])) {
        $error.= " Please enter an email.";
    }

    if(!isset($_POST['website']) || empty($_POST['website'])) {
        $error.= " Please enter a Website Domain.";
    }

    if(!isset($_POST['company']) || empty($_POST['company'])) {
        $error.= " Please enter a Company Name.";
    }

    if(!isset($_POST['building']) || empty($_POST['building'])) {
        $error.= " Please enter a Building Name or Number.";
    }

    if(!isset($_POST['streetname']) || empty($_POST['streetname'])) {
        $error.= " Please enter a Street Name.";
    }

    if(!isset($_POST['state']) || empty($_POST['state'])) {
        $error.= " Please enter a State.";
    }

    if(!isset($_POST['postcode']) || empty($_POST['postcode'])) {
        $error.= " Please enter a Zip Code/Post Code.";
    }

    if(!isset($_POST['country']) || empty($_POST['country'])) {
        $error.= " Please select your country.";
    }

    if(!isset($_POST['aboutcompany']) || empty($_POST['aboutcompany'])) {
        $error.= " Please enter details about your company.";
    }

    if($error != "")
    {
        $sql = "
        INSERT INTO
            organiserdbase
        SET
            category = '".$_POST['category']."',
			linkcategory = '".str_replace(' ', '-',strtolower($_POST['linkcategory']))."',
            firstname = '".$_POST['firstname']."',
            surname = '".$_POST['surname']."',
            email = '".$_POST['email']."',
            website = '".$_POST['website']."',
            company = '".$_POST['company']."',
            building = '".$_POST['building']."',
            streetname = '".$_POST['streetname']."',
            state = '".$_POST['state']."',
            postcode = '".$_POST['postcode']."',
            country = '".$_POST['country']."',
            aboutcompany = '".$_POST['aboutcompany']."'";

        $result = mysql_query($sql) or die("An error occurred ".mysql_error());

        if($result)
        {
            echo "success";
        }

    }
    else
    {
        echo "You have the following errors: ".$error;
    }
}
?>

Change this…

if($error != "")

…to…


if($error == "")

Excellent thanks,

This is all the code, its still not entering “event-manager” (something with a hyphen into the the database onto one line.

Is there something in the code which is telling it to split the insert into two lines?

<?php
function mysql_real_escape_array($t)
{
    return array_map("mysql_real_escape_string",$t);
}

function trim_array($ar)
{
    return array_map("trim",$ar);
}

if(isset($_POST['form_id']))
{
    $_POST = mysql_real_escape_array($_POST);
    $_POST = trim_array($_POST);
    $error = "";

    if(!isset($_POST['category']) || empty($_POST['category'])) {
        $error = "Please select a category.";
    }

    if(!isset($_POST['firstname']) || empty($_POST['firstname'])) {
        $error.= " Please enter a First Name.";
    }

    if(!isset($_POST['surname']) || empty($_POST['surname'])) {
        $error.= " Please enter a Surname.";
    }

    if(!isset($_POST['email']) || empty($_POST['email'])) {
        $error.= " Please enter an email.";
    }

    if(!isset($_POST['website']) || empty($_POST['website'])) {
        $error.= " Please enter a Website Domain.";
    }

    if(!isset($_POST['company']) || empty($_POST['company'])) {
        $error.= " Please enter a Company Name.";
    }

    if(!isset($_POST['building']) || empty($_POST['building'])) {
        $error.= " Please enter a Building Name or Number.";
    }

    if(!isset($_POST['streetname']) || empty($_POST['streetname'])) {
        $error.= " Please enter a Street Name.";
    }

    if(!isset($_POST['state']) || empty($_POST['state'])) {
        $error.= " Please enter a State.";
    }

    if(!isset($_POST['postcode']) || empty($_POST['postcode'])) {
        $error.= " Please enter a Zip Code/Post Code.";
    }

    if(!isset($_POST['country']) || empty($_POST['country'])) {
        $error.= " Please select your country.";
    }

    if(!isset($_POST['aboutcompany']) || empty($_POST['aboutcompany'])) {
        $error.= " Please enter details about your company.";
    }

    if($error == "")
    {
        $sql = "
        INSERT INTO
            organiserdbase
        SET
            category = '".$_POST['category']."',
			linkcategory = '".str_replace(' ', '-',strtolower($_POST['linkcategory']))."',
            firstname = '".$_POST['firstname']."',
            surname = '".$_POST['surname']."',
            email = '".$_POST['email']."',
            website = '".$_POST['website']."',
            company = '".$_POST['company']."',
            building = '".$_POST['building']."',
            streetname = '".$_POST['streetname']."',
            state = '".$_POST['state']."',
            postcode = '".$_POST['postcode']."',
            country = '".$_POST['country']."',
            aboutcompany = '".$_POST['aboutcompany']."'";

        $result = mysql_query($sql) or die("An error occurred ".mysql_error());



    }

}
?>



<div class="registerinfopagecell">
<div class="registerpageheaderorganiser">
Create Your Organiser Profile
</div>
<div class="registerform">
				
<form class="form_id"  class="appnitro"  method="post" action="">
										
	
<?php if($error) echo "<span style=\\"color:#ff0000;\\">".$error."</span><br /><br />"; ?>		
<ul >
			
					
		<div class="forminputcell">
<li class="li_1" >
		<div class="forminputleft">
<label class="description" for="element_3">Your Job Role:</label>
</div>
		<div id="forminputright">
						<select class="element select medium" name="category" id="category" >
							<option value="" selected="selected">Venue:</option>
							<option value="Event Manager" >Event Manager</option>
							<option value="Event Managent Courses" >Event Managent Courses</option>
							<option value="Event Management Software" >Event Management Software</option>
							<option value="Entertainment Staff" >Entertainment Staff</option>
						</select> 			
					</div> 	
		
		</div>
</li>
</div>


Try changing this line…

linkcategory = '".str_replace(' ', '-',strtolower($_POST['linkcategory']))."',

…to…

linkcategory = '".str_replace(' ', '-',strtolower($_POST['category']))."',

Brilliant, thats now working perfectly. Thanks mate.