1

I'm new to PHP, trying to update table in php when i clicked on update link it show message "Not Updated".where i went wrong? following is the code which is executed when update link is clicked.

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

?>
Anita S
  • 21
  • 2
  • Possible duplicate of [MySQLi Query return value in case of Select with no matching rows](https://stackoverflow.com/questions/52306485/mysqli-query-return-value-in-case-of-select-with-no-matching-rows) – Madhur Bhaiya Sep 13 '18 at 07:11
  • If any of the fields are strings, then you need quotes round the values. Even better would be to look at prepared statements and the advantages of them. https://stackoverflow.com/questions/1290975/how-to-create-a-secure-mysql-prepared-statement-in-php might be a start point. – Nigel Ren Sep 13 '18 at 07:14
  • Use double quotes instead of single $sql = " ... ", and php vars will be passed to your query. And each string value in your query must be with quotes around $sql = "UPDATE members_t SET Fname = '$_POST[FIRSTNAME]' WHERE $id = $_POST[id]"; – ustmaestro Sep 13 '18 at 07:19
  • Remove coma before WHERE – ustmaestro Sep 13 '18 at 07:20
  • Possible duplicate of [When to use single quotes, double quotes, and back ticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql) – Nigel Ren Sep 13 '18 at 08:02

1 Answers1

0

Please execute the following script :

<?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"; 
}  

?>

If any error occurs then execute the sql statement in phpmyadmin or MySQL Workbench and share the error thrown(if any).

You have created a Table with the following column names : FIRSTNAME, LASTNAME , EMAIL, CONTACT, WHATSAPP,ADDRESS,PROFESSION but you're using Fname,Lname,Memail,Mcontact and so on. Also your create table query is not correct , theres a syntax error :

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

The code has been updated according to your table and should work.

  • after doing this changes it gives notice Undefined index: FIRSTNAME LASTNAME EMAIL CONTACT WHATSAPP ADDRESS PROFESSION id and below that shows Not updated – Anita S Sep 13 '18 at 09:07
  • I've updated the code. Please try this one and let me know if its working for you. Keep in mind that you need to use POST associative array like so : $_POST['key'] inside quotes instead of $_POST[key] – prashant bana Sep 13 '18 at 09:16
  • can you please share your updated code ? Do check if $conn is established and that you're not initializing connection on $conn2 and then using $conn for query. – prashant bana Sep 13 '18 at 09:39
  • i have replace $conn2 by $conn – Anita S Sep 13 '18 at 10:21
  • Please post the updated code and the Table structure that youre trying to save to – prashant bana Sep 13 '18 at 10:23
  • CREATAE TABLE MEMBERS_T ( id INT(6) NOT NULL AUTO INCREMENT PRIMARY KEY, FIRSTNAME VARCHAR(30), LASTNAME VARCHAR(30), EMAIL VARCHAR(30), CONTACT INT(20), WHATSAPP INT(20), ADDRESS VARCHAR(50), PROFESSION VARCHAR(30)); – Anita S Sep 14 '18 at 06:37
  • I've updated the code above according to your Table data structures . Please execute the script and share the errors if any. – prashant bana Sep 14 '18 at 06:50
  • after adding above changes it gives Notice: Undefined index: Fname Lname Memail Mcontact Mwhtap Maddress Mprofession id and below that shows "Not updated" – Anita S Sep 14 '18 at 07:29
  • Sorry i made a silly error. Please try this script.I mixed up the table column names with Ajax Key values. – prashant bana Sep 14 '18 at 07:36
  • gives notice Undefined index: FIRSTNAME LASTNAME EMAIL CONTACT WHATSAPP ADDRESS PROFESSION id and below that shows Not updated – Anita S Sep 14 '18 at 08:35
  • You're not using the correct column names in your question itself. Your CREATE command creates a table with column names such as : FIRSTNAME, LASTNAME and so on but your using UPDATE with column name `FName` and `LName` so on. – prashant bana Sep 14 '18 at 09:01
  • Please check the updated file and let me know if any error occurs – prashant bana Sep 14 '18 at 09:02