Page 1 of 1

update script

Posted: Thu Aug 31, 2006 1:24 pm
by bluelad
Is this update script correct??

Code: Select all

<?php
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);

$songid = $_POST['id'];
$songname = $_POST['song'];
$songartist =$_POST['artist'];
$songwww = $_POST['www'];

$sql=mysql_query("UPDATE rec_song SET song='$songname', artist='$songartist', www='$songwww' where id='$id'");

echo "updated info:::$songname and $songartist and $songwww";
?>

Posted: Thu Aug 31, 2006 1:26 pm
by Luke
in what way? Is the SQL query correct? The PHP code? Practice? What are you asking?

Posted: Thu Aug 31, 2006 1:27 pm
by andym01480
Nearly in terms of code! $songid not $id

BUT!
Put a password on your database root and create another user with password.
Check and clean your data especially if from a public form!
The or die (mysql_error()) will tell you if there is something wrong while testing

Code: Select all

<?php
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);

$songid = $_POST['id'];
$songname = $_POST['song'];
$songartist =$_POST['artist'];
$songwww = $_POST['www'];

$sql=mysql_query("UPDATE rec_song SET song='$songname', artist='$songartist', www='$songwww' where id='$songid'") or die (mysql_error());

echo "updated info:::$songname and $songartist and $songwww";
?>

Posted: Thu Aug 31, 2006 1:33 pm
by Benjamin

Code: Select all

<?php
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);

$songid = mysql_real_escape_string(trim(strip_tags($_POST['id'])));
$songname = mysql_real_escape_string(trim(strip_tags($_POST['song'])));
$songartist = mysql_real_escape_string(trim(strip_tags($_POST['artist'])));
$songwww = mysql_real_escape_string(trim(strip_tags($_POST['www'])));

$sql= mysql_query("UPDATE rec_song SET song='" . $songname . "', artist='$songartist', www='$songwww' where id='$songid'") or die (mysql_error());

echo "updated info:::$songname and $songartist and $songwww";
?>

Posted: Thu Aug 31, 2006 1:45 pm
by bluelad
these are the eroors i get on running both your scripts

Code: Select all

Notice: Undefined index: id in d:\program files\easyphp1-8\www\recommend\update1.php on line 5
Unknown column 'song' in 'field list'
I am trying only a simple update script and its eating my head off :evil:

Posted: Thu Aug 31, 2006 1:49 pm
by Benjamin
Those are notices. You will still need to test to see if the values are empty or not.

Code: Select all

if ((!empty($_POST['id'])) && (!empty($_POST['song'])) && (!empty($_POST['artist'])) && (!empty($_POST['www']))) {
  // update the database
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);

$songid = mysql_real_escape_string(trim(strip_tags($_POST['id'])));
$songname = mysql_real_escape_string(trim(strip_tags($_POST['song'])));
$songartist = mysql_real_escape_string(trim(strip_tags($_POST['artist'])));
$songwww = mysql_real_escape_string(trim(strip_tags($_POST['www'])));

$sql= mysql_query("UPDATE rec_song SET song='" . $songname . "', artist='$songartist', www='$songwww' where id='$songid'") or die (mysql_error());

echo "updated info:::$songname and $songartist and $songwww";

} else {
  echo 'All fields are required.';
}

Posted: Thu Aug 31, 2006 1:50 pm
by andym01480
Better post your form code! what have you called the songid in the form?

What are your database table columns called?

Posted: Thu Aug 31, 2006 2:02 pm
by bluelad
there are 4 fields in my table rec_song.......... id, name artist, www.

All i want to do is simply update the fields regularly with new songs and their singers.

Posted: Thu Aug 31, 2006 2:07 pm
by andym01480

Code: Select all

$sql= mysql_query("UPDATE rec_song SET song='" . $songname . "', artist='$songartist', www='$songwww' where id='$songid'") or die (mysql_error());
should be

Code: Select all

$sql= mysql_query("UPDATE rec_song SET name='" . $songname . "', artist='$songartist', www='$songwww' where id='$songid'") or die (mysql_error());


Song=name on your table!

Check your form names match what you are looking for too and then you should be okay although astions checks will help too!

Headache over.

Posted: Thu Aug 31, 2006 2:25 pm
by bluelad
it dint dude. It isnt showing any error. But the table isnt updating itself. But when i do it thru my phpMy admin it does.
I am giving my update.php and update1.php files......first files fetches the contents and the seond file updates it (hopefully)

update.php

Code: Select all

<html>
<body>
<?php
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);


$songid = $_POST['id'];
$songname = $_POST['name'];
$songartist =$_POST['artist'];
$songwww = $_POST['www'];

$sql="select *  from rec_song";
$query=mysql_query($sql);
while($row = mysql_fetch_array($query))
{
?>
<form action="update1.php" method="post">
Song name::<input type="text" name="song" value="<?=$row[1]?>">
<br>
Song author::<input type="text" name="artist" value="<?=$row[2]?>">
<br>
Song www::<input type="text" name="www" value="<?=$row[3]?>"><br>
<input type="submit" name="submit" value="update">
</form>
<?php
}
?>
</body>
</html>
update1.php

Code: Select all

<?php
if ((!empty($_POST['id'])) && (!empty($_POST['song'])) && (!empty($_POST['artist'])) && (!empty($_POST['www']))) {
  // update the database
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);

$songid = mysql_real_escape_string(trim(strip_tags($_POST['id'])));
$songname = mysql_real_escape_string(trim(strip_tags($_POST['name'])));
$songartist = mysql_real_escape_string(trim(strip_tags($_POST['artist'])));
$songwww = mysql_real_escape_string(trim(strip_tags($_POST['www'])));

$sql= mysql_query("UPDATE rec_song SET name='" . $songname . "', artist='" . $songartist ."', www='" . $songwww ."' where id='" . $songid ."'") or die (mysql_error());
echo "updated info:::$songname and $songartist and $songwww";

} else {
  echo 'All fields are required.';
} 
?>

Posted: Thu Aug 31, 2006 3:17 pm
by andym01480
You are not returning the value of songid to the update1 script. So update1 has no songid to update and doesn't update the table use a hidden input field with the value from your query...

Code: Select all

<html>
<body>
<?php
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);


$songid = $_POST['id'];
$songname = $_POST['name'];
$songartist =$_POST['artist'];
$songwww = $_POST['www'];

$sql="select *  from rec_song";
$query=mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{
?>
<form action="update1.php" method="post">
Song name::<input type="text" name="song" value="<?=$row['name']?>">
<br>
Song author::<input type="text" name="artist" value="<?=$row['artist']?>">
<br>
Song www::<input type="text" name="www" value="<?=$row['www']?>"><br>
<input type=hidden name=id value="<?=$row['songid']?>">
<input type="submit" name="submit" value="update">
</form>
<?php
}
?>
</body>
</html>

Posted: Fri Sep 01, 2006 2:07 am
by bluelad
nope dint help. There are no errors but the info doesnt get upadted...
Here look at how i created my tables

Code: Select all

create table rec_song ( id INT NOT NULL,
PRIMARY KEY ( id ) ,
name VARCHAR ( 50 ) ,
artist VARCHAR ( 50 ) ,
www VARCHAR ( 255 )
) ;

Posted: Fri Sep 01, 2006 2:40 am
by RobertGonzalez
You may want to condider checking if the mysql_query() assignment returned true or false. You might also want to check mysql_affected_rows() on your returned result.

Posted: Fri Sep 01, 2006 7:30 am
by andym01480
Firstly view source on your form script and check all the hidden values are there in the form!

Then I would change your updating query so that it is a variable, echo it and then do the mysql_query. The you can look to see what is happenning!

Code: Select all

$query="UPDATE rec_song SET name='$songname', artist='$songartist', www='$songwww' where id='$songid'";
echo "$query <p>"; //during development
$sql= mysql_query($query) or die (mysql_error());
Then use myql_affected_row() etc.

Note if you are printing out what you have in the database your browser will often use a cached version of the page, with the old results!!! <Shift><Refresh> to force a new page. and use

Code: Select all

<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="-1">


in the headers, but IE is buggy on that (no surprise), so Microsoft recommend

Code: Select all

<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
after the </body>

The next post will be someone saying that is not standards compliant! But its what Microsoft recommend to not cache a page!!!!!!!

Then when you have it working, I would recode it into one file for neatness!

Code: Select all

if (isset($_POST['submitted']){ // form submitted, so do the updating
//blah blah
exit();
}
//not submitted so do the update form with a hidden field called submitted

Posted: Fri Sep 01, 2006 8:28 am
by bluelad
thanks got it
:)