有些善意的人可以给我一个简短的解释吗?
<?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>
使用此代码php在表中插入第一个row_string为空且row_id = 1(我知道这是因为我在for循环中为row_id赋值)。 在第一个之后然后它工作正常,因为我期望从表id = 2它开始我想要的。 但我需要row_id与表id相同(当然是自动增量)。
顺便说一下,我知道有一种方法可以不用row_id,但我只是测试一些其他方法来做事。
答案 0 :(得分:0)
TRY
for($row_id=0; $row_id<$num_rows; $row_id++){
答案 1 :(得分:0)
如果您需要根据rows_test
的计数在rows_test中插入相同的数据更改您的rows_test插入查询,如下所示:
$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'))";
答案 2 :(得分:0)
<?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>