尝试将主键值分配给按钮时出错?

时间:2018-04-22 14:23:31

标签: php mysql database syntax-error primary-key

*the data from the table is printed from the database(phpmyadmin)

“查看更多”按钮需要具有每行的主键值,以便为每个事件显示不同的信息。

代码:

<?php
try{
// Run a SQL query
  $sqlstr = "SELECT * FROM events";
  $rows=$db->query($sqlstr);
//loop through all the returned records and display them in a table
  foreach ($rows as $row) {
    echo  "<tr><td >" . $row['eventCategory'] . "</td><td >" . $row['eventName'] . "</td><td >" . $row['time']. "</td><td >" . $row['description']  . "</td><td >" . "<a href='viewmore.php?event_id= $row['event_id']' id='viewbtn' name='viewbtn'>view more</a>"."</td></tr >" ;


  }

  echo "</table> <br>";
} catch (PDOException $ex){
  //this catches the exception when the query is thrown
  echo "Sorry, a database error occurred when querying the vehicle records. Please try again.<br> ";
  echo "Error details:". $ex->getMessage();
}

?>

当我运行它时,我收到此错误..

解析错误:语法错误,意外''(T_ENCAPSED_AND_WHITESPACE),期待' - '或标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)

错误在这一行

echo  "<tr><td >" . $row['eventCategory'] . "</td><td >" . $row['eventName'] . "</td><td >" . $row['time']. "</td><td >" . $row['description']  . "</td><td >" . "<a href='viewmore.php?event_id= $row['event_id']' id='viewbtn' name='viewbtn'>view more</a>"."</td></tr >" ;

1 个答案:

答案 0 :(得分:0)

在$ row ['event_id']附近添加“”正常

<?php
try{
// Run a SQL query
  $sqlstr = "SELECT * FROM events";
  $rows=$db->query($sqlstr);
//loop through all the returned records and display them in a table
  foreach ($rows as $row) {
    echo  "<tr><td >'".$row['eventCategory']."'</td><td >'".$row['eventName']."'</td><td >'".$row['time']."'</td><td >'".$row['description']."'</td><td >" . "<a href='viewmore.php?event_id= ".$row['event_id']."' id='viewbtn' name='viewbtn'>view more</a>"."</td></tr >" ;


  }

  echo "</table> <br>";
} catch (PDOException $ex){
  //this catches the exception when the query is thrown
  echo "Sorry, a database error occurred when querying the vehicle records. Please try again.<br> ";
  echo "Error details:". $ex->getMessage();
}

?>