Update A Row in SQl

Codes here !

Moderators: egami, macek, gesf

Post Reply
Zypho
New php-forum User
New php-forum User
Posts: 10
Joined: Fri Mar 19, 2004 9:49 am
Location: Canada
Contact:

Thu Apr 08, 2004 5:13 pm

Hey does this look right because it says it worked but it doesnt update?

Code: Select all

<?php 

include "config.php";

$db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");
/* We have now connected, unless you got an error message */
/* Lets save some news ! */
$query = "UPDATE news SET title='$_POST[title]', news='$_POST[news]' WHERE id = $_GET[id]";
$result = mysql_query($query);
echo "News item saved.";
mysql_close($db);
?>

Enquea
New php-forum User
New php-forum User
Posts: 34
Joined: Fri Feb 27, 2004 5:57 am
Contact:

Thu Apr 08, 2004 5:30 pm

Maybe this does the tric :D

I've changed the WHERE id = $_GET to $_POST
[php]
<?php
include ("config.php");

$db = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db ($db_name) or die ("Cannot connect to database");
/* We have now connected, unless you got an error message */
/* Lets save some news ! */
mysql_query("UPDATE news ".
" SET title = '$_POST[title]', ".
" news = '$_POST[news]' ".
" WHERE id = '$_POST[id]' ") or die (mysql_error());

echo "News item saved.";
mysql_close($db);
?>
[/php]
Greetz,
Enq
Practise bares art or something...... 8)

Zypho
New php-forum User
New php-forum User
Posts: 10
Joined: Fri Mar 19, 2004 9:49 am
Location: Canada
Contact:

Thu Apr 08, 2004 7:38 pm

i tryed that already and it didnt work:(

User avatar
swirlee
Moderator
Moderator
Posts: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Thu Apr 08, 2004 8:47 pm

Echo your query and see if it looks like you expect it to. Post it here.
Image

liquedus
php-forum Active User
php-forum Active User
Posts: 264
Joined: Tue Apr 08, 2003 5:18 am
Location: Ottawa, Canada
Contact:

Thu Apr 08, 2004 9:36 pm

$query = "UPDATE news SET title='".$_REQUEST['title']."', news='".$_REQUEST['news']."' WHERE id = ".$_REQUEST['id'];

wow... look at all these new emoticons! :-D

User avatar
gesf
Moderator
Moderator
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Fri Apr 09, 2004 12:32 am

If you´re using a form to post the data, use $_GET or $_POST depending on its method. Otherwise use $_GET.
[php]<?php
$title = $_POST['title'];
$news = $_POST['news'];
$id = $_POST['id'];

mysql_query("UPDATE news SET title = '$title', news = '$news' WHERE id = '$id' ") or die (mysql_error());
?>[/php]
Sincerely,
Gonçalo "gesf" Fontoura

Post Reply