gpt4 book ai didi

php - 单击更新链接后表格未更新

转载 作者:行者123 更新时间:2023-11-30 21:39:37 28 4
gpt4 key购买 nike

我是 PHP 的新手,当我单击更新链接时尝试更新 php 中的表,它显示消息“未更新”。我哪里出错了?以下是单击更新链接时执行的代码。

<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "registration";

$conn=mysqli_connect($servername,$username,$password,$dbname);

$sql= 'UPDATE members_t SET Fname =$_POST[FIRSTNAME], Lname
=$_POST[LASTNAME], Memail = $_POST[EMAIL],Mcontact = $_POST[CONTACT],Mwhtap
= $_POST[WHATSAPP], Maddress = $_POST[ADDRESS], Mprofession =
$_POST[PROFESSION], WHERE id= $_POST[id]';

if(mysqli_query($conn, $sql))
header("refresh:1; url=up.php");
else
echo "Not Updated";

?>

最佳答案

请执行以下脚本:

<?php 

$servername="localhost";
$username="root";
$password="";
$dbname = "registration";

$conn=mysqli_connect($servername,$username,$password,$dbname);

if (!$conn){
die("Connection failed: " . mysqli_connect_error());
}

$firstName = $_POST["FIRSTNAME"];
$lastName = $_POST["LASTNAME"];
$email = $_POST["EMAIL"];
$contact = $_POST["CONTACT"];
$whatsapp = $_POST["WHATSAPP"];
$address = $_POST["ADDRESS"];
$profession = $_POST["PROFESSION"];
$id = $_POST["id"];


$sql= "UPDATE `members_t` SET `FIRSTNAME` = '$firstName',`LASTNAME` = '$lastName',`EMAIL` = '$email' ,`CONTACT` = $contact,`WHATSAPP` = $whatsapp, `ADDRESS` = '$address', `PROFESSION` = '$profession' WHERE id = $id";

if(mysqli_query($conn, $sql)){
header("refresh:1; url=up.php");
}else{
echo "Not Updated";
}

?>

如果发生任何错误,则在 phpmyadmin 或 MySQL Workbench 中执行 sql 语句并共享抛出的错误(如果有)。

您创建了一个包含以下列名称的表:FIRSTNAME、LASTNAME、EMAIL、CONTACT、WHATSAPP、ADDRESS、PROFESSION,但您使用的是 Fname、Lname、Memail、Mcontact 等。此外,您的创建表查询不正确,存在语法错误:

CREATE TABLE MEMBERS_T(id INT(6) NOT NULL PRIMARY KEY AUTO_INCREMENT, FIRSTNAME VARCHAR(30), LASTNAME VARCHAR(30), EMAIL VARCHAR(30), CONTACT INT(20), WHATSAPP INT(20), ADDRESS VARCHAR(50), PROFESSION VARCHAR(30));

代码已根据您的表格进行了更新,应该可以使用。

关于php - 单击更新链接后表格未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52308342/

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