Problems with UPDATE statements

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Problems with UPDATE statements

Postby bigdaisy » 08. June 2010 12:34

Hi,

My first topic. I'm going mad. The only part of this coding that doesn't work is the UPDATE statement. Can someone tell me why please? The echos prove the rest is functioning properly. After the code there are the results of the different attempts.

Fingers crossed,
Mike

<?php
include('misc.inc');
$connection = mysql_connect($host,$user,$password)
or die ('No connection');

$db = mysql_select_db($database,$connection)
or die ('No selection');

$query = "SELECT * FROM books";
$result = mysql_query($query)
or die('mysql error ' . mysql_error() . ' in query ' . $query);

$ct1 = array('Kunst','Kinderbüchen','Literatur','Geography','Photography');
$ix = -1;

while ($row = mysql_fetch_array($result))
{
extract($row);
$ix++;
$cat = $ct1[$ix];
echo $cat,"<br>",$ix,"<br>";

$query = "UPDATE books SET catalog='$cat'";
$result2 = mysql_query($query)
or die('mysql error ' . mysql_error() . ' in query ' . $query);

echo $catalog,"<br>",$ix,"<br>";

if ($ix == 4)
$ix = -1;
};
exit;
?>

LOCAL & SERVER - BOTH PUT cat IN ALL ROWS = it 'worked'!
$query = 'UPDATE books SET catalog="cat"';
$query = "UPDATE books SET catalog='cat'";


LOCAL & SERVER - PUTS '$cat' IN ALL ROWS = not the value of $cat
$query = 'UPDATE books SET catalog="$cat"';

LOCAL - PUTS NOTHING IN ALL ROWS = no changes
SERVER - PUTS the value of $ct1[3] IN ALL ROWS = not the value of $ct1[$ix]
$query = "UPDATE books SET catalog='$cat'";


LOCAL & SERVER - PUTS '$ct1[$ix]' IN ALL ROWS = not the value of $ct1[$ix]
$query = 'UPDATE books SET catalog="$ct1[$ix]"';


LOCAL - PUTS the value of $ct1[1] IN ALL ROWS = not the content of $ct1[$ix]
SERVER - PUTS NOTHING IN ALL ROWS = no changes
$query = "UPDATE books SET catalog='$ct1[$ix]'"; :(
bigdaisy
 
Posts: 1
Joined: 17. May 2010 16:26
Location: Amsterdam, NL

Re: Problems with UPDATE statements

Postby Radzio » 08. June 2010 13:21

From your post I understand only that you have a problem with UPDATE statement.
I can give you these hints (as I don't really get what is the real problem):
1) if you want to have value of variable in a string then use double quotes instead of single ones or use this syntax:
Code: Select all
'UPDATE books SET catalog="' . $cat . '"'

2) if you don't want UPDATE to affect all rows then you must use WHERE statement
Radzio
 
Posts: 81
Joined: 05. June 2010 12:26


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 62 guests