gpt4 book ai didi

php - 使用 PHP 将 HTML 表单数据插入 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 08:28:54 25 4
gpt4 key购买 nike

我对这段代码有疑问,因为它不起作用,但它在语义上似乎是正确的。它到达“die("Errore nella query: $query.");”,但我不明白为什么。请帮助我:

<?php
// Connessione al DB Server
$conn = mysqli_connect("localhost","root","","mydb");
// Controllo
if (!$conn){
echo "connessione database fallita";
exit();
}
$nome = $_POST["nm"];
$cognome = $_POST["cgn"];
$email = $_POST["ml"];
$username = $_POST["nkn"];
$password = md5($_POST["psw1"]);

$nome = mysqli_real_escape_string($conn,trim($nome));
$cognome = mysqli_real_escape_string($conn,trim($cognome));
$email = mysqli_real_escape_string($conn,trim($email));
$username = mysqli_real_escape_string($conn,trim($username));
$password = mysqli_real_escape_string($conn,trim($password));

$query = "SELECT username FROM utenti WHERE username = '".$username."'";
$risultato = mysqli_query($conn,$query);
if (!$result) {
die("Errore nella query: $query.");
}
$riga = mysqli_fetch_array($risultato,MYSQLI_NUM);
$n = mysqli_affected_rows($conn);
if($n >= 1){
echo "Username non disponibile";
}else{
$inserisci = "INSERT INTO utenti (nome,cognome,email,username,password) VALUES ('$nome','$cognome','$email','$username','$password')";
mysqli_query($conn,$inserisci);
?>
<p>Utente: <?php echo $username ?> registrato con successo.</p>
<?php
}
mysqli_close($conn);
?>

最佳答案

您检查 $result 变量,但 SQL 数据存储在 $risultato 中。正确重命名变量

关于php - 使用 PHP 将 HTML 表单数据插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17188843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com