Jump to content


Photo

UPDATING record in mySQL. PLEASE HELP


  • Please log in to reply
23 replies to this topic

#1 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 07:37 PM

Hi

i want to get data from mySQL and wish to delete or update it.

The delete is fine but when it comes to update, nothing happens at all.

i want the data to be present in the textboxes so that the user does not have to type the whole thing in again.  PLEASE PLEASE help. i need it by tomorrow.

Thank you.

this is my code:

edit.php

<?
// Connect database
include("dbConfig.php");

// Get all records in all columns from table and put it in $result.
$result=mysql_query("select * from articles ORDER BY id");

/*Split records in $result by table rows and put them in $row.
Make it looping by while statement. */
while($row=mysql_fetch_assoc($result)){

// Output
echo "ID : {$row['id']} <br/>";
echo "Title : {$row['title']} <br/>";
echo "Content : {$row['content']} <br/>";
echo "Filename : {$row['filename']} <hr>";

// Add a link with a parameter(id) and it's value.
echo '<a href="update.php?id='.$row['id'].'">Update</a>';
echo "<HR>";

}

mysql_close();
?>
//------------------------------------------------------------------------------------------



update.php

<?
// Connect database.
include("dbConfig.php");
if($Submit){
// Get parameters from form.
$id=$_POST['id'];
$title=$_POST['title'];
$content=$_POST['content'];
$filename=$_POST['filename'];
mysql_query("update articles set title='$title', content='$content', filename='$filename' where id='$id'");
header("location:edit.php");


// Get id parameter (GET method) from edit.php
$id=$_GET['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
//$result=mysql_query("select * from articles where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);
}
?>
<html>
<body>
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>Name :
<input name="name" type="text" id="title" value="<? echo $row['title']; ?>"/>
<br />
Email :
<input name="email" type="text" id="content" value="<? echo $row['content']; ?>"/>
<br />
Tel :
<input name="tel" type="text" id="filename" value="<? echo $row['filename']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 07:41 PM

You are not exiting from the if statement in the correct place.  Change:

<?
// Connect database.
include("dbConfig.php");
if($Submit){
// Get parameters from form.
$id=$_POST['id'];
$title=$_POST['title'];
$content=$_POST['content'];
$filename=$_POST['filename'];
mysql_query("update articles set title='$title', content='$content', filename='$filename' where id='$id'");
header("location:edit.php");


// Get id parameter (GET method) from edit.php
$id=$_GET['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
//$result=mysql_query("select * from articles where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);
}
?>

to:

<?php

// Connect database.
include("dbConfig.php");
if($Submit){
	// Get parameters from form.
	$id=$_POST['id'];
	$title=$_POST['title'];
	$content=$_POST['content'];
	$filename=$_POST['filename'];
	mysql_query("update articles set title='$title', content='$content', filename='$filename' where id='$id'");
	header("location:edit.php");
	exit;
}

// Get id parameter (GET method) from edit.php
$id=$_GET['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
//$result=mysql_query("select * from articles where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);
?>


#3 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 07:47 PM

Thank for the reply.

it is now giving me an error.

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/acreinternet.co.uk/subdomains/stage2/httpdocs/new/update.php on line 23


#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 07:50 PM

That means your query is failing.  Probably because you commented out your query:

// Get records in all columns from table where column id equal in $id and put it in $result.
//$result=mysql_query("select * from articles where id='$id'");


#5 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 20 August 2006 - 07:50 PM

You can't just ASSUME when you toss a query, it will be executed ok.

<?php
mysql_query($query) or die('Query Failed: '.$query.' MySQL returned: '.mysql_error());
?>


#6 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 07:56 PM

Thank you for your replies.

everything is fine now and the text boxes appear with the data inside them BUT once i click on submit, nothing happens .

EXAMPLE: if i change name from DAVID to JOHN and click on update, the page refreshes and the name changes to DAVID again.

#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 07:58 PM

If register_globals is not on, then you need to change this:

if($Submit){

to

if($_POST['Submit']){

Which you should probably do anyway.

#8 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 08:01 PM

now it goes to page edit, BUT the data is the same.

#9 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 08:09 PM

Anyone knows why? :-[

#10 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 08:12 PM

Echo out your query, then place it into phpMyAdmin or MySQL Query Browser and check to make sure that it is executing correctly.

#11 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 08:21 PM

When i echo out my $result, the output is Resource id #4

#12 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 08:37 PM

please help

#13 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 20 August 2006 - 08:41 PM

you need to test the string...

change thsi bit of ode so that the query string is in a var that you can print to screen...

<?php
if($Submit){
	// Get parameters from form.
	$id=$_POST['id'];
	$title=$_POST['title'];
	$content=$_POST['content'];
	$filename=$_POST['filename'];
	$qry = "update articles set title='$title', content='$content', filename='$filename' where id='$id'";
	echo $qry;
	mysql_query($qry);
	//header("location:edit.php"); comment this to make sure your qry is echoed...
	//exit;
}
?>


follow me on twitter @PHPsycho

#14 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 09:06 PM

after i changed the code to the above reply, i got this.


Parse error: parse error, unexpected $ in /var/www/vhosts/acreinternet.co.uk/subdomains/stage2/httpdocs/new/update.php on line 65

line 65 is the HTML TAG </body>

#15 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 20 August 2006 - 09:16 PM

Quite often when I get this error, it means I have an open bracket either a "() or {} or quotation marks " or ' not matched up........have one side open and not closed later on.

I have also found this error if i have forgot to close the my php with ?>

Ken

#16 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 09:24 PM

checked everything and no luck. This is a joke man. no one knows what the hell is wrong with that code. the code is getting on my nerves.  >:( .



#17 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 20 August 2006 - 09:25 PM

Yup, that's a bracket error.

Gimme everyting your got and I'll put it in good old Zend DE and fix it for you...

#18 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 20 August 2006 - 09:30 PM

Thanks for that.

index.php


<?php
session_start();

if (!$_SESSION["valid_user"])
{
// User not logged in, redirect to login page
Header("Location: login.php");
}



// Display Member information
echo "<p>User ID: " . $_SESSION["valid_id"];
echo "<p>Username: " . $_SESSION["valid_user"];
echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);

// Display Delete, Edit and add article links
echo "<p><a href=\"articles.php\">VIEW all Articles</a></p>";
echo "<p><a href=\"add.php\">Add New Article</a></p>";

echo"<br>";
// Display logout link
echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";
?>




edit.php

<?
// Connect database
include("dbConfig.php");

// Get all records in all columns from table and put it in $result.
$result=mysql_query("select * from articles ORDER BY id");

/*Split records in $result by table rows and put them in $row.
Make it looping by while statement. */
while($row=mysql_fetch_assoc($result)){

// Output
echo "ID : {$row['id']} <br/>";
echo "Title : {$row['title']} <br/>";
echo "Content : {$row['content']} <br/>";
echo "Filename : {$row['filename']} <hr>";

// Add a link with a parameter(id) and it's value.
echo '<a href="update.php?id='.$row['id'].'">Update</a>';
echo "<HR>";

}

mysql_close();
?>


update.php

<?php
// Connect database.
include("dbConfig.php");

if($_POST['Submit']){
// Get parameters from form.
$id=$_POST['id'];
$title=$_POST['title'];
$content=$_POST['content'];
$filename=$_POST['filename'];
//mysql_query("update articles set title='$title', content='$content', filename='$filename' where id='$id'");
$q="UPDATE `articles` SET title='$title', content='$content', filename='$filename' where id='$id'";
$r = mysql_query($q);
if ( !$r )
  {
 
  echo "ERROR Please try again";
}
else
{

header("location:edit.php");
exit;
}
}

// Get id parameter (GET method) from edit.php
$id=$_GET['id'];


// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from articles where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);
echo $id;



?>


<html>
<body>
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>Name :
<input name="name" type="text" id="title" value="<? echo $row['title']; ?>"/>
<br />
Email :
<input name="email" type="text" id="content" value="<? echo $row['content']; ?>"/>
<br />
Tel :
<input name="tel" type="text" id="filename" value="<? echo $row['filename']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>



#19 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 20 August 2006 - 10:18 PM

I take it back, it's not a bracket error. :P

Get rid of the shorthand tags ("<?") and replace them with proper tags ("<?php").

#20 pezzie

pezzie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 21 August 2006 - 12:59 PM

any help. i still have not figured out what is wrong with that dam code of mine.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users