SQL Insert INTO question
am 07.12.2005 17:59:26 von geekgirl1
First time poster.....
This is the problem. I want to add the value of $_POST[review] to the
reviews table where the unique id from the reviews table equals the
review id on my form. The statement below does not work. Should I
use UPDATE instead?
"INSERT INTO reviews (review_txt)
=09VALUES
=09('$_POST[review]') WHERE review_id =3D $id";
Marian
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: SQL Insert INTO question
am 07.12.2005 18:08:13 von Bastien Koert
Assuming a new record
"INSERT INTO reviews (review_id, review_txt)
VALUES
($id,'$_POST[review]')";
Bastien
>From: geekgirl1
>To: php-db@lists.php.net
>Subject: [PHP-DB] SQL Insert INTO question
>Date: Wed, 7 Dec 2005 11:59:26 -0500
>
>First time poster.....
>
>This is the problem. I want to add the value of $_POST[review] to the
>reviews table where the unique id from the reviews table equals the
>review id on my form. The statement below does not work. Should I
>use UPDATE instead?
>
>"INSERT INTO reviews (review_txt)
> VALUES
> ('$_POST[review]') WHERE review_id = $id";
>
>Marian
>
>--
>PHP Database Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: SQL Insert INTO question
am 08.12.2005 12:33:51 von El Bekko
geekgirl1 wrote:
> First time poster.....
>
> This is the problem. I want to add the value of $_POST[review] to the
> reviews table where the unique id from the reviews table equals the
> review id on my form. The statement below does not work. Should I
> use UPDATE instead?
>
> "INSERT INTO reviews (review_txt)
> VALUES
> ('$_POST[review]') WHERE review_id = $id";
>
> Marian
"INSERT INTO reviews (review_txt)
VALUES
('$_POST[\'review\']') WHERE review_id = '$id'";
There, now it should work :)
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: SQL Insert INTO question
am 08.12.2005 21:09:50 von David Mitchell
------=_Part_24498_6920250.1134072590928
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Is that a syntax supported by MySQL? That is, an INSERT with WHERE clause?
I tried it against Oracle, and it doesn't work (can you imagine how
upsetting it would have been to have learned that, after having worked with
SQL for several years, one can do an update using an INSERT statement?).
I'm pretty sure it doesn't work against MS SQL Server, DB2, etc (not 100%
certain, but 99.9%).
On 12/8/05, El Bekko wrote:
>
> geekgirl1 wrote:
> > First time poster.....
> >
> > This is the problem. I want to add the value of $_POST[review] to the
> > reviews table where the unique id from the reviews table equals the
> > review id on my form. The statement below does not work. Should I
> > use UPDATE instead?
> >
> > "INSERT INTO reviews (review_txt)
> > VALUES
> > ('$_POST[review]') WHERE review_id =3D $id";
> >
> > Marian
>
> "INSERT INTO reviews (review_txt)
> VALUES
> ('$_POST[\'review\']') WHERE review_id =3D '$id'";
>
> There, now it should work :)
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
------=_Part_24498_6920250.1134072590928--
Re: Re: SQL Insert INTO question
am 08.12.2005 21:50:41 von dpgirago
In full agreement here. I scratched my head this morning, then looked up
"INSERT" in the MySQL Docs for a sanity check. It must be an UPDATE
statement to work as indicated below.
David
<< Sorry David Mitchell >>
============================================================ ==============
Is that a syntax supported by MySQL? That is, an INSERT with WHERE clause?
I tried it against Oracle, and it doesn't work (can you imagine how
upsetting it would have been to have learned that, after having worked with
SQL for several years, one can do an update using an INSERT statement?).
I'm pretty sure it doesn't work against MS SQL Server, DB2, etc (not 100%
certain, but 99.9%).
On 12/8/05, El Bekko wrote:
>
> geekgirl1 wrote:
> > First time poster.....
> >
> > This is the problem. I want to add the value of $_POST[review] to the
> > reviews table where the unique id from the reviews table equals the
> > review id on my form. The statement below does not work. Should I
> > use UPDATE instead?
> >
> > "INSERT INTO reviews (review_txt)
> > VALUES
> > ('$_POST[review]') WHERE review_id = $id";
> >
> > Marian
>
> "INSERT INTO reviews (review_txt)
> VALUES
> ('$_POST[\'review\']') WHERE review_id = '$id'";
>
> There, now it should work :)
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: SQL Insert INTO question
am 08.12.2005 22:22:12 von Graham Cossey
How about:
$review =3D $_POST['review'];
$sql =3D "SELECT * FROM reviews WHERE review_id =3D '$id'";
$res =3D mysql_query($sql) or die ("Query failed: " . mysql_error());
if (mysql_num_rows($res) > 0)
{
$sql_upd =3D "UPDATE reviews
SET review_text =3D '$review'
WHERE review_id =3D '$id' ";
$res_upd =3D mysql_query($sql_upd) or die ("Query failed: " . mysql_er=
ror());
}else{
$sql_ins =3D "INSERT INTO reviews (review_id, review_text)
VALUES('$id', '$review')";
$res_ins =3D mysql_query($sql_ins) or die ("Query failed: " .
mysql_error());
}
--
Graham
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: SQL Insert INTO question
am 09.12.2005 00:41:20 von Frank Flynn
--Apple-Mail-2--988225320
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed
Geekgirl,
Let me expand on the answers so far...
There are two things you can do here: add a new record or update an
existing one. In SQL this makes a big difference and in your
question you've confused the two (MySQL has a trick which kind of
mixes the two and it is limited). I'm not exactly sure what you
really intend to do so I'll show you are three and explain each:
A schema which might be like yours:
CREATE TABLE reviews
( review_id int NOT NULL auto_increment,
review_txt text,
primary key (review_id))
- To insert a new row
INSERT INTO reviews (review_txt)
VALUES ('$_POST[review]')
This will create a new record and will automatically generate the new
review_id, if you want to specify the review_id you can do this
INSERT INTO reviews (review_id , review_txt)
VALUES ( $id, '$_POST[review]')
But this review_id must NOT exist or you will get a 'duplicate key'
error.
-To update an existing row use this:
UPDATE reviews
SET review_txt = '$_POST[review]'
WHERE review_id = $id
If the row with that review_id does not exist you will not get an
error - but nothing will get saved; if the row with that review_id
exists it will get updated.
-Finally there is a way to mix the two (I believe this is MySQL only):
REPLACE INTO reviews (review_id , review_txt)
VALUES ( $id, '$_POST[review]')
This works just like INSERT except if this review_id exists the old
one will get deleted (this is because it is defined as the key in the
create table statement). There is an important difference between
this and the UPDATE statement; this is if you have other columns
which are not mentioned in an UPDATE statement they will stay
unchanged but REPLACE will delete the old row so all other columns
will be their default values or NULL.
Feel free to write with more questions,
Good Luck,
Frank
On Dec 7, 2005, at 9:42 PM, geekgirl1 wrote:
>
> This is the problem. I want to add the value of $_POST[review] to the
> reviews table where the unique id from the reviews table equals the
> review id on my form. The statement below does not work. Should I
> use UPDATE instead?
>
> "INSERT INTO reviews (review_txt)
> VALUES
> ('$_POST[review]') WHERE review_id = $id";
>
> Marian
--Apple-Mail-2--988225320--