473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 = "$conditions" WHERE
isbn = "$isbn";
ELSE
INSERT INTO prices (isbn, price, conditions) VALUES ("$isbn",
$price, "$conditions");
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 3435
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("BEGIN");

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

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(!($connection = @ mysql_connect($hostName, $username,$password)))
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
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...
0
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...
1
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...
5
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...
3
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...
22
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...
6
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...
2
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...
16
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...
9
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.