Username_null
Username_null

Reputation: 1315

Basic update database table using php question

I seem to be missing something quite fundamental here and yet my code doesn't seem to be any different to any of the numerous online tutorials that I have looked at. What I would like is for someone to look at this and say....Oh you have forgotten to...etc;

This is what I have on a separate update page which is intended to perform the update then cycle back to the main admin page:

require_once('../Connections/MyConn.php');
$sql_statement = "UPDATE skyscrapers SET ";
$sql_image = "Ad_image = '" . $_REQUEST['image'] . "', ";
$sql_expire = "Ad_Expires = '" . $_REQUEST['expire'] . "'";

$result = mysql_query($sql_statement . $sql_image . $sql_expire . " WHERE Ad_ID=" . $_REQUEST['ADID']);
if (!$result) {
  echo("<p>Error performing query: " . mysql_error() . "</p>");
  exit();
} 

mysql_close ($MyConn); 
header("location:Admin_skyscrapers.php");

However when I run this I get the following error:-

"Error performing query: No database selected"

Well, haven't I selected the database in the connection script which already works everywhere else?

I realise the code isn't very pretty and I am being naughty using the url to pass variables at the moment - I do promise to change this when I get it to work :)

So, any pointers would be helpful, thanks in advance.

Edit to add...

This is the connection script with the sensitive stuff redacted:-

# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_MyConn = "*************.co.uk";
$database_MyConn = "db**********";
$username_MyConn = "dbo*********";
$password_MyConn = "*****";
$MyConn = mysql_pconnect($hostname_MyConn, $username_MyConn, $password_MyConn) or trigger_error(mysql_error(),E_USER_ERROR); 

Upvotes: 1

Views: 441

Answers (2)

Hetal1311
Hetal1311

Reputation: 364

For DB select you have to add mysql_select_db(DatabaseName); or $dbconn=mysql_select_db($dbname,$MyConn);in MyConn.php

For update in Database you have to use connection variable which is in MyConn.php i.e.$MyConn as follows

$result = mysql_query($sql_statement . $sql_image . $sql_expire . " WHERE Ad_ID=" . $_REQUEST['ADID'],$MyConn);

or

 $result = mysql_query($sql_statement . $sql_image . $sql_expire . " WHERE Ad_ID=" . $_REQUEST['ADID'],$dbconn);

respectively

Hope It Helps!!!!!!!

Upvotes: 1

Jared
Jared

Reputation: 12524

This may or may not be declared in your MyConn.php but all you need is a line:

mysql_select_db($db_name);

Where $db_name is the name of your database.

This should come before you attempt to execute the query.

Upvotes: 4

Related Questions