Jump to content


PHP Error - Editing Rows


12 replies to this topic

#1 Erik Bernskiold

    Jedi In Training

  • Members
  • PipPip
  • 422 posts
  • Gender:Male
  • Location:Gothenburg, Sweden
  • Interests:I love to do booth Web Design and Photography. Nothing beats a nice day out in the nature with the camera gear, getting loads of nice photos. I have been playing the flute for 6 years now and I love it, and I am playing the pipe organ as well. I also like to teach other people the in and outs of software such as the CS3 suite from Adobe.

Posted 04 July 2006 - 09:06 AM

Ok, I have just finished a script which displays random quotes from a database and some more features. But I am having trouble with the edit quote part, simple it doesn't update as it should when you click submit. I highlighted the edit part in bold.

<?php 
$path = $_SERVER['SCRIPT_FILENAME'];
$path = str_replace('index.php', "", $path);
require_once("$path/header.php");
require_once("$path/config.php"); // Include config.php

$log = $_SESSION['log'];
$username = $_SESSION['username'];



if ($_GET['act'] == "add") { ?>
<h2>Add Quotes</h2><form name="quote_add" id="quote_add" method="post" action="process.php">
  <table border="0" cellspacing="0" cellpadding="4">
	<tr>
	  <td valign="top">Quote:</td>
	  <td><textarea name="quote" cols="25" rows="5" id="quote"></textarea></td>
	</tr>
	<tr>
	  <td>Person who said the quote: </td>
	  <td><input name="quoter" type="text" id="quoter" size="25" /></td>
	</tr>
	<tr>
	  <td>Page: (If in a book, movie) </td>
	  <td><input name="page" type="text" id="page" size="25" /></td>
	</tr>
	<tr>
	  <td colspan="2" align="center"><input name="Submit" type="submit" value="Submit" />&nbsp;&nbsp;<input name="Reset" type="reset" value="Reset" /></td>
	</tr>
  </table>
</form>
<? }

else if ($_GET['act'] == "adduser") { ?>
<h2>Add Quotes</h2><form name="user_add" id="user_add" method="post" action="adduser.php">
  <table border="0" cellspacing="0" cellpadding="4">
	<tr>
	  <td valign="top">Username:</td>
	  <td><input name="username" type="text" id="username" size="25" /></td>
	</tr>
	<tr>
	  <td>Password: </td>
	  <td><input name="password" type="text" id="password" size="25" /></td>
	</tr>
	<tr>
	  <td colspan="2" align="center"><input name="Submit" type="submit" value="Submit" />&nbsp;&nbsp;<input name="Reset" type="reset" value="Reset" /></td>
	</tr>
  </table>
</form>
<? }

else if ($_GET['act'] == "readme") {
include("readme.html");
}

[b]else if ($_GET['act'] == "update") {
$result = mysql_query("SELECT * FROM xld_quotes");
$count = mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="">
<td>
<table width="400" border="0" cellspacing="1" cellpadding="0">


<?
while ($rows = mysql_fetch_array($result)) { ?>
<tr>
<td align="center"><strong>Id</strong><br /><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><strong>Quote</strong><br /><textarea cols="25" rows="5" name="quote[]" id="quote"><? echo $rows['quote']; ?></textarea></td>
<td align="center"><strong>Quoter</strong><br /><input name="quoter[]" type="text" id="quoter" value="<? echo $rows['quoter']; ?>"><br />
<strong>Page</strong><br /><input name="page[]" type="text" id="page" value="<? echo $rows['page']; ?>"></td>
</tr>
<? } ?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
// Check if button name "Submit" is active, do this
if ($_POST['Submit']) {
for ($i=0; $i<$count; $i++) {
$sql1 = "UPDATE $table SET quote = '$quote[$i]', quoter = '$quoter[$i]', page = '$page[$i]' WHERE id = '$id[$i]'";
$result1 = mysql_query($sql1);
}
}

if ($result1) { ?>
<meta http-equiv="refresh" content="0;URL=index.php?act=update">[/b]
<? } 
} else {
			$install = 'installer.php';
			if (file_exists($install)) {
				echo "<font color=\"#FF0000\"><b>WARNING:</b> Installer.php is still present on the server. For security reasons, please delete this file immediately.</font>";
				echo "<br /><br />";
			}
			$number_quotes = mysql_num_rows(mysql_query("SELECT id FROM xld_quotes"));
			$number_admin = mysql_num_rows(mysql_query("SELECT id FROM xld_users")); 
?>
<table width="500" cellpadding="0" cellspacing="0">
	<tr>
		<td align="center" valign="top">
<table width="200" cellpadding="0" cellspacing="0" style="border:thin solid #000000;">
<tr>
<td width="200" height="20" align="center" background="images/bar.gif" style="color:#000000;"><b>Random Quote</b></td>
</tr>
<tr>
<td width="200"><?php include("quotes.php"); ?></td>
</tr>
</table>
		</td>
		<td align="center" valign="top">
<table width="200" cellpadding="0" cellspacing="0" style="border:thin solid #000000;">
<tr>
<td width="200" height="20" align="center" background="images/bar.gif" style="color:#000000;"><b>Version Check</b></td>
</tr>
<tr>
<td width="200"><strong>Your Version:</strong> <?php include("VERSION"); ?><br />
Latest Version: <?php echo file_get_contents('http://xldstudios.com/scripts/xldq/versionc.php'); ?>
</td>
</tr>
</table>
		</td>
	</tr>
	<tr>
		<td colspan="2">
<b>Welcome Back, <? echo $username; ?></b><br /><br />
<b>Welcome</b> to the Admin Control Panel (ACP) of XLD - Quotes. Are you in mood of adding quotes? Then just go to the Add Quotes link in the menue above.
<br /> <br />
		We thank you for purchasing XLD - Quotes and we hope you'll have a great time using it. We will answer any of your questions about this script on our site, so just pop over to it via the link above. You can also submit a support ticket in the client center.<br /><br />
				<b>Stats</b><br />
				Number of Quotes: <? echo $number_quotes; ?><br />
				Number of Users:<? echo $number_admin; ?></b>
</td>
</tr>
</table>
<? }		
include("footer.php"); 
?>

I am very thankful for replies!

#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 04 July 2006 - 09:13 AM

1) does anything happen, any errors, mysql_errors?

2) i have a feeling it might be to do with all of the $var[$i]'s. So I would remove the update query, and simply just echo all of them out. And if they don't echo out then they are not being set which would be the cause of your problem.

Matt

#3 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 04 July 2006 - 10:45 AM

<?php
$path = $_SERVER['SCRIPT_FILENAME'];
$path = str_replace('index.php', "", $path);
require_once("$path/header.php");
require_once("$path/config.php"); // Include config.php

$log = $_SESSION['log'];
$username = $_SESSION['username'];



if ($_GET['act'] == "add") { ?>
<h2>Add Quotes</h2><form name="quote_add" id="quote_add" method="post" action="process.php">
  <table border="0" cellspacing="0" cellpadding="4">
	<tr>
	  <td valign="top">Quote:</td>
	  <td><textarea name="quote" cols="25" rows="5" id="quote"></textarea></td>
	</tr>
	<tr>
	  <td>Person who said the quote: </td>
	  <td><input name="quoter" type="text" id="quoter" size="25" /></td>
	</tr>
	<tr>
	  <td>Page: (If in a book, movie) </td>
	  <td><input name="page" type="text" id="page" size="25" /></td>
	</tr>
	<tr>
	  <td colspan="2" align="center"><input name="Submit" type="submit" value="Submit" />&nbsp;&nbsp;<input name="Reset" type="reset" value="Reset" /></td>
	</tr>
  </table>
</form>
<? }

else if ($_GET['act'] == "adduser") { ?>
<h2>Add Quotes</h2><form name="user_add" id="user_add" method="post" action="adduser.php">
  <table border="0" cellspacing="0" cellpadding="4">
	<tr>
	  <td valign="top">Username:</td>
	  <td><input name="username" type="text" id="username" size="25" /></td>
	</tr>
	<tr>
	  <td>Password: </td>
	  <td><input name="password" type="text" id="password" size="25" /></td>
	</tr>
	<tr>
	  <td colspan="2" align="center"><input name="Submit" type="submit" value="Submit" />&nbsp;&nbsp;<input name="Reset" type="reset" value="Reset" /></td>
	</tr>
  </table>
</form>
<? }

elseif ($_GET['act'] == "readme") {
include("readme.html");
}

elseif ($_GET['act'] == "update") {
$result = mysql_query("SELECT * FROM xld_quotes");
$count = mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="">
<td>
<table width="400" border="0" cellspacing="1" cellpadding="0">


<?
while ($rows = mysql_fetch_array($result)) { ?>
<tr>
<td align="center"><strong>Id</strong><br /><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><strong>Quote</strong><br /><textarea cols="25" rows="5" name="quote[]" id="quote"><? echo $rows['quote']; ?></textarea></td>
<td align="center"><strong>Quoter</strong><br /><input name="quoter[]" type="text" id="quoter" value="<? echo $rows['quoter']; ?>"><br />
<strong>Page</strong><br /><input name="page[]" type="text" id="page" value="<? echo $rows['page']; ?>"></td>
</tr>
<? } ?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?
// Check if button name "Submit" is active, do this
if ($_POST['Submit']) {
for ($i=0; $i<$count; $i++) {
$sql1 = "UPDATE $table SET quote = '$quote[$i]', quoter = '$quoter[$i]', page = '$page[$i]' WHERE id = '$id[$i]'";
$result1 = mysql_query($sql1) or die(mysql_error());
}
}

if ($result1) { ?>
<meta http-equiv="refresh" content="0;URL=index.php?act=update">[/b]
<? }
} else {
			$install = 'installer.php';
			if (file_exists($install)) {
				echo "<font color=\"#FF0000\"><b>WARNING:</b> Installer.php is still present on the server. For security reasons, please delete this file immediately.</font>";
				echo "<br /><br />";
			}
			$number_quotes = mysql_num_rows(mysql_query("SELECT id FROM xld_quotes"));
			$number_admin = mysql_num_rows(mysql_query("SELECT id FROM xld_users"));
?>
<table width="500" cellpadding="0" cellspacing="0">
	<tr>
		<td align="center" valign="top">
<table width="200" cellpadding="0" cellspacing="0" style="border:thin solid #000000;">
<tr>
<td width="200" height="20" align="center" background="images/bar.gif" style="color:#000000;"><b>Random Quote</b></td>
</tr>
<tr>
<td width="200"><?php include("quotes.php"); ?></td>
</tr>
</table>
		</td>
		<td align="center" valign="top">
<table width="200" cellpadding="0" cellspacing="0" style="border:thin solid #000000;">
<tr>
<td width="200" height="20" align="center" background="images/bar.gif" style="color:#000000;"><b>Version Check</b></td>
</tr>
<tr>
<td width="200"><strong>Your Version:</strong> <?php include("VERSION"); ?><br />
Latest Version: <?php echo file_get_contents('http://xldstudios.com/scripts/xldq/versionc.php'); ?>
</td>
</tr>
</table>
		</td>
	</tr>
	<tr>
		<td colspan="2">
<b>Welcome Back, <? echo $username; ?></b><br /><br />
<b>Welcome</b> to the Admin Control Panel (ACP) of XLD - Quotes. Are you in mood of adding quotes? Then just go to the Add Quotes link in the menue above.
<br /> <br />
		We thank you for purchasing XLD - Quotes and we hope you'll have a great time using it. We will answer any of your questions about this script on our site, so just pop over to it via the link above. You can also submit a support ticket in the client center.<br /><br />
				<b>Stats</b><br />
				Number of Quotes: <? echo $number_quotes; ?><br />
				Number of Users:<? echo $number_admin; ?></b>
</td>
</tr>
</table>
<? }		
include("footer.php");
?>

Please try this and then post the error it gives (as i suspect it will).

Remember to ALWAYS use or die ('Error: '.mysql_error()); on all queries.

Edited by .Matt, 05 July 2006 - 09:14 AM.


#4 Erik Bernskiold

    Jedi In Training

  • Members
  • PipPip
  • 422 posts
  • Gender:Male
  • Location:Gothenburg, Sweden
  • Interests:I love to do booth Web Design and Photography. Nothing beats a nice day out in the nature with the camera gear, getting loads of nice photos. I have been playing the flute for 6 years now and I love it, and I am playing the pipe organ as well. I also like to teach other people the in and outs of software such as the CS3 suite from Adobe.

Posted 04 July 2006 - 01:58 PM

After doing what you told me Matt I got the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET quote = '\"Oh, what\'s it to you if my timetable is a bit full?\"', quoter =' at line 1


#5 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 04 July 2006 - 02:12 PM

ahh, ok i see the problem xD

Replace what you have with this:

$sql1 = "UPDATE `$table` SET quote = '$quote[$i]' WHERE id = '$id[$i]'";
$run1 = mysql_query($sql1) or die('Error: '.mysql_error();

$sql2 = "UPDATE `$table` SET quoter = '$quoter[$i]' WHERE id = '$id[$i]'";
$run2 = mysql_query($sql2) or die('Error: '.mysql_error();

$sql3 = "UPDATE `$table` SET page = '$page[$i]'  WHERE id = '$id[$i]'";
$run3 = mysql_query($sql3) or die('Error: '.mysql_error();

To my knowledge you cant update more than 1 field within a single UPDATE query. Should have seen that before.

Edited by .Matt, 04 July 2006 - 02:16 PM.


#6 Erik Bernskiold

    Jedi In Training

  • Members
  • PipPip
  • 422 posts
  • Gender:Male
  • Location:Gothenburg, Sweden
  • Interests:I love to do booth Web Design and Photography. Nothing beats a nice day out in the nature with the camera gear, getting loads of nice photos. I have been playing the flute for 6 years now and I love it, and I am playing the pipe organ as well. I also like to teach other people the in and outs of software such as the CS3 suite from Adobe.

Posted 04 July 2006 - 02:19 PM

OK, however now another error has appreared when I replaced

$sql1 = "UPDATE $table SET quote = '$quote[$i]', quoter = '$quoter[$i]', page = '$page[$i]' WHERE id = '$id[$i]'";
$result1 = mysql_query($sql1) or die(mysql_error());

with your code. The error I get is:

Parse error: syntax error, unexpected ';' in /home/wizardis/public_html/quotes/index.php on line 89


#7 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 04 July 2006 - 02:27 PM

Hi there,

Sorry, half asleep. replace with this:

$sql1 = "UPDATE `$table` SET quote = '$quote[$i]' WHERE id = '$id[$i]'";
$run1 = mysql_query($sql1) or die('Error: '.mysql_error());

$sql2 = "UPDATE `$table` SET quoter = '$quoter[$i]' WHERE id = '$id[$i]'";
$run2 = mysql_query($sql2) or die('Error: '.mysql_error());

$sql3 = "UPDATE `$table` SET page = '$page[$i]'  WHERE id = '$id[$i]'";
$run3 = mysql_query($sql3) or die('Error: '.mysql_error());

Forgot about the final ).

#8 Erik Bernskiold

    Jedi In Training

  • Members
  • PipPip
  • 422 posts
  • Gender:Male
  • Location:Gothenburg, Sweden
  • Interests:I love to do booth Web Design and Photography. Nothing beats a nice day out in the nature with the camera gear, getting loads of nice photos. I have been playing the flute for 6 years now and I love it, and I am playing the pipe organ as well. I also like to teach other people the in and outs of software such as the CS3 suite from Adobe.

Posted 05 July 2006 - 03:42 AM

Now it does update the quote, but it still doesn't update the quoter and page, any ideas?

EDIT: Now it updates it all fine! Thanks for all help, I really appriciate it all!

Edited by Erik B, 05 July 2006 - 04:15 AM.


#9 Jem

    Young Padawan

  • Members
  • Pip
  • 93 posts
  • Location:England
  • Interests:Photography, design &amp; developing, walking, cycling, reading.

Posted 05 July 2006 - 08:13 AM

View Post.Matt, on Jul 4 2006, 08:12 PM, said:

To my knowledge you cant update more than 1 field within a single UPDATE query. Should have seen that before.
Yes you can.. :)

#10 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 05 July 2006 - 09:13 AM

View PostJem, on Jul 5 2006, 02:12 PM, said:

View Post.Matt, on Jul 4 2006, 08:12 PM, said:

To my knowledge you cant update more than 1 field within a single UPDATE query. Should have seen that before.
Yes you can.. :)

Thats why i said to my knoledge, im interested to know as to how..... :D :)

I assume it is as the code was?
SET var = x, var = y

?

Edited by .Matt, 05 July 2006 - 09:19 AM.


#11 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 05 July 2006 - 09:57 AM

yea its simple:

$query = "UPDATE `picture` SET `picture_name` = '$name', `picture_desc` = '$desc', `cat_id` = '$cat1', `cat_id2` = '$cat2' WHERE `user_id` = '$user_id' AND `picture_id` = '$pic'";


#12 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 05 July 2006 - 10:33 AM

How the bloody hell (if you'll excuse the language) did i not know that? lol.

I'll be sure to use that.

#13 Erik Bernskiold

    Jedi In Training

  • Members
  • PipPip
  • 422 posts
  • Gender:Male
  • Location:Gothenburg, Sweden
  • Interests:I love to do booth Web Design and Photography. Nothing beats a nice day out in the nature with the camera gear, getting loads of nice photos. I have been playing the flute for 6 years now and I love it, and I am playing the pipe organ as well. I also like to teach other people the in and outs of software such as the CS3 suite from Adobe.

Posted 05 July 2006 - 10:44 AM

I'll be sure to remember that too ;) Again thanks for all your help everyone!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users