在数据库中运行查询后输入数据2次

时间:2014-11-05 05:26:49

标签: php mysql

我遇到了另一个问题请帮忙。 当我查询我的db 1次时,2行输入db!

以下是代码:

<?php

$servername = "localhost";
$username = "***";
$password = "***";
$dbname = "***";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$name = $_POST['name'];
$famil = $_POST['famil'];
$email = $_POST['email'];
$amount = $_POST['amount'];

$name = mysqli_real_escape_string($conn, $name);
$famil = mysqli_real_escape_string($conn, $famil);
$email = mysqli_real_escape_string($conn, $email);
$amount = mysqli_real_escape_string($conn, $amount);

$sql = "INSERT INTO users".
"(name, famil, email, amount)".
"VALUES ('$name','$famil','$email','$amount')";

mysqli_query($conn,$sql);
if (mysqli_query($conn, $sql)) {
    echo "Data entered successfully.";
} else {
    echo "Error entering data: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

并且2次后的结果运行此代码:

3   mohsen  gholi   ***@yahoo.com   235354346
4   mohsen  gholi   ***@yahoo.com   235354346
5   mohsen  gholi   ***@yahoo.com   235354346
6   mohsen  gholi   ***@yahoo.com   235354346

1 个答案:

答案 0 :(得分:1)

按照Fred

的说法删除此行
mysqli_query($conn,$sql);

只需使用:

if (mysqli_query($conn, $sql)) {
    echo "Data entered successfully.";
} else {
    echo "Error entering data: " . mysqli_error($conn);
}