10 Replies - 844 Views - Last Post: 22 January 2011 - 04:09 AM Rate Topic: ***** 1 Votes

#1 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

Help with php / mysql database update and deletescript

Posted 21 January 2011 - 10:41 PM

hi..everyone... i am newbie with php programming language and i had encounter some of the problem due to update and delete scripts ... hope u all can help me up with this .. when click update ..it turns our blank page and databases are not updated.... :no:

here is my code..


<?php include "connect.php"?>
<?php connect_db() ?>
<?php
$com_id = $_POST['com_id'];
$com_name = $_POST['name'];
$com_matric = $_POST['matric'];
$com_email = $_POST['email'];
$com_yearofstudy = $_POST['yearofstudy'];
$com_position = $_POST['position'];

$sql="SELECT * FROM committee ORDER BY com_id DESC";
$result=mysql_query($sql);

?>
<?php
while($rows=mysql_fetch_array($result)) {// Start looping table row 
?>
<html>
<head></head>
<body>
<form id="org_update" name="org_update" method="post" action="update_form.php">

<table>
<tr>
<td><strong>Name :</strong>:<?php echo $rows['com_name']; ?></td>
</tr>
<tr>
<td><strong>Matric :</strong><?php echo $rows['com_matric']; ?></td>
</tr>
<tr>
<td><strong>Email :</strong><?php echo $rows['com_email']; ?></td>
</tr>
<tr>
<td><strong>Year of Study :</strong><?php echo $rows['com_yearofstudy']; ?></td>
</tr>
<tr>
<td><strong>Position :</strong><?php echo $rows['com_position']; ?></td>
</tr>
<tr>
<td><input type="submit" value="Update" ></td>
</tr>
</table>
<input type="hidden" name="com_id" value="<?php echo $rows['com_id']; ?>" >
<input type="hidden" id="com_name" name="com_name" value="<?php echo $rows['com_name']; ?>" >
<input type="hidden" id="com_name" name="com_matric" value="<?php echo $rows['com_matric']; ?>" >
<input type="hidden" id="com_name" name="com_email" value="<?php echo $rows['com_email']; ?>" >
<input type="hidden" id="com_name" name="com_yearofstudy" value="<?php echo $rows['com_yearofstudy']; ?>" >
<input type="hidden" id="com_name" name="com_position" value="<?php echo $rows['com_position']; ?>" >
<td>
	<a href="delete_cs.php?userID=<?php echo $row['com_id']?>"><img src="images/delete.jpg"></a>
</td>

</form>

</body>
</html>
<?php
}
mysql_close();
?>



update_form.php


<?php include "connect.php"?>
<?php connect_db() ?>
<?php
$com_id = $_POST['com_id'];
$com_name = $_POST['com_name'];
$com_matric = $_POST['com_matric'];
$com_email = $_POST['com_email'];
$com_yearofstudy = $_POST['com_yearofstudy'];
$com_position = $_POST['com_position'];
echo $com_id;
$sql="SELECT * FROM committee WHERE com_id='$com_id'";
$result=mysql_query($sql);
?>


<html>
<head></head>
<body>
<h3>Computer Science Committee</h3>
		<form id="cs_chart" name="cs_chart" method="post" action="update_cs.php">
			<table>

				<tr>
					<td>Name: </td>
					<td><input type="text" name="com_name" value="<?php echo $com_name;?>"></td>
				</tr>
				<tr>
					<td>Matric: </td>
					<td><input type="text" name="com_matric" value="<?php echo $com_matric;?>"></td>
				</tr>
				<tr>
					<td>email: </td>
					<td><input type="text" name="com_email" value="<?php echo $com_email;?>"></td>
				</tr>
				<tr>
					<td>Year of Study: </td>
					<td><input type="text" name="com_yearofstudy" value="<?php echo $com_yearofstudy;?>"></td>
				</tr>
				<tr>
					<td>Position: </td>
					<td><input type="text" name="com_position" value="<?php echo $com_position;?>"></td>
				</tr>
				<tr>
					<td colspan="2">

					<input type="submit" value="UPDATE">
					</td>
				</tr>
				<input type="hidden" id="com_name" name="com_id" value="<?php echo $rows['com_id']; ?>" >

		</table>
</form>
</body>

</html>



update_cs.php

<?php include "connect.php"?>
<?php connect_db() ?>

<?php
			$com_id=$_POST['com_id'];
			echo $com_id;		
			$com_name = $_POST['com_name'];
			$com_matric = $_POST['com_matric'];
			$com_email = $_POST['com_email'];
			$com_yearofstudy = $_POST['com_yearofstudy'];
			$com_position = $_POST['com_position'];

			$sql ="UPDATE committee SET com_name='$com_name',com_matric='$com_matric',com_email='$com_email',com_yearofstudy='$com_yearofstudy',com_position = '$com_position'WHERE com_id = '$com_id'";	
									
				$result = mysql_query($sql);
				if($result){
				echo "Successful updated!<BR/>";
				echo "<a href=cs_result.php>View your chart>";

				exit();	
				}				
				else
				{
				echo "error";
				}
				mysql_close();	
?>



delete_cs.php

<?php include "connect.php"?>
<?php connect_db() ?>

<?php
			
				$com_id = $_GET['com_id'];
						
				$sql = "DELETE FROM committee WHERE com_id = $com_id";
												
				$result = mysql_query($sql);
				if($result){
				
				echo "<a href=cs_result.php>View your chart>";

				exit();	
				}				
				else
				{
				echo "error";
				}
				mysql_close();	
?>



in the end ..it turns our blank page and databases not updated ... :( ....
hope u all can help me out with tis.... thanks everyone ...

Is This A Good Question/Topic? 0
  • +

Replies To: Help with php / mysql database update and deletescript

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5304
  • View blog
  • Posts: 27,195
  • Joined: 10-May 07

Re: Help with php / mysql database update and deletescript

Posted 21 January 2011 - 11:02 PM

You set the variable result on line 12, but then you use it on line 16 without ever verifying that your select statement returned a value.

PHP will post a blank page when it doesn't know what to do. & when you code loops on values that don't exist, PHP doesn't know what to do.

Try the following statement. I suggest using this (or similar) anytime that you are working with SQL :

$sql="SELECT * FROM committee ORDER BY com_id DESC";
$result=mysql_query($sql);
if(!$result) {
  die("<p>The following SQL failed.</p><p>".$sql."</p>");
}
while($rows=mysql_fetch_array($result)) {// Start looping table row


Was This Post Helpful? 2
  • +
  • -

#3 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

Re: Help with php / mysql database update and deletescript

Posted 21 January 2011 - 11:27 PM

hey .... the database is updated now :D.... and now i am having trouble with delete now ... i cant delete yet .... :(

This post has been edited by newbieHQ: 21 January 2011 - 11:36 PM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5304
  • View blog
  • Posts: 27,195
  • Joined: 10-May 07

Re: Help with php / mysql database update and deletescript

Posted 21 January 2011 - 11:32 PM

<?php
  $com_id = $_GET['com_id'];
  if($com_id) {
    $sql = "DELETE FROM committee WHERE com_id = $com_id";
  }
  else die('com id was not set');
?>


Was This Post Helpful? 1
  • +
  • -

#5 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

Re: Help with php / mysql database update and deletescript

Posted 21 January 2011 - 11:47 PM

it returns com id was not set.

i just modified my delete_cs.php code..
here it is:

<a href="delete_cs.php?com_id=<?php echo $row['com_id']?>"><img src="images/delete.jpg"></a>



and it return error 'com id was not set'
:(

This post has been edited by newbieHQ: 21 January 2011 - 11:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

Re: Help with php / mysql database update and deletescript

Posted 22 January 2011 - 12:14 AM

here is the code i just modified again.


<a href="delete_cs.php?com_id=<?php echo $row['com_id'];?>"><img src="images/delete.jpg"></a>



delete_cs.php
  $com_id = $_GET['com_id'];
  if($com_id) {
    $sql = "DELETE FROM committee WHERE com_id = $com_id";
	echo "successful delete<BR/>";
	echo "<a href=cs_result.php>View your chart>";
  }
  else die('com id was not set');



and turns out the database is not updated ... :(
Was This Post Helpful? 0
  • +
  • -

#7 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

Re: Help with php / mysql database update and deletescript

Posted 22 January 2011 - 12:39 AM

problem solved

thanks no2pencil ...

<?php include "connect.php"?>
<?php connect_db() ?>

<?php
			
  $com_id = $_GET['com_id'];
  if($com_id) {
    $sql = "DELETE FROM committee WHERE com_id = '$com_id'";
    $result=mysql_query($sql);
	if(!$result) {
    die("<p>The following SQL failed.</p><p>".$sql."</p>");
    }

	echo "successful delete<BR/>";
	echo "<a href=cs_result.php>View your chart>";
    }
  else die('com id was not set');
	
?>


Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5304
  • View blog
  • Posts: 27,195
  • Joined: 10-May 07

Re: Help with php / mysql database update and deletescript

Posted 22 January 2011 - 12:44 AM

Glad you got it working :)

Also remember to validate your variables. Coding is easy, it's just syntax. But it takes a programmer to catch invalid input. :)
Was This Post Helpful? 0
  • +
  • -

#9 newbieHQ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 20-January 11

Re: Help with php / mysql database update and deletescript

Posted 22 January 2011 - 03:24 AM

haha ..thanks no2pencil .... erm... i have some question here and i do not understand about it ...


 die("<p>The following SQL failed.</p><p>".$sql."</p>");



what is the meaning of .$sql. ?

hope you can explain it for me ...
thanks once again .. :D
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Help with php / mysql database update and deletescript

Posted 22 January 2011 - 03:31 AM

$sql is your SQL query string

This post has been edited by Dormilich: 22 January 2011 - 03:31 AM

Was This Post Helpful? 0
  • +
  • -

#11 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Help with php / mysql database update and deletescript

Posted 22 January 2011 - 04:09 AM

The dots there are concatenation operators. That is to say that, for that die() call, you're piecing three strings together into one: "<p>The following SQL failed.</p><p>", the value of your $sql variable (so your query), and "</p>". Think of how + is used to output variable values inside strings when working with Javascript. The dot is basically the same thing in PHP.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1