duansanzi5265 2015-12-10 21:39
浏览 34
已采纳

创建SQL行后,在PHP bind_result方法中获取有关该行的数据

I have a function that inserts data into SQL table.

public function process_form(){
    # Start function when submit button is clicked
    if (isset($_POST['submit'])) {
      # Assign form data
      $host = $_SESSION['logged_user'];
      $game = str_replace('g','',$_POST['game']);
      $region = str_replace('r','',$_POST['region']);
      $name = $_POST['match-name'];
      $mode = str_replace('mode_0','',$_POST['mode']);
      $bid = $_POST['bid'];
      # SQL query
      $q = '
        INSERT INTO matches(
          match_id,
          host,
          game,
          region,
          match_name,
          match_mode,
          started,
          start_time,
          finished,
          finish_time,
          validated,
          team_a,
          team_b,
          bid
        )
        VALUES (
          null,
          ?,
          ?,
          ?,
          ?,
          ?,
          0,
          null,
          0,
          null,
          0,
          null,
          null,
          ?
        );
      ';
      # Process SQL
      if ($stmt = $this->db->prepare($q)) {
        $stmt->bind_param('iiisii',$host,$game,$region,$name,$mode,$bid);
        $stmt->bind_result()
        $stmt->execute(VARIABLE_GOES_HERE);
        $stmt->close();
      }
    }
  }#endfunc(process_form)

And form that gathers needed data with action attribute which redirects user to next page where inserted data is displayed.

<form method="POST" action="match.php?id=VARIABLE_GOES_HERE">

I want to extend my SQL query within $q which will give me Id of currently created row and then within $stmt->bind_result() fill variable with the id. which will be then added within action attribute in form element.

  • 写回答

1条回答 默认 最新

  • dongwu3747 2015-12-10 23:18
    关注

    What was needed is, to add this line of code: $last_id = $stmt->insert_id;

      if ($stmt = $this->db->prepare($q)) {
            $stmt->bind_param('iiisii',$host,$game,$region,$name,$mode,$bid);
            $stmt->execute();
            $last_id = $stmt->insert_id;
            $stmt->close();
    

    and then within form this:

    <form method="POST" action="match.php?id='.$last_id.'">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效