user6538098 user6538098 - 4 months ago 8
PHP Question

How do I update the database values using update statement?

I am trying to update

'company_name', 'company_add', 'price'
as primary key
'id'
but it shows me a
'something went wrong'
message along with an
'undefined id'
error. please help me!

<?php
include('data_conn.php');

if(isset($_POST['sub']))
{
$comname=$_POST['cname'];
$comadd=$_POST['cadd'];
$pri=$_POST['price'];

$query ="UPDATE login SET company_name=$comname,company_add=$comadd,price=$pri WHERE id=$id";
$result = mysql_query($query);
echo $result;
if(!$result)
{
echo '<script language="javascript">';
echo 'alert("something went Wrong...:("); location.href="edit.php"';
echo '</script>';
}else{
echo '<script language="javascript">';
echo 'alert("successfully updated!!!"); location.href="edit.php"';
echo '</script>';
}
}
?>

Answer

You have to put the character values in single quotes:

$query ="UPDATE login SET company_name='$comname',company_add='$comadd',price=$pri WHERE id=$id";

Stop using deprecated mysql_* API. Use mysqli_* or PDO with prepared Statements. Atleast use the error function, to get the error message.

Comments