我有一个将数据发送到数据库的提交表单。然后我用HTML输出数据库中的数据。现在,我必须通过表单再将一个数据提交回数据库。我无法弄清楚如何使新数据与现有条目相对应。
这是我的输出代码(从数据库到HTML的数据,附加数据的新表格):
<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>
这是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
$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);
?>
现在,reader.php添加了一个包含表单内容的新数据库条目,我需要它将该数据添加到现有的相应条目中。
编辑:所以,我刚才意识到我需要使用UPDATE而不是INSERT,但我不知道如何...... 编辑:感谢评论者,我认为我在正确的方向上取得了一些进展。新代码仍然无效:输出+表格:
<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();
?>
任何帮助都会让我的世界充满震撼。
编辑:它有效!我只想在这里发布功能代码以防其他人需要它:output.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 :(得分:1)
在您的表单中,您可能希望添加一个隐藏字段,该字段是要更新的ID。因此,对于从数据库中提取的每一行,您将显示希望它们能够编辑的字段,然后还包括表单type = hidden字段,其中包括对象ID或其他标识信息。例如:
<form>
<input type="text" name="full_name" value="<?php echo $full_name ?>" />
<input type="hidden" name="id" value="<?php echo $id ?>" />
</form>
答案 1 :(得分:0)
获取要插入的现有行的ID。
实际上它不是插入的。它的更新。
为此你必须运行更新查询。
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
在php中进行条件检查。
if(id exist)
run update
else
run insert
http://www.w3schools.com/php/php_mysql_update.asp
http://www.weberdev.com/get_example-4392.html
理解上述链接中的示例并尝试在脚本中实现它
答案 2 :(得分:0)
//Assuming - parameters ready, connection establish and $connString set
$sql1 = "SELECT * FROM submissions";
$res = mysql_query($sql, $connString);
//loop like you did, but check out foreach loops, mysql_fetch_assoc, alternative forms
$id=mysql_result($result,$i,"id"); //similarly for other fields
//HTML form, elements, submit button as necessary for elements in $res
echo "<input type='hidden' name='submission_row_id' id='prev_insert_id_".$i."' value='$prevId' />";
//within loop and form. HTML with php tag for variable is fine too, im just fairly unfamiliar
//end loop
reader.php - 保留你的大多数人
$reader=mysql_real_escape_string($_POST['reader']);
$id = $_POST['submission_row_id'];
$sql2="INSERT INTO submission (id, reader) VALUES ($id,'$reader')";
$sql2success = mysql_query($sql2, $connString);
$outmessage = $sql2success ?
"Successful - table1 row $prevId updated" :
"Failure on update - $sql2"
echo $outmessage; //among other things
答案 3 :(得分:0)
简短回答:您需要重新设计表单。
简短回答:您的页面能够显示现有条目并创建新条目。不做的是提供一种编辑现有的方法(这是你想要做的)。
编辑表单和新表单几乎相同:关键区别在于表单需要提交正在编辑的条目的当前ID。然后,提交页面可以使用它来确定是否需要构建INSERT
语句或UPDATE
语句。
还必须完成渲染表单以进行编辑。 URL需要提供一种方法来指定哪一行(通常是ID),页面可以使用该行从数据库中获取该行以预先填充已编辑的字段。
(从长远来看,您也应该找到一个框架或构建自己的框架。这样您就可以将SQL调用与页面生成和数据处理隔离开来。)
答案 4 :(得分:0)
扩展zod回答,数据库中的每个条目都应该有一个唯一的标识符,这是zod引用的id。
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
现在,您可以通过其ID以及“将[e]数据添加到现有的相应条目”来引用每个条目。你可以说
UPDATE .. FROM ... WHERE id = '$id'
答案 5 :(得分:0)
嘿梅根,佐德是对的。首先,您需要传递表提交的id。然后,您需要检查记录是否存在。你可以这两种方式:
SELECT COUNT(*) as total FROM submission WHERE ID = $id
如果总计&gt; 0,那么你知道你需要更新;否则,您需要插入记录。
如果您使用的是MySQL(就像您的代码演示一样)。您还可以使用“ON DUPLICATE KEY UPDATE”
INSERT INTO submission (id, Date,url,uploadedfile,contact) VALUES (1,'2010-12-31','www.google.com', 'Megan')
ON DUPLICATE KEY UPDATE Date = now();
这是一个方便的工具,但您必须检查它是否适用于您。您可以在以下站点中获取有关此功能的更多信息: http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html
答案 6 :(得分:0)
这是你似乎遇到麻烦的地方:
$sql="INSERT INTO submission (reader) VALUES ('$reader')";
尝试将其替换为:
if($_POST['unique_id']){
$sql = "UPDATE submission (reader) VALUES ($reader) WHERE id = $unique_id LIMIT 1;";
}else{
$sql="INSERT INTO submission (reader) VALUES ('$reader')";
}