Simple Update

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jennyBret
Forum Newbie
Posts: 1
Joined: Tue Apr 23, 2002 10:15 am

Simple Update

Post by jennyBret »

Hi Guys

I'm only new at php and im having problems with a simple update (so im told). I can retrieve but i now want to be able to update the data that appears and display errors if no data there to show etc... Any help is much appreciated! Heres the code below.
Jen.



<?php
if ($customer_search)
{
$db = mysql_connect("localhost", "fineart");
mysql_select_db("fineart", $db);
$result = mysql_query("select * from customer where customerid = $customer_search" );
$row = mysql_fetch_array($result);
$customer_search = $row['customerID'];
}
if($btnupdate)
$db = mysql_connect("localhost", "fineart");
mysql_select_db("fineart",$db);
$sql = "UPDATE customer SET title = '$title', fname = '$fname', lname = '$lname',
address1 = '$address1', phonenumber = '$phonenumber' WHERE customer_search = $customerid";
$result = mysql_query($sql);
echo $result;

<script language = "Javascript">
alert("Thank you! Information Updated");
</script>
?>
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

is it getting to the "update" section?
and have you tried echoing $sql and seeing if the same line from a command line works?
-enygma
User avatar
Phirus
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 4:10 pm
Contact:

Post by Phirus »

Just from looking at your code, u have left off the { and } at the if($btnupdate) segment..

Hope that helps,

PhIrUs

Code: Select all

<?php 
if ($customer_search) 
&#123; 
$db = mysql_connect("localhost", "fineart"); 
mysql_select_db("fineart", $db); 
$result = mysql_query("select * from customer where customerid = $customer_search" ); 
$row = mysql_fetch_array($result); 
$customer_search = $row&#1111;'customerID']; 
&#125; 
if($btnupdate)&#123;
$db = mysql_connect("localhost", "fineart"); 
mysql_select_db("fineart",$db); 
$sql = "UPDATE customer SET title = '$title', fname = '$fname', lname = '$lname', 
address1 = '$address1', phonenumber = '$phonenumber' WHERE customer_search = $customerid"; 
$result = mysql_query($sql); 
echo $result; 
&#125;

<script language = "Javascript"> 
alert("Thank you! Information Updated"); 
</script> 
?>
User avatar
chiefmonkey
Forum Commoner
Posts: 25
Joined: Sat Apr 20, 2002 5:21 am
Location: Glasgow UK

Post by chiefmonkey »

How about adding mysql_error() as well,

$result = mysql_query($sql) or die(mysql_error());


I am a great believer in building in some type of error checking when testing scripts.

Try writing a simple function which returns mysql_error() and mysql_errorno() and your original query and include it on your production pages then run all your mysql queries through it, obviously remove it when you publish your code

George
User avatar
Phirus
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 4:10 pm
Contact:

Post by Phirus »

I think this is more along the line of what you are trying to do...

Code: Select all

<?

// Database constants
define("DATABASE_HOST", "localhost");
define("DATABASE_USER", "** YOUR_USER **");
define("DATABASE_PASSWORD", "** YOUR_PASS **");
define("DATABASE_NAME", "fineart");

$dbLink = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD);

if(!$dbLink) &#123;
        print "Unable to connect to the database, please contact Sysadmin asap.";
&#125; else &#123;
		$dbUse = mysql_select_db(DATABASE_NAME, $dbLink);
&#125;



if(isset($customer_search))&#123;

	$result = mysql_query("SELECT * FROM customer WHERE customerid = $customer_search");
	$row = mysql_fetch_array($result);
	$customer_search = $row&#1111;'customerID'];

&#125;

if(isset($btnupdate))&#123;

	$iqry_update = mysql_query("UPDATE customer SET title = '$title', fname = '$fname', lname = '$lname', address1 = '$address1', phonenumber = '$phonenumber' WHERE customer_search = $customerid")
	echo "<script language="Javascript">alert("Thank you! Information Updated");</script>";

&#125;

?>
Hope that helps,

PhIrUs
Post Reply