单击更新链接后表未更新

时间:2018-09-13 07:09:29

标签: php mysql

我是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";

?>

1 个答案:

答案 0 :(得分:0)

请执行以下脚本:

<?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));

该代码已根据您的表进行更新,并且可以正常工作。