无法更新记录

时间:2019-05-07 16:15:00

标签: php

我是Php的新手。以下代码不起作用。
我没有错误,查询在浏览器中不起作用,但在phpmyadmin中起作用。

我该如何解决?谢谢。

include_once("settings.php");
$login = $_SESSION["login"];

$name = $_POST["name"];
$surname = $_POST["surname"];
$email = $_POST["email"];

$query = "use db1; update table1 set name = '$name', surname = '$surname', email = '$email' where column1= '$login'";

$rec = mysqli_query($connection, $query);

if($rec){
    echo "Successful";
}
else{
    echo "Error";
}

1 个答案:

答案 0 :(得分:2)

您的代码不适用于php,我进行了相同的更改:

include_once("settings.php");
$login = $_SESSION["login"];

$name = $_POST["name"];
$surname = $_POST["surname"];
$email = $_POST["email"];

$query = "update table1 set name = '$name', surname = '$surname', email = '$email' where column1= '$login'";

$rec = mysqli_query($connection, $query);

if($rec){
    echo "Successful";
}
else{
    echo "Error";
}

测试示例:

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

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

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

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

$conn->close();
?>