仅插入第一行并忽略新条目

时间:2017-11-20 13:23:33

标签: php mysql

我为制药产品制作了一个表格,用户可以选择制造商名称并写下项目的详细信息。除了这个问题,一切都正常运行。已经好几个小时了,我无法弄明白。

问题是它只选择第一行并忽略新条目。我想要的是它会选择每一行或每一个新条目并插入第二个数据库,这里代码没有错误但只更新第一行。

 //Select Everything from database
$perm = "SELECT * FROM test";
$result34 = mysqli_query($dbc, $perm);

if ($perm) {
  //How many rows
  $count = mysqli_num_rows($result34);

  //Retrieve data

  if($count>=1) {

    $rows = mysqli_fetch_array($result34);
    $namex= $rows['name'];
    $categoryx = $rows['category'];

    //takeout the spaces and strip tags

    $namey = strip_tags($namex);
    $category = str_replace(' ', '', $category);


    //INSERT INTO TABLES

    $sql34 = "INSERT INTO final1 (Name, Category) VALUES ($namey, $category);";
    $result55 = mysqli_query($dbc, $sql34);
      }
    }

准备好的陈述:

$sql2 = "INSERT INTO final1 (Name, Category) VALUES (?, ?);";
    $stmt1 = mysqli_stmt_init($dbc);

    if (!mysqli_stmt_prepare($stmt1, $sql2)) {
      echo "SQL error";
    } else {
      mysqli_stmt_bind_param($stmt1, "ss", $namey, $category);
      mysqli_stmt_execute($stmt1);
    }

1 个答案:

答案 0 :(得分:1)

你需要while循环

//Select Everything from database
$perm = "SELECT * FROM test";
$result34 = mysqli_query($dbc, $perm);

if ($result34) {
  //How many rows
  $count = mysqli_num_rows($result34);

  //Retrieve data

  if($count>=1) {
    while($rows = mysqli_fetch_array($result34) ){
    $namex= $rows['name'];
    $categoryx = $rows['category'];
    //takeout the spaces and strip tags
    $namey = strip_tags($namex);
    $category = str_replace(' ', '', $category);
    //INSERT INTO TABLES
    $sql34 = "INSERT INTO final1 (Name, Category) VALUES ($namey, $category);";
    $result55 = mysqli_query($dbc, $sql34);
      }
    }
}
?>