Hello all i have some problem with update query. select query works all right but update qurey doesn't i have tried to to run that query in mysql directly from phpmyadmin and it runs fine ... can you check what i m missing here in code in update query and i m a student new to php

<?php session_start();
if(!isset($_SESSION['password'])||!isset($_SESSION['username'])){
    header("location:admin_login.php");
    exit();
}
function renderform($name='',$category='',$sub='',$price='',$detail='',$error='',$id=NULL){
    if (isset($id)){
        echo "ID : $id </br>";
    }
    if ($error != ''){
        echo "<div style='border:1px solid red; color:red; text-align:center;'>$error </div>"; } ?>

        <form method="post" action="">
            <input type="hidden" name="id" value="<?php $id; ?>">
            Product name : </br>
            <input type="text" name="name" value="<?php echo $name; ?>"> </br>
            Category : </br>
            <input type="text" name="category" value="<?php echo $category; ?>"></br>
            SubCategory : </br>
            <input type="text" name="subcategory" value="<?php echo $sub; ?>"> </br>
            Price : </br>
            <input type="text" name="price" value="<?php echo $price; ?>"> </br>
            Detail : </br>
            <textarea name="detail" col="50" row="15" ><?php echo $detail; ?>

Recommended Answers

All 4 Replies

You did not provide the code with the update query.

<?php session_start();
if(!isset($_SESSION['password'])||!isset($_SESSION['username'])){
    header("location:admin_login.php");
    exit();
}
function renderform($name='',$category='',$sub='',$price='',$detail='',$error='',$id=NULL)
{
    if (isset($id)){
        echo "ID : $id </br>";
    }
    if ($error != ''){
        echo "<div style='border:1px solid red; color:red; text-align:center;'>$error </div>"; 
    } 
?>

        <form method="post" action="">
            <input type="hidden" name="id" value="<?php $id; ?>">
            Product name : </br>
            <input type="text" name="name" value="<?php echo $name; ?>"> </br>
            Category : </br>
            <input type="text" name="category" value="<?php echo $category; ?>"></br>
            SubCategory : </br>
            <input type="text" name="subcategory" value="<?php echo $sub; ?>"> </br>
            Price : </br>
            <input type="text" name="price" value="<?php echo $price; ?>"> </br>
            Detail : </br>
            <textarea name="detail" col="50" row="15" ><?php echo $detail; ?></textarea> </br>
            <input type="submit" name="submit" value="submit" >
        </form>
<?php
}
include ("../header.php");
?>  
<tr> 
<td>
<?php  
if (isset($_GET['id'])){
    // editing the old one 
    if (is_numeric($_GET['id'])){
        $id=$_GET['id'];
        if (isset($_POST['submit'])){
            // form is submitted
            $name= htmlentities($_POST['name'],ENT_QUOTES);
            $category= htmlentities($_POST['category'],ENT_QUOTES);
            $sub= htmlentities($_POST['subcategory'],ENT_QUOTES);
            $price= htmlentities($_POST['price'],ENT_QUOTES);
            $detail= htmlentities($_POST['detail'],ENT_QUOTES);
            //$id= htmlentities(is_numeric($_POST['id']));// i will ask about this too 
            if ( isset($name) && isset($category) && isset($sub) && isset($price) && isset($detail) && isset($id) && is_numeric($price) ){
                $name=addslashes($name);
                $category=addslashes($category);
                $sub=addslashes($sub);
                //$price=addslashes($price); i will ask about that way ... "can i have to use addslashes function after check that variable for is_numeric more over that is a field for numeric data to be inputed " ......
                $detail=addslashes($detail);
                $query= mysql_query("UPDATE `product` SET `product_name`= $name WHERE `product_id`= $id");
                mysql_error();
                echo "Please wait you are being redirected to view page ";
                //header('Refresh: 2; URL=view.php');
            }
            else {
                $error="Fill all required fields";
                renderform($name,$category,$sub,$price,$detail,$error,$id);
            }
        }
        else {
            // show form with old data and user has to update it ... 
            include_once ("../scripts/connect.php");
            $query= mysql_query("SELECT * FROM product WHERE product_id={$id}");
            $data=mysql_fetch_array($query);
            $name=$data['product_name'];
            renderform($name,$data['product_category'],$data['product_subcategory'],$data['product_price'],$data['product_detail'],NULL,$id);
        }
    }
    else{
        header("Location:view.php");
    }
}
else {
// new product ...
}
?>
</td>
</tr>
<?php 
include ("../footer.php");
?>

$query= mysql_query("UPDATE product SET product_name= $name WHERE product_id= $id");

You are missing single quotes around $name

$query = mysql_query("UPDATE `product` SET `product_name` = '$name' WHERE `product_id` = $id");

try this query :

 $query= mysql_query("UPDATE product SET product_name = '$name' WHERE product_id= '$id'");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.