Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Jun 2, 2006, 16:19   #1
safeasfcuk
SitePoint Guru
 
safeasfcuk's Avatar
 
Join Date: Jan 2006
Location: UK (SWANSEA)
Posts: 655
edit data in mysql?

hey guys im trying to edit my data in mysql i have 1 database and 4 tables. i wanted to keep it all on the same page. so far iv got as far as displaying all the items in a list im stuck trying to find a way of editing the data off the same page how would i do this?

this is the code i have so far for the listing of the items? any ideas ?
Code:
else if (isset($_GET['edit'])) {
?>

<form action="" method=post enctype=multipart/form-data>

 <p>Select the product range you want to edit<br>
 <input type=radio name=table value=clarice checked=checked> Clarice Cliff<br />
<input type=radio name=table value=welsh> Welsh Gaudy<br />
<input type=radio name=table value=deco> Deco Antique<br />
<input type=radio name=table value=royal> Royal Doulton<br />
<input type=Submit name="submit" value=Edit>
<?php

 if ($_POST['submit']) {
 $source=$_POST['table'];

include ("dbconnect.php");

$sql="SELECT * FROM " . mysql_real_escape_string($_POST['table']);

$result=mysql_query($sql) or die(mysql_error()); 
?>

<table width=293 border=1>
  <tr>
    <td width=171>Product Name</td>
    <td width=106>Update</td>
  </tr>
  <tr>
  <?php
  
while($rows=mysql_fetch_array($result)){
?>
	<td><?php echo $rows['name']; ?></td>
<td align="center"><a href="index.php?id=<? echo $rows['id']; ?>">update</a></td>
  </tr>
  <?php
  }
  


	?>
</table>
<?php
mysql_close();
}
}
safeasfcuk is offline   Reply With Quote
Old Jun 2, 2006, 17:46   #2
mattalexx
Not yet perfect
 
mattalexx's Avatar
 
Join Date: Oct 2005
Location: Taos, NM, US
Posts: 446
PHP Code:

mysql_query("UPDATE tb ..... ");

/* or */
mysql_query("INSERT ...");
http://dev.mysql.com/doc/mysql/en/insert.html
http://dev.mysql.com/doc/mysql/en/UPDATE.html
mattalexx is offline   Reply With Quote
Old Jun 2, 2006, 19:20   #3
vd01
SitePoint Member
 
Join Date: Mar 2006
Posts: 12
why not use phpmyadmin?
vd01 is offline   Reply With Quote
Old Jun 3, 2006, 01:25   #4
safeasfcuk
SitePoint Guru
 
safeasfcuk's Avatar
 
Join Date: Jan 2006
Location: UK (SWANSEA)
Posts: 655
i know how to do the command but i dont know what statment to use to get it to run after the user clicks on the link?
safeasfcuk is offline   Reply With Quote
Old Jun 3, 2006, 03:18   #5
KCgame
Consultant
 
KCgame's Avatar
 
Join Date: Mar 2005
Location: Singapore
Posts: 828
Quote:
Originally Posted by safeasfcuk
i know how to do the command but i dont know what statment to use to get it to run after the user clicks on the link?
I hope that I get your question right

You can have two alternatives to this problem. One is to use button and one is via link.
To use button trigger for editing:
PHP Code:

if(isset($_POST[Submit]){

//insert all commands here
}
To use links to update, you need to pass the variable into the processing page, like http://www.example.com?amt=3000 :
PHP Code:

//this is the processing page


$amount=$_REQUEST['amt'] //grab insert data from the url

//insert as per normal using $amount variable
Hope it helps
KCgame is offline   Reply With Quote
Old Jun 3, 2006, 03:38   #6
safeasfcuk
SitePoint Guru
 
safeasfcuk's Avatar
 
Join Date: Jan 2006
Location: UK (SWANSEA)
Posts: 655
thanks m8 great help worked it out now.

only prob is its working for the first item but when i click on item 2 or 3 in the list of products its bringing up an error.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/safeasfc/public_html/work/client/admin/index.php on line 250

this is my full code for this part can some one tell me where i gone wrong please?

Code:
else if (isset($_GET['edit'])) {
?>

<form action="" method=post enctype=multipart/form-data>

 <p>Select the product range you want to edit<br>
 <input type=radio name=table value=clarice checked=checked> Clarice Cliff<br />
<input type=radio name=table value=welsh> Welsh Gaudy<br />
<input type=radio name=table value=deco> Deco Antique<br />
<input type=radio name=table value=royal> Royal Doulton<br />
<input type=Submit name="submit" value=Edit>
<?php

 if ($_POST['submit']) {
 $source=$_POST['table'];

include ("dbconnect.php");

$sql="SELECT * FROM " . mysql_real_escape_string($_POST['table']);

$result=mysql_query($sql) or die(mysql_error()); 
?>

<table width=293 border=1>
  <tr>
    <td width=171>Product Name</td>
    <td width=106>Update</td>
  </tr>
  <tr>
  <?php
  
while($rows=mysql_fetch_array($result)){
?>
	<td><?php echo $rows['name']; ?></td>
<td align="center"><a href="index.php?id=<? echo $rows['id']; ?>&frmtable=<? echo $source; ?>">update</a></td>
  </tr>
  <?php
  }
  


	?>
</table>
<?php
mysql_close();
}
}

else if (isset($_GET['id'])) {

$id=$_REQUEST['id'];
$table = $_REQUEST['frmtable']; 

include ("dbconnect.php");

$sql="SELECT * FROM $table WHERE id='$id'";

$result=mysql_query($sql);

$rows=mysql_fetch_array($result); <=============LINE 250

?>

<form action= method=get name=frmedit>
<input name= frmname type=text value="<?php echo $rows['name']; ?> "  />


</form>
<?php

}
i tryed changing it to while($rows=mysql_fetch_array($result)){
AHH FIGURED SOMTHING OUT IF I GO STRAIGHT TO THE 3RD OF LAST ENTRY IS WORKS BUT NOT IF I PRESS BACK ON THE BROWSER DO I NEED TO CLEAR SOMTHING OR CLOSE SOMTHING?

Last edited by safeasfcuk; Jun 3, 2006 at 04:12.
safeasfcuk is offline   Reply With Quote
Old Jun 3, 2006, 04:14   #7
KCgame
Consultant
 
KCgame's Avatar
 
Join Date: Mar 2005
Location: Singapore
Posts: 828
May I know where is line 250?

Is it possible for you to use php tag to wrap around your codes instead of codes tag? Thanks.

I will try to help out later (got something to attend to now).
KCgame is offline   Reply With Quote
Old Jun 3, 2006, 04:20   #8
safeasfcuk
SitePoint Guru
 
safeasfcuk's Avatar
 
Join Date: Jan 2006
Location: UK (SWANSEA)
Posts: 655
im not sure what u mean by codes tags or php tag?

line 250 is marked liine 250 thanks m8
safeasfcuk is offline   Reply With Quote
Old Jun 3, 2006, 07:40   #9
KCgame
Consultant
 
KCgame's Avatar
 
Join Date: Mar 2005
Location: Singapore
Posts: 828
I mean displaying the codes in (easier to see ):
PHP Code:

else if (isset($_GET['edit'])) {

?>

<form action="" method=post enctype=multipart/form-data>

<p>Select the product range you want to edit<br>
<input type=radio name=table value=clarice checked=checked> Clarice Cliff<br />
<input type=radio name=table value=welsh> Welsh Gaudy<br />
<input type=radio name=table value=deco> Deco Antique<br />
<input type=radio name=table value=royal> Royal Doulton<br />
<input type=Submit name="submit" value=Edit>
<?php

if ($_POST['submit']) {
$source=$_POST['table'];

include (
"dbconnect.php");

$sql="SELECT * FROM " . mysql_real_escape_string($_POST['table']);

$result=mysql_query($sql) or die(mysql_error());
?>

<table width=293 border=1>
  <tr>
    <td width=171>Product Name</td>
    <td width=106>Update</td>
  </tr>
  <tr>
  <?php
  
while($rows=mysql_fetch_array($result)){
?>
    <td><?php echo $rows['name']; ?></td>
<td align="center"><a href="index.php?id=<? echo $rows['id']; ?>&frmtable=<? echo $source; ?>">update</a></td>
  </tr>
  <?php
  
}
  


    
?>
</table>
<?php
mysql_close
();
}
}

else if (isset(
$_GET['id'])) {

$id=$_REQUEST['id'];
$table = $_REQUEST['frmtable'];

include (
"dbconnect.php");

$sql="SELECT * FROM $table WHERE id='$id'";

$result=mysql_query($sql);

$rows=mysql_fetch_array($result); <=============LINE 250

?>

<form action= method=get name=frmedit>
<input name= frmname type=text value="<?php echo $rows['name']; ?> "  />


</form>
<?php

}
ok, safeasfcuk. I am unable to fgure out the purpose of this script myself.. can you give a little explanation or correct me if I misunderstand anything below.

1. From the script, it seems that you are testing if the incoming URL contains either 'edit' OR 'id' variables, right? What is the purpose of these variables? Will there be any chance these two variables appear at the same time?

2. Is the line 250 still prompting the SQL warnings without the while() ?

3. What is the Form action?
Code:
<form action= method=get name=frmedit>
4. Are you combining the processing page into the display page? It is advisable to create another page just for processing and later direct to the display page (for easy troubleshooting).
KCgame is offline   Reply With Quote
Old Jun 3, 2006, 10:04   #10
safeasfcuk
SitePoint Guru
 
safeasfcuk's Avatar
 
Join Date: Jan 2006
Location: UK (SWANSEA)
Posts: 655
managed to fix that but now. now i cant get the description into the text area box.
its displays the name and the price in the text box but not the description in the text area box. also the submit button dosnt seem to work?

im driving myself crazy now lol need a break i think.

this is the code i have so far all working except the descripton and the submit button.

im trying to display the data from a database into a form to be edited.]]
Code:
else if (isset($_GET['edit'])) {
?>

<form action="" method=post enctype=multipart/form-data>

 <p>Select the product range you want to edit<br>
 <input type=radio name=table value=clarice checked=checked> Clarice Cliff<br />
<input type=radio name=table value=welsh> Welsh Gaudy<br />
<input type=radio name=table value=deco> Deco Antique<br />
<input type=radio name=table value=royal> Royal Doulton<br />
<input type=Submit name="submit" value=Edit>
<?php

 if ($_POST['submit']) {
 $source=$_POST['table'];

include ("dbconnect.php");

$sql="SELECT * FROM " . mysql_real_escape_string($_POST['table']);

$result=mysql_query($sql) or die(mysql_error()); 
?>

<table width=293 border=1>
  <tr>
    <td width=171>Product Name</td>
    <td width=106>Update</td>
  </tr>
  <tr>
  <?php
  
while($rows=mysql_fetch_array($result)){
?>
	<td><?php echo $rows['name']; ?></td>
<td align="center"><a href="index.php?id=<? echo $rows['id']; ?>&frmtable=<? echo $source; ?>">update</a></td>
  </tr>
  <?php
  }
  


	?>
</table>
<?php
mysql_close();
}
}

else if (isset($_GET['id'])) {

$id=$_REQUEST['id'];
$table = $_REQUEST['frmtable']; 

include ("dbconnect.php");

$sql="SELECT * FROM $table WHERE id='$id'";

$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){

?>

<form action="" method="post" name=frmedit>
<input name= frmname type=text value="<?php echo $rows['name']; ?> "  />
<br />
<input name= frmprice type=text value="<?php echo $rows['price']; ?> "  />
<br />
<textarea name=frmdescription cols=50 rows=8 value="<?php echo $rows['description']; ?>"></textarea>
<br />
<input name="id" type="hidden" id="frmid" value="<? echo $rows['id']; ?>">
<br />
<input name="submit" type="button" value="submit" />
</form>
<?php
if (isset($_POST['submit'])) {

$name=$_REQUEST['frmname'];
$price=$_REQUEST['frmprice'];
$description=$_REQUEST['frmdescription'];
$id=$_REQUEST['frmid'];

echo $name, $price, $description, $id ;

}
}
}
safeasfcuk is offline   Reply With Quote
Old Jun 3, 2006, 18:55   #11
KCgame
Consultant
 
KCgame's Avatar
 
Join Date: Mar 2005
Location: Singapore
Posts: 828
To display value in the textbox:
PHP Code:

echo'<textarea name="frmdescription" cols="50" rows="8">'.$rows['description'].'</textarea>'; 

KCgame is offline   Reply With Quote
Old Jun 4, 2006, 06:39   #12
safeasfcuk
SitePoint Guru
 
safeasfcuk's Avatar
 
Join Date: Jan 2006
Location: UK (SWANSEA)
Posts: 655
is that why the submit button wont work either?
safeasfcuk is offline   Reply With Quote
Old Jun 4, 2006, 07:12   #13
KCgame
Consultant
 
KCgame's Avatar
 
Join Date: Mar 2005
Location: Singapore
Posts: 828
Quote:
Originally Posted by safeasfcuk
is that why the submit button wont work either?
No, I think your submit button fails to work because it does not contain a valid action...
Code:
<form action="" method=post enctype=multipart/form-data>
.
.
.
.
<form action="" method="post" name=frmedit>
KCgame is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 22:26.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved