创建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.

我有一个将数据插入SQL表的函数。 p>

  public function process_form(){
#单击提交按钮时启动函数
 if(isset($ _ POST ['submit'])){
#分配表单数据
 $ 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']; \  n #SQL查询
 $ q ='
 INSERT INTO匹配(
 match_id,
 host,
 game,
 region,
 match_name,
 match_mode,
 started,
 start_time,
 完成,
 finish_time,
验证,
 team_a,
 team_b,
 bid 
)
 VALUES(
 null,
?,
?,
?,
?,
  ?,
  0,
 null,
 0,
 null,
 0,
 null,
 null,
?
); 
'; 
#处理SQL 
 if($ stmt = $  this-&gt; db-&gt; prepare($ q)){
 $ stmt-&gt; bind_param('iiisii',$ host,$ game,$ region,$ name,$ mode,$ bid); 
 $  stmt-&gt; bind_result()
 $ stmt-&gt; execute(VARIABLE_GOES_HERE); 
 $ stmt-&gt; close(); 
} 
} 
} #endfunc(process_form)
  code>   pre> 
 
 

使用action属性收集所需数据的表单,该属性将用户重定向到显示插入数据的下一页。 p>

 &lt; form method  =“POST”action =“match.php?id = VARIABLE_GOES_HERE”&gt; 
  code>  pre> 
 
 

我想在$ q中扩展我的SQL查询,这将给出我的ID 当前创建的行然后在$ stmt-&gt; bind_result()填充变量与id。 然后将在表单元素的action属性中添加。 p> div>

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.'">