Page 1 of 1

Simple Update

Posted: Tue Apr 23, 2002 10:15 am
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>
?>

Posted: Tue Apr 23, 2002 10:46 am
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

Posted: Tue Apr 23, 2002 10:51 am
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> 
?>

Posted: Tue Apr 23, 2002 10:58 am
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

Posted: Tue Apr 23, 2002 11:01 am
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