简体   繁体   中英

mysqli Update with extra query

At first I have to enter two Accountnames in the website.

My tablename is 'account'.

The rows are id, username and recruiter.
The lines are
7, Janis, 0
and
4, testlol, 0.

I want to 'connect' two users. In the first line - the line of Janis - have to change his 'recruiter' into the value of the other user.

So the result should looks like:

7 | Janis | 4
and
4 | testlol | 7

...

In my php script I wrote

<?php
$servername = "localhost";
$username = "name";
$password = "test";
$dbname = "db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql1 = "UPDATE account SET recruiter=??? WHERE username=\"" . $_POST["account1"] . "\"";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

$conn->close();
?> 

What do I have to write instead of the ??? for updating it with the other user's id?

It's from this form:

<form action="handler.php" method="post" >
    <p>Account Nr.1:</p><input class="input" type="text" name="account1" value="">
    <p>Account Nr.2:</p><input class="input" type="text" name="account2" value="">
    <p></p><input class="button" type="submit" name="submit" value="Bestätigen"> 
</form>

This should be what you're looking for below. Although, it's hard to tell from you post what exactly you want. If I'm following correctly, you want to switch the recruiter value to other recruiter? If so, this should work:

$sql1 = "UPDATE account SET recruiter = $_POST["account2"] WHERE username = $_POST["account1"]";

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