mysql创建重复的行

时间:2017-01-15 13:52:59

标签: php mysql

我只是初学者而且无法理解为什么我的代码在插入后生成相同的相同行。请帮我。 这是我的HTML代码:

<form id="sing" action="signup.php" method="POST">
    <input type="text" name="first" placeholder="სახელი"> <br>
    <input type="text" name="uid" placeholder="მომხმარებელი"> <br>
    <input type="password" name="pwd" placeholder="პაროლი"> <br>
    <button type="submit">რეგისტრაცია</button>
</form>

PHP:

include 'dbh.php';

$first = $_POST['first'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];


$sql = "INSERT INTO user (first, uid, pwd) 
VALUES ('$first', '$uid', '$pwd')";

$result = mysqli_query($conn, $sql);

if (!mysqli_query($conn, $sql)) {
    echo "maica joo";
} else {
    echo "kaia";
}

3 个答案:

答案 0 :(得分:1)

这是因为mysqli_query($conn, $sql)被调用两次,将代码更改为:

$result = mysqli_query($conn, $sql);

if (!$result) {
    echo "maica joo";
} else {
    echo "kaia";
}

答案 1 :(得分:0)

你正在运行mysqli_query()两次!!所以它创建了2个相同的行。

// first time it insert a row
$result = mysqli_query($conn, $sql);

// now you run it again and it creates that row again
if (!mysqli_query($conn, $sql)) {
    echo "maica joo";
} else {
    echo "kaia";
}

将此修改为

// first time it insert a row
$result = mysqli_query($conn, $sql);

// now test $result to see if the query failed
if (!$result) {
    echo "maica joo";
} else {
    echo "kaia";
}

答案 2 :(得分:0)

您正在使用两个mysqli_query运行查询两次。

将其更改为:

$result = mysqli_query($conn, $sql);

if (!$result) {