'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!
Solution 1:[1]
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");
}
?>
Solution 2:[2]
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");
}
?>
Solution 3:[3]
you can use mysqli_insert_id get last id insert. Then update with id.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jophab |
| Solution 2 | General Grievance |
| Solution 3 | Thu ?oàn |
