3

I have the following code in php

$countryiso = mysql_query("SELECT distinct name as name FROM location_country where code NOT IN('A1','A2','AP','EU') order by name");
echo '<table>';
echo '<th>Country</th><th> Add/Remove </th>';
while ($row = mysql_fetch_assoc($countryiso)) {
 echo '<tr>';
 echo '<td>'. $row['name'] . '</td>';
 echo '<td><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="id" value="<?php echo $id; ?>" /><input type="checkbox" name="checkinsat" value="1"<?php if($data['checkinsat'] == '1') echo 'checked'; ?>/><input type="submit" ></form></td>';
 echo '</tr>';
}
 echo '</table>';

And i get the error

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in don't know where i am mistaking.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Adrian
  • 2,273
  • 4
  • 38
  • 78

3 Answers3

2

Don't echo the parameters into the form using php tags, use a string concatenation instead.

Change:

echo '<td><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="id" value="<?php echo $id; ?>" /><input type="checkbox" name="checkinsat" value="1"<?php if($data['checkinsat'] == '1') echo 'checked'; ?>/><input type="submit" ></form></td>';

to:

echo '<td><form method="post" action="'. $_SERVER['PHP_SELF'] .'"> <input type="hidden" name="id" value="'. $id .'" /><input type="checkbox" name="checkinsat" value="1"'. ($data['checkinsat'] == '1'? 'checked' : '') .'<input type="submit" ></form></td>';

Further, do not use mysql_* functions - it's deprecated (see red box) and vulnerable to sql-injection. Use PDO or MySQLi.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

you have typo here

echo '<td><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="id" value="<?php echo $id; ?>" /><input type="checkbox" name="checkinsat" value="1"<?php if($data['checkinsat'] == '1') echo 'checked'; ?>/><input type="submit" ></form></td>';

should be

echo '<td><form method="post" action="'.$_SERVER['PHP_SELF']'"><input type="hidden" name="id" value="'.$id.'" /><input type="checkbox" name="checkinsat" value="1"';
 if($data['checkinsat'] == '1') 
  echo 'checked />';
 echo '<input type="submit" ></form></td>';

you are writing <?PHP ... ?> in php tag and that is causes this error

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

The problem is your echo on line 7. Your doing <?php echo ... ?> inside of an echo. You should instead concatenate the values into the string.

Instead of:

 echo '<td><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="id" value="<?php echo $id; ?>" /><input type="checkbox" name="checkinsat" value="1"<?php if($data['checkinsat'] == '1') echo 'checked'; ?>/><input type="submit" ></form></td>';

Try this:

$checked = ($data['checkinsat'] == '1') ? 'checked' : '';
echo '<td><form method="post" action="'.$_SERVER['PHP_SELF'].'"><input type="hidden" name="id" value="'. $id.'" /><input type="checkbox" name="checkinsat" value="1" '.$checked.'/><input type="submit" ></form></td>';
Jeemusu
  • 10,415
  • 3
  • 42
  • 64