[英]Updating/editing mysql data with lookup table
I have 4 tables: 我有4张桌子:
Departments:
dept_id dept_name
1 dept 1
2 dept 2
3 dept 3
4 dept 4
-------------
Hours:
hours_id proj_hours
1 18
2 36
-------------
Project:
proj_id proj_name
1 project1
2 project2
-------------
Summary:
Summary_id proj_id hours_id dept_id
1 1 1 1
2 2 1 2
3 1 2 3
4 1 1 4
-----------------------------------------------------
SELECT *
FROM summary S
JOIN projects P ON S.proj_id = P.proj_id
JOIN departments D ON D.dept_id = S.dept_id
JOIN hours H ON H.hours_id = S.hours_id
I have an index.php table that neatly displays the join data from above with a broken edit function..... I can't figure out how to update the actual values from the lookup table from a manual edit function. 我有一个index.php表,该表通过损坏的编辑功能从上方巧妙地显示了联接数据.....我无法弄清楚如何从手动编辑功能的查找表中更新实际值。
<td align="center"><a href="edit2.php?id=<?php echo $rows['summary_id']; ?>">Edit</a></td>
is used to send the summary_id to the edit page, which it's defintely sending, but not the values.. I have blank fields. 用来将summary_id发送到编辑页面,该页面必须发送,但不发送值。.我有空白字段。 From there, I'd like to have my fields ready to edit, but again, blank fields... I'm having a problem the "id's" as opposed to "values" and formatting that right.
从那里,我想准备好要编辑的字段,但还是要空白字段……我遇到了一个问题,即“ id”而不是“ values”,并格式化该格式。
Suggestions? 有什么建议吗?
edit.php code: edit.php代码:
<?php
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if(isset($_GET['summary_id']))
{
$id = $_GET['summary_id'];
$res = mysql_query("SELECT * FROM summary WHERE proj_id='$proj_id'");
$rows = mysql_fetch_array($res);
}
if (isset($_POST['nproj_id']))
{
$summary_id = $_POST['summary_id'];
$nproj_id = $_POST['nproj_id'];
$nclarity_id = $_POST['clarity_id'];
$nhours_id = $_POST['nhours_id'];
$ndept_id = $_POST['ndept_id'];
$sql = "UPDATE summary SET clarity_id='$nclarity_id' WHERE proj_id='$proj_id'"
or die ("couldn't update".mysql_error());
$res = mysql_query($sql) or die ("couldn't update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=index2.php'>";
}
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="edit2.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Clarity ID</strong></td>
<td align="center"><strong>Hours</strong></td>
<td align="center"><strong>Department</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="nclarity_id" type="text" id="nclarity_id" value="<?php echo $rows['clarity_id']; ?>">
</td>
<td align="center">
<input name="nproj_hours" type="text" id="nproj_hours" value="<?php echo $rows['proj_hours']; ?>" size="15">
</td>
<td>
<input name="ndept_name" type="text" id="ndept_name" value="<?php echo $rows['dept_name']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="summary_id" type="hidden" id="summary_id" value="<?php echo $rows['summary_id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Update">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
Here's my index.php .... the corrections you made still didn't work. 这是我的index.php...。您所做的更正仍然无效。 :(
:(
<?php
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if($_POST['submitted'])
{
$proj_id = $_POST['proj_id'];
$summary_id = $_POST['summary_id'];
$clarity_id = $_POST['clarity_id'];
$hours_id = $_POST['hours_id'];
$proj_hours = $_POST['proj_hours'];
$dept_name = $_POST['dept_name'];
$dept_id = $_POST['dept_id'];
$sqlinsert = "INSERT INTO summary (summary_id,proj_id,hours_id,dept_id) VALUES (NULL,
'$proj_id','$hours_id','$dept_id'";
if (!mysql_query($sqlinsert)) {
die('error inserting new record'.mysql_error());
} // end of the nested if statement
}
$result = mysql_query("SELECT *
FROM summary S
JOIN projects P ON S.proj_id = P.proj_id
JOIN departments D ON D.dept_id = S.dept_id
JOIN hours H ON H.hours_id = S.hours_id
LIMIT 0 , 30");
?>
<form action="index.php" method="post">
Clarity #: <input type="text" name="clarity_id"><br />
Estimated Hours: <input type="text" name="proj_hours"><br />
Department: <input type="text" name="dept_name"><br />
<input type="hidden" name="submitted" value="true"/>
<input type="submit" value="enter">
</form>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Clarity#</strong></td>
<td align="center"><strong>Estimated Hours</strong></td>
<td align="center"><strong>Department</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['clarity_id']; ?></td>
<td><?php echo $rows['proj_hours']; ?></td>
<td><?php echo $rows['dept_name']; ?></td>
<td align="center"><a href="edit2.php?id=<?php echo $rows['summary_id']; ?>">Edit</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
Some corrections are made..try this 进行了一些更正。尝试此
<?php
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if(isset($_GET['summary_id']))
{
$id = $_GET['summary_id'];
$res = mysql_query("SELECT * FROM summary WHERE `summary_id`='$id'");//Correction is made here
if($res){
$rows = mysql_fetch_array($res);
}
else{
echo "Fetch Error: ".mysql_error();
}
}
if (isset($_POST['btnSubmit'])) //correction is here
{
$summary_id = $_POST['summary_id'];
$nproj_id = $_POST['nproj_id'];
$nclarity_id = $_POST['clarity_id'];
$nhours_id = $_POST['nhours_id'];
$ndept_id = $_POST['ndept_id'];
$sql = "UPDATE summary SET clarity_id='$nclarity_id' WHERE proj_id='$proj_id' AND `summary_id`='{$summary_id}'"; //correction is here
if(mysql_query($sql)){echo "Update successful";}else{echo "couldn't update".mysql_error();}
echo "<meta http-equiv='refresh' content='0;url=index2.php'>";
}
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="edit2.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Clarity ID</strong></td>
<td align="center"><strong>Hours</strong></td>
<td align="center"><strong>Department</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="nclarity_id" type="text" id="nclarity_id" value="<?php echo $rows['clarity_id']; ?>">
</td>
<td align="center">
<input name="nproj_hours" type="text" id="nproj_hours" value="<?php echo $rows['proj_hours']; ?>" size="15">
</td>
<td>
<input name="ndept_name" type="text" id="ndept_name" value="<?php echo $rows['dept_name']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="summary_id" type="hidden" id="summary_id" value="<?php echo $rows['summary_id']; ?>">
</td>
<td align="center">
<input type="submit" name="btnSubmit" value="Update"> <!--correction is here-->
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.