473,808 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL query problems

Eh, I was wondering if anyone could help me with a SQL query problem
I'm having. I'm a complete newbie to SQL and MySQL, so any help would
be greatly appreciated.

I am using PHP to allow users to submit ISBNs, prices, and item
conditions into a MySQL database. The table the values are inserted
into is declared with

CREATE TABLE prices (isbn VARCHAR(10), price FLOAT UNSIGNED,
conditions VARCHAR(5))

I use this SQL query to check whether or not the passed ISBN is
already in the database. If it does, it updates the columns with the
new price and conditions. If it does not, it inserts a fresh row. The
variables $isbn, $price, and $conditions are all PHP variables which
are automatically inserted into the query.

BEGIN
SELECT "true" INTO $exists FROM prices WHERE isbn = "$isbn";
IF $exists = "true" THEN
UPDATE prices SET price = $price, conditions = "$condition s" WHERE
isbn = "$isbn";
ELSE
INSERT INTO prices (isbn, price, conditions) VALUES ("$isbn",
$price, "$condition s");
ENDIF;
END

However, it doesn't understand my SELECT statement, and I'm sure it
has other problems.

Regards,
RG
Jul 19 '05 #1
1 3457
Hi,
First a question, you're coding in PHP? If so, then your entire syntax
is wrong. If by BEGIN you mean to start a transaction, the syntax
should be:
mysql_query("BE GIN");

To commit a transaction you would use
mysql_query("CO MMIT");

Next, if you want to check whether a ISBN number is already in your
table, and if so do an update and otherwise do an insert, you are
probably looking for some code that looks somewhat like this:

// First, connect to the server
if(!($connectio n = @ mysql_connect($ hostName, $username,$pass word)))
showerror();

// Select the database
if (!(@ mysql_selectdb( $databaseName, $connection)))
showerror();

// Check whether ISBN number is present
$query = "SELECT * FROM prices WHERE isbn = " . $isbn;

if (!($result = @ mysql_query ($query, $connection)))
showerror();

// if there is a row present the isbn needs to be updated, otherwise
it needs to be inserted
if (mysql_num_rows ($result) > 0) {
$query = "UPDATE prices SET price = " . $price . ", conditions = " .
$conditions . " WHERE isbn = " . $isbn;
}
else {
$query = "INSERT INTO prices (isbn, price, conditions) VALUES (" .
$isbn . ", " . $price . ", " . $conditions . ")";
}


if (!(@ mysql_query ($query, $connection)))
showerror();


Note that the variables $hostName, $username, etc... are stored in a
different file, which you must include at the top of your PHP file
that's calling the code I gave you above.
Also the function showerror() should be in a seperate file, which
should also be included.

The showerror() function looks like this:
<?php
function showerror() {
die("Error " . mysql_errno() . " : " . mysql_error());
}
?>

I hope this will help get you started, but it would probably be a good
idea you buy a book to get the hang of some basic stuff. I can suggest
"Web Database Applications with PHP & MySQL" by H.E. Williams and D.
Lane.

All the best, Jonck
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5876
by: carverk | last post by:
Hello All I'm in the middle of moving a MS Access DB to a MySql backend. I have figured out about 90% of the problems I have faced, execpt for this one. I have 3 Queries, which pull records depending on a date range. (Today,Last 7 Days,Next 7 Days) The one I'm having problems with is the "Last 7 Days" Query.
0
4123
by: Mike N. | last post by:
Hello to all: First let me apologize for the length of this question, I've made an attempt to include as much information as is needed to help with the question. I am having problems putting together a query to pull out an alternative hierarchical view of my data. The database is implemented under SQL Sever 2000 and I am writing the front end using VB.Net and ADO.net. The following is the portion of my database structure that I am...
1
6178
by: Jeff Blee | last post by:
I hope someone can help me get this graph outputing in proper order. After help from Tom, I got a graph to display output from the previous 12 months and include the average of that output all in the one graph. The output was in the order of the months, but after unioning with the averages SQL code, the order is lost. Below is the full sql code that is the data source for the graph: SELECT (Format(.,"mmm"" '""yy")) AS Month,...
5
1999
by: sulemanzia | last post by:
hi. i am working first time with query and reports. i have created a database. i have reports and query. i have a button in my form by the name of (View reports) if a user clicks on that button it opens another form with 3 options 1) Problems all report 2) problems resolved 3) Problems not resolved Now what i really want is when a user clicks on Problems all report it should go directly into excel worksheet based on my query which i have...
3
1536
by: faceman28208 | last post by:
Over the past few years I have consulted on six large projects that all independently arrived at the same moronic design desision: The use of SQL query classes. No, I don't mean a class hierararchy like: SQL Query UpdateQuery SelectQuery Where you hide the details of the underlying database API. (Along the lines of what Rogue Wave does.)
22
31214
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for June, and will return all records in that month.
6
2781
by: gerbski | last post by:
Hi all, I am relatively new to ADO, but up to now I got things working the way I wanted. But now I've run into somethng really annoying. I am working in MS Access. I am using an Access frontend separately from a backend. The tables from the backend database are linked in the frontend database. In the frontend there is a Form with a listbox in it. The listbox rowsource is a query that selects all the records from a (linked)
2
2334
by: webhead74 | last post by:
Hi, I'm having intermittent problems with queries from my php script to a postgresql database. I have a form where I can enter a search query - for instance a last name. This leads to a results page with a brief bit of information about each of the matching results. From there, I can click a link associated with any of the results which takes me to a page with all of the details. Pretty standard stuff.
16
3526
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for renaming the duplicate records? My thinking was to take the results of the duplicate query, and somehow have it number each line where there is a duplicate (tried a groups query, but "count" won't work), then do an update query to change the duplicate to...
9
2993
by: Bob Darlington | last post by:
The following query opens slowly the first time it is opened (6-7 seconds), but then is less than one second for the next random number of openings before slowing (6-7 seconds) again. SELECT tTenantDetails.LAN, tTenants.Archive, tTenants.PropNum FROM tTenants INNER JOIN tTenantDetails ON tTenants.LAN = tTenantDetails.LAN WHERE (((tTenants.PropNum)=10)); I'm using Access 2002 with XP Pro OS and 2GB RAM. tTenants and tTenantDetails have...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10631
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.