简体   繁体   中英

Get data from 2 different forms and continue the same table row

Please help me get this done since it's driving me crazy already.

I'm new to this whole process so what seems easy for you might be a nightmare for me, and no, google didn't help :(

So, i'm having one mysql table named member with the following structure:

  • mem_id
  • username
  • password
  • firstname
  • lastname
  • titlu (title)
  • descriere (description)
  • joy (integer)
  • comm

I'm parsing user details using execute.php looking like this:

<?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");
}
?>

Now i have another form that inserts gift details and must continue filling the same row in mysql.

I've tried the following but no luck:

<?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");
}
?>

Thank you very much for your support!

Change your second file to the below code. it will redirect to index.php?remark=failed only if no user exist with the given username

 <?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");
    }
    ?>

I managed to get it done by using:

<?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");
    }
    ?>

But now, i need help with the next step:

I need to get all the data from the database,except mine, as a logged in user, in a html table. I'm trying the following but brings back nothing:

<? 
$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>";

?>

Any ideas?

Thank you very much!

you can use mysqli_insert_id get last id insert. Then update with id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM