update script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

update script

Post 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";
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

in what way? Is the SQL query correct? The PHP code? Practice? What are you asking?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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";
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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";
?>
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

Post 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:
Last edited by bluelad on Thu Aug 31, 2006 1:51 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.';
}
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Better post your form code! what have you called the songid in the form?

What are your database table columns called?
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

Post 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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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.
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

Post 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.';
} 
?>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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>
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

Post 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 )
) ;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

Post by bluelad »

thanks got it
:)
Post Reply