MySQL veritabanı girdileri karşılık gelen veri ekleme

0 Cevap php

Ben bir veritabanına veri gönderen bir başvuru formu var. Sonra çıktı bu HTML veritabanından veri. Şimdi, geri veritabanına bir form üzerinden veri bir daha parça göndermek zorunda. Ben yeni veri mevcut kayıtlara karşılık nasıl anlamaya olamaz.

Bu benim çıkış kodu (veritabanından veri ek veriler için yeni bir form ile HTML) 'dir:

<html>
<body>
<?php

mysql_connect(localhost,root,root);
@mysql_select_db(test) or die( "Unable to select database");
$query="SELECT * FROM submission";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Read By</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date/Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">URL</font></th>
<th><font face="Arial, Helvetica, sans-serif">Uploaded File</font></th>
<th><font face="Arial, Helvetica, sans-serif">Email / Twitter</font></th>
<th><font face="Arial, Helvetica, sans-serif"></font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$Date=mysql_result($result,$i,"Date");
$url=mysql_result($result,$i,"url");
$uploadedfile=mysql_result($result,$i,"uploadedfile");
$contact=mysql_result($result,$i,"contact");
?>

<tr><form name="reader" action="reader.php" method="POST">
<td><font face="Arial, Helvetica, sans-serif"><input type="checkbox" name="reader" value="Max"> Max <input type="checkbox" name="reader" value="Aaron"> Aaron</font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $Date; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $url; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $uploadedfile; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $contact; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><input type="submit" value="Submit"></font></td>
</form>
</tr>

<?php
$i++;
}
?>
</body>
</html>

Ve bu reader.php kodu:

    <?php

$error=0;

$con = mysql_connect('localhost', 'root', 'root'); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("longform", $con); //Replace with your MySQL DB Name
$reader=mysql_real_escape_string($_POST['reader']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO submission (reader) VALUES ('$reader')"; /*collect is the name of the MySQL table where the form data will be saved.name, email and comments are the respective table fields*/

if (!mysql_query($sql,$con)) {
 die('Error: ' . mysql_error());
}

$message="Done!";
mysql_close($con); 


?>

Şu anda, reader.php formun içeriği ile yeni bir veritabanı girdi ekler, ben varolan ilgili girişe verileri eklemeniz gerekir.

EDIT: Yani, ben sadece INSERT yerine UPDATE kullanarak gereken fark, ama ben nasıl hiçbir fikrim yok ...

EDIT: Ben, Commenters sayesinde doğru yönde bazı ilerlemeler yaptık düşünüyorum. Yeni kod rağmen hala çalışmıyor:

Çıktı + Form:

<html>
<body>
<?php

$id=$_GET['id'];

mysql_connect(localhost,root,root);
@mysql_select_db(longform) or die( "Unable to select database");
$query="SELECT * FROM submission";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Read By</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date/Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">URL</font></th>
<th><font face="Arial, Helvetica, sans-serif">Uploaded File</font></th>
<th><font face="Arial, Helvetica, sans-serif">Email / Twitter</font></th>
<th><font face="Arial, Helvetica, sans-serif"></font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$Date=mysql_result($result,$i,"Date");
$url=mysql_result($result,$i,"url");
$uploadedfile=mysql_result($result,$i,"uploadedfile");
$contact=mysql_result($result,$i,"contact");
?>

<tr><form name="reader" action="reader.php" method="POST">
<td style="display:none;"><input type="hidden" name="unique_id" value="<? echo $id; ?>"></td>
<td><font face="Arial, Helvetica, sans-serif"><input type="checkbox" name="reader" value="Max"> Max <input type="checkbox" name="reader" value="Aaron"> Aaron</font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $Date; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $url; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $uploadedfile; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $contact; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><input type="submit" value="Submit"></font></td>
</form>
</tr>

<?php
$i++;
}
?>
</body>
</html>

reader.php:

<?php

$error=0;

$con = mysql_connect('localhost', 'root', 'root'); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("longform", $con); //Replace with your MySQL DB Name

$reader=mysql_real_escape_string($_POST['reader']); //This value has to be the same as in the HTML form file

$query="UPDATE submission SET reader='$reader' WHERE id='$unique_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();

?>

Bu herhangi bir yardım tamamen benim dünyamı kaya olacaktır.

DÜZENLEME: Çalışır! Ben sadece başkası ihtiyacı durumda burada fonksiyonel kod göndermek istedim:

dosya.php

<html>
<body>
<?php

mysql_connect(localhost,root,root);
@mysql_select_db(test) or die( "Unable to select database");
$query="SELECT * FROM table WHERE field = '';
";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th></th>
<th><font face="Arial, Helvetica, sans-serif">Read By</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date/Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">URL</font></th>
<th><font face="Arial, Helvetica, sans-serif">Uploaded File</font></th>
<th><font face="Arial, Helvetica, sans-serif">Email / Twitter</font></th>
<th><font face="Arial, Helvetica, sans-serif"></font></th>
</tr>

<?php
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$date=mysql_result($result,$i,"date");
$url=mysql_result($result,$i,"url");
$uploadedfile=mysql_result($result,$i,"uploadedfile");
$contact=mysql_result($result,$i,"contact");
?>

<tr><form action="reader.php" method="POST">
<td><input type="hidden" name="id" value="<? echo $id; ?>"></td>
<td><font face="Arial, Helvetica, sans-serif"><input type="checkbox" name="reader" value="Max"> Max <input type="checkbox" name="reader" value="Aaron"> Aaron</font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $date; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $url; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $uploadedfile; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $contact; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><input type="submit" value="Submit"></font></td>
</form>
</tr>

<?php
$i++;
}
?>
</body>
</html>

reader.php

<?php

$error=0;

$con = mysql_connect('localhost', 'root', 'root'); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("longform", $con); //Replace with your MySQL DB Name

$reader=mysql_real_escape_string($_POST['reader']); //This value has to be the same as in the HTML form file
$id=mysql_real_escape_string($_POST['id']);
$query="UPDATE submission SET reader='$reader' WHERE id='$id'";
mysql_query($query);
echo "Record Updated";
mysql_close();
?>

0 Cevap