无论是否成功添加消息都不起作用

时间:2017-12-16 11:56:05

标签: php mysqli insert

我正在努力将数据插入到数据库中,我想添加一条消息,无论是否已成功添加数据。

$res = mysqli_query($link,"insert into product values('',
'".mysqli_real_escape_string($link, $_POST[product_name])."',
,'".mysqli_real_escape_string($link, $_POST[product_description])."')");

if($res)
{
echo "Success";
}
else
{
echo "Error";
}

2 个答案:

答案 0 :(得分:1)

您可以通过最后一次插入ID识别,或者您可以使用以下代码

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>

了解更多细节 http://php.net/manual/en/function.mysql-insert-id.php

答案 1 :(得分:0)

$product = mysqli_real_escape_string($link, $_POST['product_name']);
$desc = mysqli_real_escape_string($link, $_POST['product_description']);

if (!mysqli_query($link,"INSERT INTO product (columnName, columnName2 ) VALUES ($product, $desc)"))
{
    echo("Error description: " . mysqli_error($link));
}
else
{
    echo "Success";
}