doujingjiao0015
2017-07-26 09:57
浏览 103
已采纳

mysql在插入数据时跳转第一行

can some kindheart give me a short explanation about this?

<?php

$selection = query("SELECT * FROM rows_test"); 
/* query() is an abbreviation of mysqli_query();*/

$num_rows = mysqli_num_rows($selection);

$row_id = '';

$row_string = '';

if(isset($_POST['submit'])){

for($row_id=1; $row_id<=$num_rows; $row_id++){

    $row_string = 'ROW-' . date('Y') . '-' .$row_id;

}


$sql  = "INSERT INTO rows_test (row_id, row_string) ";
$sql .= "VALUES ('{$row_id}','{$row_string}')";

$result = query($sql);
confirm_query($result); /*Test if there is a result*/


}


?>

<form action="" method="post">
    <input type="submit" name="submit" value="Button" />
</form>

With this code php insert in the table the first row_string empty and the row_id =1 (i get that this is because i assign the value for row_id in the for loop).. after the first then it works fine as i expect so from the table id=2 it start what i want. but i need that the row_id is the same as the table id(that is of course auto increment).

By the way i know there's a way to do without row_id but i'm just testing some other ways to do things.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanmianzhou5353 2017-07-26 12:52
    已采纳

    maybe my lack in english make me goofy to be understandable, but this is what i was looking for. this works fine for what i was asking. of course is not very clean, but as i said i was testing something different. thanks for your answers.

    <?php
    
    $result = query("SELECT * FROM rows_test");
    $num_rows = mysqli_num_rows($result);
    
    $row_string = '';
    
    if(isset($_POST['submit']) && $num_rows == 0){
    
        $row_id = 1;
        $row_string = 'ORD-' . date('Y') . '-' . 1;
    
        $sql = "INSERT INTO rows_test (row_id, row_transaction_id) VALUES ('{$row_id}','{$row_string}')";
    
        $result = query($sql);
        confirm_query($result);
    
    
    }elseif(isset($_POST['submit']) && $num_rows !== 0){
    
        for($row_id=1; $row_id<=$num_rows; $row_id++){
    
    
    
        }
    
        $row_string = 'ORD-' . date('Y') . '-' .$row_id;
    
        $sql = "INSERT INTO rows_test (row_id, row_string) VALUES ('{$row_id}','{$row_string}')";
    
        $result = query($sql);
        confirm_query($result);
    
    
        }
    
    
    ?>
    
    <form action="" method="post">
        <input type="submit" name="submit" value="Button" />
    </form>
    
    点赞 评论
  • duanji9264 2017-07-26 10:04

    TRY

    for($row_id=0; $row_id<$num_rows; $row_id++){
    
    点赞 评论
  • du13932014807 2017-07-26 10:26

    if you need the same data to be inserted in the rows_test as per the count of rows_test

    change your rows_test insert query as listed below :

    $selection = query("SELECT * FROM rows_test"); 
    /* query() is an abbreviation of mysqli_query();*/
    
    $num_rows = mysqli_num_rows($selection);
    
    $sql  = "INSERT INTO rows_test (row_id, row_string) ";
    $sql .= "VALUES ('{$num_rows}',date('Y'))";
    
    点赞 评论

相关推荐 更多相似问题