bennetta89 Posted October 26, 2014 Share Posted October 26, 2014 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/292085-parse-syntax-error-unexpected-name-t_string/ Share on other sites More sharing options...
MDCode Posted October 26, 2014 Share Posted October 26, 2014 if (!(empty($_POST['name'])) Extra ( after the ! Quote Link to comment https://forums.phpfreaks.com/topic/292085-parse-syntax-error-unexpected-name-t_string/#findComment-1494858 Share on other sites More sharing options...
bennetta89 Posted October 27, 2014 Author Share Posted October 27, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292085-parse-syntax-error-unexpected-name-t_string/#findComment-1494862 Share on other sites More sharing options...
Solution MDCode Posted October 27, 2014 Solution Share Posted October 27, 2014 $phone = $_POST['phone"]; You have a quote (") instead of an apostrophe (') Quote Link to comment https://forums.phpfreaks.com/topic/292085-parse-syntax-error-unexpected-name-t_string/#findComment-1494863 Share on other sites More sharing options...
QuickOldCar Posted October 27, 2014 Share Posted October 27, 2014 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 } Quote Link to comment https://forums.phpfreaks.com/topic/292085-parse-syntax-error-unexpected-name-t_string/#findComment-1494864 Share on other sites More sharing options...
bennetta89 Posted October 27, 2014 Author Share Posted October 27, 2014 Thanks so much both of you all! I think it is fixed now!!! Your feedback was awesome!!! Quote Link to comment https://forums.phpfreaks.com/topic/292085-parse-syntax-error-unexpected-name-t_string/#findComment-1494870 Share on other sites More sharing options...
QuickOldCar Posted October 27, 2014 Share Posted October 27, 2014 (edited) 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 October 27, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/292085-parse-syntax-error-unexpected-name-t_string/#findComment-1494875 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.