Page 1 of 1

Some help please

Posted: Mon Jan 18, 2010 3:23 am
by atsa1
I have this code here that echo's out from mysql all rows with id. Now i haave to add delete button what takes mysql row code and uses it to delete certain row in mysql. code looks like this:

Code: Select all

 
<?php
mysql_connect ("localhost", "myuser", "mypass") or die ('Error: ' . mysql_error());
mysql_select_db ("foral");
if(!isset($cmd)) 
{
//query
$query = mysql_query("SELECT * FROM tellimus order by id");
//display row
while ($row = mysql_fetch_array($query)) {
  echo 
    '<td border="1" size="3"><input name="id" type="text" size="1" value='.$row['id'].'>'."".'<td border="1" size="3"><input name="id" type="text" size="16" value='.$row['kood'].'>'."".'<td border="1" size="3"><input name="id" type="text" size="19" value='.$row['nimetus'].'>'."".'<td border="1" size="3"><input name="id" type="text" size="11" value='.$row['kogus'].'>'."".'<td border="1" size="3"><input name="id" type="text" size="13" value='.$row['number'].'>'."".'<td border="1" size="3"><input name="id" type="text" size="11" value='.$row['kes'].'>'."".'<td border="1" size="3"><a href=\'index.php?cmd=delete&id='.$row['id'].'\'>'.$row['id'].' - Delete</a>'.
  "<br />";}
  
     }
 
if($_GET["cmd"]=="delete")
{
    $sql = 'DELETE * FROM tellimus WHERE id='.$row['id'].'';
    $query = mysql_query($sql);
    echo "Row deleted!";
}
?>
 
well it seems to work but it wont delete the row.
It echos out the "Row deleted" but in mysql the row still exist's. Where am i wrong here?
PS! I'm quite newbie in mySQL part. This is my first script to add/remove row's in MySQL.

Re: Some help please

Posted: Mon Jan 18, 2010 12:23 pm
by Brian Swan
Try eliminating the * from your delete query:

$sql = 'DELETE FROM tellimus WHERE id='.$row['id'].'';

I think that statement (with the *) is producing an error on the server, but you aren't catching it in your PHP code.

Hope that helps.

-Brian

Re: Some help please

Posted: Tue Jan 19, 2010 3:42 am
by atsa1
Oops. After cheking about 9 times to my MySQL tables i found that it Writes perfectly but reading is wrong.
Well here's the script:

Code: Select all

 
<?php
mysql_connect ("localhost", "myuser", "mypass") or die ('Error: ' . mysql_error());
mysql_select_db ("foral");
if(!isset($cmd)) 
{
//query
$query = mysql_query("SELECT * FROM tellimus order by id");
//display row
while ($row = mysql_fetch_array($query)) {
$id=$row['id'];//take out the id
  echo 
    '<form action="muuda.php?cmd=change" method="post"><td border="1" size="3"><input name="id" type="text" size="1" value='.$row['id'].'>'."".'<td border="1" size="3"><input name="kood" type="text" size="32" value='.$row['kood'].'>'."".'<td border="1" size="3"><input name="nimetus" type="text" size="36" value='.$row['nimetus'].'>'."".'<td border="1" size="3"><input name="kogus" type="text" size="12" value='.$row['kogus'].'>'."".'<td border="1" size="3"><input name="number" type="text" size="40" value='.$row['number'].'>'."".'<td border="1" size="3"><input name="kes" type="text" size="15" value='.$row['kes'].'>';
    echo '<td border="1" size="3"><A HREF="javascript&#058;decision(\'Kas kustutame?\', \'loe.php?cmd=delete&id='.$id.'\')">'.$id.' - Delete</a>'." ".'<td border="1" size="3"><input type="submit" value="'.$id.' - Muuda" style="background:none;border:0;color:#0000cc"/>'."<br /></form>";}
  
     }
?>
 
problem is that it will read only first half before the space. If i have written in MySQL like a centence: My name is
Then it will read out from it only: My
Name is words are not loaded. i cant figure it out.

Re: Some help please

Posted: Wed Jan 20, 2010 2:59 pm
by Brian Swan
It looks to me like there are some problems with the form you are printing out. I think some tags aren't closed properly. I'd start by looking closely at the form.

Hope that helps.
-Brian

Re: Some help please

Posted: Thu Jan 21, 2010 1:22 am
by atsa1
But does this cause this action:
When i write into database: Hello World. Output = Hello
When i write into database: Hello&nbsp;World Output= Hello World.
:banghead:

But i take a close look at the output code then. Maby i find something wrong...

Re: Some help please

Posted: Thu Jan 21, 2010 4:04 am
by carolin
some tags are not closed..
and try some modifications in delete query..

Re: Some help please

Posted: Thu Jan 21, 2010 8:33 am
by atsa1
Well i found my answer. Was reading about echo and got some idea from echo "text here $text" and echo 'text here $text'

so in my line: input name="id" type="text" size="1" value='.$row['id'].'>' i was missing "" over value=
I made it look like this: input name="id" type="text" size="1" value="'.$row['id'].'">' And voila. Write as mutch text into row. All gets read out!.

Thank's for directing to unclosed tags.

And very big thanks to Brian Swan.
Removing the * helped.
before i gave the row command: $sql = 'DELETE * FROM tellimus WHERE id='.$row['id'].''; delete *ALL from ... where id!
Now: $sql = 'DELETE FROM tellimus WHERE id='.$row['id'].''; delete row where id=


Old solution deleted too (just didn't notice it becausae i was looking wrong line). Old solution deleted only LAST line not the line where id='.$row['id'].'

Re: Some help please

Posted: Fri Jan 29, 2010 5:19 am
by chicago
I think your problem is that you have not closed your all the tags properly in the coding.