Jump to content

Parse Syntax Error unexpected 'name' (T_STRING)


bennetta89
Go to solution Solved by MDCode,

Recommended Posts

Hi everyone, 

 

I am fairly new to PHP coding, and I am attempting to write a script that reads entries from a contact entry from and adds them to a database. However I keep receiving an error that states:

 

" Parse error: syntax error, unexpected 'name' (T_STRING), expecting ']' in C:\wamp\www\it665\addContact.php on line 16"

 

I have been trying to figure out what I am missing, or need to remove, and I am having the hardest time. Below is the entire code for the addContact.php script that I keep receiving this error for. I would really appreciate any feedback.

 

<?php
 
$connection = mysql_connect('localhost', 'root', ''); 
$db = mysqli_connect('localhost','root', '' ,'mycontacts');
 
if(isset($_POST['submit']))
 
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone"];
 
if (!(empty($_POST['name']))
 
{
$query = "insert into contacts (contact_name, contact_address, contact_city, contact_state,  
 
contact_zip_code, contact_phones) ";
$query .= " VALUES ( '".$name."'  ,   '".$address."'  ,  '".$city."'  ,  '".$state."'  ,  '".$zip."'  ,  
 
 '".$phone."');";
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
 
}
 
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
 
 
}
mysql_close($connection); 
 
?>
 
 
 
Link to comment
Share on other sites

Thanks for your feedback @SocialCloud !

 

I tried removing the extra "(" so that the corrected lined was:

 

if  ( !empty($_POST['name']))

 

However, I was still receiving the same error message when I tried to run the code.

 

I also tried:

 

if (!(empty($_POST['name']))) 

 

Because I thought that maybe I might be missing an extra ")" at the end of that line, but that was giving me the same error as well.

Do you see anything else that may be causing it? I really appreciate all of your help. 

Link to comment
Share on other sites

Do things like this

if(isset($_POST['name']) && trim($_POST['name']) != '') {
$name = trim($_POST['name']);
}

if(isset($_POST['address']) && trim($_POST['address']) != '') {
$address = trim($_POST['address']);
}

Then down below since made a new variable use that

if($name && $address){
//run the query
}

Can also do different ways.

if(isset($name) && isset($address)){
//run the query
}
Link to comment
Share on other sites

I wrote you up using mysqli_* functions instead of the deprecated mysql_*

With some additional error checking,messages and escaping the input before the query.

<?php
//set your credentials for mysql
$hostname = 'localhost';
$username = 'root';
$password = 'password';
$database = 'database_name';

//mysqli connection        
$con = mysqli_connect($hostname, $username, $password, $database) or die("Error " . mysqli_error($con));

$error = '';

//check if form was submitted
if (isset($_POST['submit'])) {

    //checks on each form value
    
    if (isset($_POST['name']) && trim($_POST['name']) != '') {
        $name = trim($_POST['name']);
    } else {
	    $error .= "Name not set <br />";
	}
    
    if (isset($_POST['address']) && trim($_POST['address']) != '') {
        $address = trim($_POST['address']);
    } else {
	    $error .= "Address not set <br />";
	}
    
    if (isset($_POST['city']) && trim($_POST['city']) != '') {
        $city = trim($_POST['city']);
    } else {
	    $error .= "City not set <br />";
	}
    
    if (isset($_POST['state']) && trim($_POST['state']) != '') {
        $state = trim($_POST['state']);
    } else {
	    $error .= "State not set <br />";
	}
    
    if (isset($_POST['zip']) && trim($_POST['zip']) != '') {
        $zip = trim($_POST['zip']);
    } else {
	    $error .= "Zip not set <br />";
	}
    
    if (isset($_POST['phone']) && trim($_POST['phone']) != '') {
        $phone = trim($_POST['phone']);
    } else {
	    $error .= "Phone not set <br />";
	}
    
    //check to see if all variables are set to determine doing a query
    if ($name && $address && $city && $state && $zip && $phone) {
        //escape any input
        $e_name    = mysqli_real_escape_string($name);
        $e_address = mysqli_real_escape_string($address);
        $e_city    = mysqli_real_escape_string($city);
        $e_state   = mysqli_real_escape_string($state);
        $e_zip     = mysqli_real_escape_string($zip);
        $e_phone   = mysqli_real_escape_string($phone);
        
        
        //insert query
        $query  = "INSERT INTO contacts (contact_name, contact_address, contact_city, contact_state, contact_zip_code, contact_phones) VALUES ('{$e_name}', '{$e_address}', '{$e_city}', '{$e_state}', '{$e_zip}', '{$e_phone}')";
        $insert = mysqli_query($con, $query);
        
        //check if data was inserted
        if ($insert) {
            echo "Success! Your information was added.<br />";
        } else {
            die("Error: " . mysqli_error($con));
        }
        
        
    }
}

//show if an error
if($error != ''){
echo $error;
}

//if there is a mysql connection then close it
if ($con) {
    mysqli_close($con);
}
?> 

Untested, but looks like should work.

Edited by QuickOldCar
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.