update specific column

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

westside
New php-forum User
New php-forum User
Posts: 4
Joined: Fri Oct 19, 2012 12:20 pm

update specific column

Postby westside » Sat Oct 20, 2012 7:26 am

Hi Guys,

im currently developing my own website. I try to update certain data referring to $id. I couldnt update my database. Please help.

Code: Select all

<html>
<head>
<style type="text/css">
form label {
   float: left;
   width: 150px;
   margin-bottom: 5px;
   margin-top: 5px;
}
.clear {
   display: block;
   clear: both;
   width: 100%;
}
</style>
<title>Update a Record in MySQL Database</title>
</head>
<body>
<?php //Query
if(isset($_POST['update']))
{
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
 //INSERT
$id = $_POST['id']; 
$mw_url = $_POST['mw_url']; 
$mw_quality = $_POST['mw_quality']; 
$mw_size = $_POST['mw_size']; 
$mw_filetype = $_POST['mw_filetype']; 

$sql = "UPDATE mymovie ".
       "SET mw_url = $mw_url ".
       "SET mw_quality = $mw_url ".
       "SET mw_size = $mw_url ".
        "SET mw_filetype = $mw_url ".
       "WHERE id = $id" ;
 
mysql_select_db('data_movie');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<label for="id">Id</label><input type="text" name="id" id="id" />
<br class="clear" />
<label for="mw_url">Mw Url</label><input type="text" name="mw_url" id="mw_url" />
<br class="clear" />
<label for="mw_quality">Mw Quality</label><input type="text" name="mw_quality" id="mw_quality" />
<br class="clear" />
<label for="mw_size">Mw Size</label><input type="text" name="mw_size" id="mw_size" />
<br class="clear" />
<label for="mw_filetype">Mw Filetype</label><input type="text" name="mw_filetype" id="mw_filetype" />
<br class="clear" />
<input type="submit" name="sbt" id="sbt" value="Submit" />
<br class="clear" />
</form>
<?php
}
?>
</body>
</html>


and if the id doesnt exist how do i create, will it be a problem.. since im using 'update'. Thank You!

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: update specific column

Postby simplypixie » Mon Oct 22, 2012 2:46 am

You only use SET once in a query, you are formatting your surrounding quote marks incorrectly and you are missing your commas so change your query to this and it should work (subject to anything else that may be wrong)

Code: Select all

$sql = "UPDATE mymovie SET
       mw_url = ' " . $mw_url . " ',
       mw_quality = ' " . $mw_url  . " ',
       mw_size = ' " . $mw_url . " ',
       mw_filetype = ' " . $mw_url . " '
       WHERE id = ' " . $id . ' ";


Don't forget to sanitzie your input as well (mysql_real_escape_string)


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 2 guests