Update operation not working in Php MySQL database

953

I am learning php and MySql database. I am trying to make payroll management software. In my database both insert & delete operation are executing well but i am facing problem in update operation. Here is my php script :

<html>
<body>
<?php
    session_start();    
    $submit = $_POST['submit'];
    $term = $_POST['id'];

    //open database
    $connect = mysql_connect("localhost","root","#") or die("Couldn't connect");
    mysql_select_db("caselab") or die("Couldn't connect");  
    $sql = mysql_query("SELECT id FROM users WHERE id='$term'");        
    $count = mysql_num_rows($sql);  
    if($count!=0)
    {
        // output data of each row
        $id = $_POST['id'];
        $name = strip_tags($_POST['name']);
        $email = strip_tags($_POST['email']);
        $address = strip_tags($_POST['address']);
        $contactinfo = $_POST['contactinfo'];

        if($submit)
        {
            //open database
            $connect = mysql_connect("localhost","root","#") or die("Couldn't connect");
            mysql_select_db("caselab") or die("Couldn't connect");  

            // Existence Check
            if($name  && $email && $address && $contactinfo)
            {
                $queryreg = mysql_query ("Update users SET username = '$name', email = '$email' , address = '$address' , contactinfo = '$contactinfo' WHERE id = $id");
                    echo ("Congratulations!! Your changes have been saved !! <a href='payroll.html'>Click to go back to home page</a>");        
            }
            else
                echo("Please fill all the details");
        }   
        mysql_close($connect);
    }
    else
        echo("No such employee. Please try again.<a href='payroll.html'>Click to go back to home page</a> ");
?>
</html>
</body>

I would be highly thankful if my problem gets resolved.

199

Answer

Solution:

Why is there a ) before WHERE?

Update users SET username = $name, email = $email , address = $address , contactinfo = $contactinfo) WHERE id = $id");

Try this:

$myqry =  "Update users SET username = '". $name."', email = '".$email."' , address = '".$address."', contactinfo = '".$contactinfo."' WHERE id = ".$id.";
echo($myqry;
$queryreg = mysql_query($myqry);
if .....

However, i need reminder you that this is not a good programming method and you need learn how to PDO after you understand the basic query concepts. http://php.net/manual/en/book.pdo.php

People are also looking for solutions to the problem: php - Infinite loop when using cURL to access a REST API, what is causing it?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.