mercredi 13 décembre 2017

Repeating same data in mysql

I faced a problem. I display a database table from mysql to my web page and using checkbox to tick the data and store to another database table. However, when I add 5 persons in 1 time and there is only four person is added to my database table and the another one is repeated with the same person's ID and name. Means that each time I add, there is a person's name is keep repeating. Any help??Below is my code.

<?php
try{
$con = new PDO("mysql:host=localhost;dbname=kgruum member", "root", "");
$sql = $con->query("SELECT * FROM member");

echo"<table class='info' align='center' border='1'>";
echo"<tr><td width='10'></td>
<td width='10'><b>ID</b></td>
<td width='500'><b>Name</b></td>
<td width='50'><b>Handicap</b></td><tr>";
foreach($sql as $row) 
{
    $ID = $row["ID"];
    $Name = $row["Name"];
    $Handicap = $row["Handicap"];
echo"<tr>
<td><form method='POST' action='Add participant.php'><input type='checkbox' name='insert[]' value='$ID|$Name|$Handicap'></td>
<td>$ID</td>
<td>$Name</td>
<td>$Handicap</td><tr>";
}

echo"</table><div align='center'><input type='submit' value='submit' src='add selected button.png' alt='submit Button' onmouseover='this.src='pink add selected button.png'' onmouseout='this.src='add selected button.png'' name='add_btn' id='add_btn'></div><br></form>";


if(isset($_POST['add_btn'])){
if(!empty($_POST['insert'])) {
    foreach($_POST['insert'] as $check) {
    $st=$con->prepare("INSERT INTO participants(ID,Name,Handicap) VALUES('$ID','$Name','$Handicap')");
    $values = explode('|', $check);
    $ID = $values[0];
    $Name = $values[1];
    $Handicap = $values[2];
    $st->execute();

    }echo "<script type='text/javascript'>
    alert('Successful Insert ! ');
    window.location.href = 'Add participant.php';
    </script>";
}else{
echo "<script type='text/javascript'>alert('You didn't choose which user you want to insert ! ')</script>";
}
}
}catch(PDOException $e)
{
echo "error".$e->getMessage();
}
?>




Aucun commentaire:

Enregistrer un commentaire