添加新的SQL记录,从当前的php中获取ID?id =

时间:2015-04-19 18:42:44

标签: php sql

我意识到我可能是有史以来最大的菜鸟,但这让我整天都疯了......

所以基本上我有一张叫做“客户”的桌子。我已经管理了一个search.php,view.php和edit.php来显示和编辑客户端搜索结果,但我想要一个'添加项目'每个搜索结果下的按钮链接到newproject.php,其中我当前正在查看的客户端的ID被填充到表单中,因此我可以创建一个新项目'根据客户的详细信息而不必再把它们放进去 - 我整天尝试过不同的事情,但绝对无济于事,我现在已经明显地错过了这一点。基本解决方案!

任何帮助都会非常感激!

    // connect to the database
include('connect-db.php');

// get results from database
$result = mysql_query("SELECT * FROM clients") 
    or die(mysql_error());  

// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";

echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";

// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {

    // echo out the contents of each row into a table
    echo "<tr>";
    echo '<td>' . $row['ID'] . '</td>';
    echo '<td>' . $row['Title'] . '</td>';
    echo '<td>' . $row['LastName'] . '</td>';
    echo '<td><a href="edit.php?id=' . $row['ID'] . '">Edit</a></td>';
    echo '<td><a href="delete.php?id=' . $row['ID'] . '">Delete</a></td>';
    echo "</tr>"; 
} 

// close table>
echo "</table>";

是view.php

$sql="INSERT INTO projects (projectname, budget)
VALUES
('$_POST[projectname]','$_POST[budget]')";
 if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>

是insertproject.php

<form action="insertproject.php" method="post">
Project Name: <input type="text" name="projectname" /><br><br>
Budget: <input type="text" name="budget" /><br><br>

是newproject.php

出于某些原因,我脑子里想到了一个想要从URL中预先填充GET ID的文本框直接进入项目表单...

如果这对你来说听起来比对我来说更简单,那么你应该获得诺贝尔奖!

1 个答案:

答案 0 :(得分:1)

将client_id传递给newproject.php并收集client_id中的newproject.php,并将该值放在表单内的隐藏字段中。

在搜索结果页面添加

<a href="newproject.php?client_id=<?php echo $clientID; ?>">Add Project </a>

在newproject.php页面

<?php
$clientId = $_REQUEST['client_id']
?>
<form method="post" action="newproject.php">
<input type="hidden" name="client_id" id="client_id" value="<?php echo $clientId;?>"/>
-----------------
Your Project Form
-----------------
</form>