从2种不同的表单中获取数据并继续使用相同的表格行

时间:2016-11-25 08:07:54

标签: php mysql

请帮我完成这件事,因为它已经让我疯了。

我是这个过程的新手,所以看起来容易对我来说可能是个噩梦,不,谷歌没有帮助:(

所以,我有一个名为member的mysql表具有以下结构:

  
      
  • mem_id
  •   
  • 用户名
  •   
  • 密码
  •   
  • 姓名
  •   
  • 名字
  •   
  • titlu(标题)
  •   
  • descriere(description)
  •   
  • 欢乐(整数)
  •   
  • COMM
  •   

我正在使用execute.php解析用户详细信息,如下所示:

<?php
session_start();
include('db.php');
$username=$_POST['username'];

$result  =  mysqli_query($db,"SELECT  *  FROM  member  WHERE  username='$username'");
$num_rows  =  mysqli_num_rows($result);

if  ($num_rows)  {
header("location:  index.php?remarks=failed");
}
else
{
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$username=$_POST['username'];
$password=$_POST['password'];
mysqli_query($db,"INSERT  INTO  member(firstname,  lastname,  username,  password)VALUES('$firstname',  '$lastname',  '$username',  '$password')");
header("location:  index.php?remarks=success");
}
?>

现在我有另一个插入礼物详细信息的表单,并且必须继续填写mysql中的同一行。

我尝试过以下但没有运气:

<?php
session_start();
include('db.php');
$username=$_POST['username'];

$result  =  mysqli_query($db,"SELECT  *  FROM  member  WHERE  username='$username'");
$num_rows  =  mysqli_num_rows($result);

if  ($num_rows)  {
header("location:  index.php?remarks=failed");
}
else
{
$titlu = $_POST['titlu'];
$descriere = $_POST['descriere'];
$joy = $_POST['joy'];
$comm = $_POST['comm'];
$sql = "UPDATE member 
            SET titlu = '".mysql_real_escape_string($_POST[titlu])."'
            SET descriere = '".mysql_real_escape_string($_POST[descriere])."'
            SET joy = '".mysql_real_escape_string($_POST[joy])."'
            SET comm = '".mysql_real_escape_string($_POST[comm])."'
            WHERE username='".mysql_real_escape_string($_POST['username'])."'";
header("location:  welcome.php?remarks=success");
}
?>

非常感谢您的支持!

3 个答案:

答案 0 :(得分:1)

将您的第二个文件更改为以下代码。仅当没有用户存在给定用户名

时,它才会重定向到index.php?remark=failed
 <?php
    session_start();
    include('db.php');
    $username=$_POST['username'];

    $result  =  mysqli_query($db,"SELECT  *  FROM  member  WHERE  username='$username'");
    $num_rows  =  mysqli_num_rows($result);

    if  (!$num_rows)  {
    header("location:  index.php?remarks=failed");
    }
    else
    {
    $titlu = $_POST['titlu'];
    $descriere = $_POST['descriere'];
    $joy = $_POST['joy'];
    $comm = $_POST['comm'];
    $sql = "UPDATE member 
                SET titlu = '".mysql_real_escape_string($_POST[titlu])."',
                descriere = '".mysql_real_escape_string($_POST[descriere])."',
               joy = '".mysql_real_escape_string($_POST[joy])."',
                comm = '".mysql_real_escape_string($_POST[comm])."'
                WHERE username='".mysql_real_escape_string($_POST['username'])."'";
mysqli_query($sql);
    header("location:  welcome.php?remarks=success");
    }
    ?>

答案 1 :(得分:1)

我设法通过使用:

来完成它
<?php
    session_start();
    include('db.php');
    include('session.php');

    $res  =  mysqli_query($db,"SELECT * FROM member where mem_id=$loggedin_id");    
    $num_rows  =  mysqli_num_rows($res);

    if  (!$num_rows)  {
        header("location:  welcome.php?remarks=failed");
    }
    else
    {
    $titlu = $_POST['titlu'];
    $descriere = $_POST['descriere'];
    $joy = $_POST['joy'];
    $comm = $_POST['comm'];

mysqli_query($db,"UPDATE member 
               SET titlu = '$titlu',
               descriere = ' $descriere ',
               joy = '$joy',
               comm = '$comm'
               where mem_id=$loggedin_id");
    header("location:  welcome.php?remarks=success");
    }
    ?>

但现在,我需要下一步的帮助:

我需要从html表中获取数据库中的所有数据,除了我的,作为登录用户。我正在尝试以下但没有带回任何东西:

<? 
$result = mysql_query($db,"SELECT * FROM member")

or die(mysql_error());

echo "<table border='1' cellpadding='10'>";

echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";

while($row = mysql_fetch_array( $result )) {

echo "<tr>";

echo '<td>' . $row['mem_id'] . '</td>';

echo '<td>' . $row['firstname'] . '</td>';

echo '<td>' . $row['lastname'] . '</td>';

echo "</tr>";

}



// close table>

echo "</table>";

?>

有什么想法吗?

非常感谢!

答案 2 :(得分:-1)

你可以使用mysqli_insert_id获取最后一个id插入。然后用id。

更新