1

I made a form for pharma products where user select the manufacturers name and write the details of the project. Everything is up and running fine except only this issue. Its been hours and i cant figure it out.

The problem is Its only selects the first row and ignore new entries. What i want is it will select every row or every new entry and insert into second database here the code is working with no errors but update only the first row.

 //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);
      }
    }

With prepared statement:

$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);
    }
  • You should call `mysqli_fetch_array` in a loop. Check out the user notes in [the manual](http://php.net/manual/en/mysqli-result.fetch-array.php) – apokryfos Nov 20 '17 at 13:26
  • you mean fetch the array using while loop ..right .? – Linnea Anderson Nov 20 '17 at 13:29
  • Why not use `INSERT INTO final1 (Name, Category) SELECT name, category FROM test` and let the database handle the copy. – Raymond Nijland Nov 20 '17 at 13:43
  • I dont want to just copy the data from 1 table to another i want to validate the data first and then move the data to second database. – Linnea Anderson Nov 20 '17 at 13:51
  • It probably selects all records from your table, but since you're calling mysqli_fetch_array() only once, you're only seeing the first result. – Niellles Nov 20 '17 at 14:02

1 Answers1

1

you need while loop

//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);
      }
    }
}
?>
pedram shabani
  • 1,654
  • 2
  • 20
  • 30
  • haha lol xD while loop update all the entries in just 1 single click.. means eg: i have 10 entries in a database and submit a new entry which is 11 its taking all 11 entries and insert all 11 at once but thanks for the try .. – Linnea Anderson Nov 20 '17 at 13:37
  • This is the way to go though. If you want to loop over only new records (i.e. entries that are in test and not in final1) just alter your query to [select only those records](https://stackoverflow.com/questions/2686254/how-to-select-all-records-from-one-table-that-do-not-exist-in-another-table) and then loop over them.. – Niellles Nov 20 '17 at 14:01
  • excuse me.we have Earthquake.for this i answer late.((I hope this isn't rude))you can change your query to get the desired result .for example:-> $perm = "SELECT * FROM test WHERE id='$id' .....ect "; – pedram shabani Nov 20 '17 at 15:04